Escribe un expect
guión.
Aquí hay un ejemplo:
#!/usr/bin/expect
#If it all goes pear shaped the script will timeout after 20 seconds.
set timeout 20
#First argument is assigned to the variable name
set name [lindex $argv 0]
#Second argument is assigned to the variable user
set user [lindex $argv 1]
#Third argument is assigned to the variable password
set password [lindex $argv 2]
#This spawns the telnet program and connects it to the variable name
spawn telnet $name
#The script expects login
expect "login:"
#The script sends the user variable
send "$user "
#The script expects Password
expect "Password:"
#The script sends the password variable
send "$password "
#This hands control of the keyboard over to you (Nice expect feature!)
interact
Para ejecutar:
./myscript.expect name user password
Si bien sugeriría usar expect
, también, para uso no interactivo, los comandos de shell normales pueden ser suficientes. telnet
acepta su comando en stdin, por lo que solo necesita canalizar o escribir los comandos a través de heredoc:
telnet 10.1.1.1 <<EOF
remotecommand 1
remotecommand 2
EOF
(Editar:a juzgar por los comentarios, el comando remoto necesita algo de tiempo para procesar las entradas o el SIGHUP temprano no es tomado con gracia por telnet
. En estos casos, puede intentar dormir un poco en la entrada :)
{ echo "remotecommand 1"; echo "remotecommand 2"; sleep 1; } | telnet 10.1.1.1
En cualquier caso, si se vuelve interactivo o algo así, usa expect
.
Telnet se usa a menudo cuando aprende el protocolo HTTP. Solía usar ese script como parte de mi web-scraper:
echo "open www.example.com 80"
sleep 2
echo "GET /index.html HTTP/1.1"
echo "Host: www.example.com"
echo
echo
sleep 2
digamos que el nombre del script es get-page.sh y luego:
get-page.sh | telnet
le dará un documento html.
Espero que sea útil para alguien;)