Execute comandos remotos via Telnet usando autenticação

De Wiki Hackstore

Requisitos

Compile o expect:

emerge dev-tcltk/expect

Crie o script expect

#!/usr/bin/expect

set timeout 20
set hostName [lindex $argv 0]
set userName [lindex $argv 1]
set password [lindex $argv 2]

spawn telnet $hostName

expect "Login:"
send "$userName\r"
expect "Password:"
send "$password\r";
expect ">"
send "reboot\r"
send  "quit\r"
expect eof


Crie um script bash para executar automaticamente (opcional)

vi reboot-router.sh
if [ -z "${1}" ] ; then
        echo -e "\n\033[1;31mPor favor, digite: <ip> <usuario> <senha>.\033[m\017\n\nEx:\n\n./$0 \033[1;31m192.168.1.1 admin admin123\033[m\017\n";
        exit
else
        /root/reboot.expect $1 $2 $3
fi

Dê permissão e execute o script

chmod +x reboot-router.sh
./reboot-router.sh 192.168.1.1 admin admin123
spawn telnet 192.168.1.1
Trying 192.168.1.1...
Connected to 192.168.1.1.
Escape character is '^]'.
BCM96328 Broadband Router
Login: admin
Password:
 > reboot

The system shell is being reset. Please wait...
 > quit

Bye bye. Have a nice day!!!
Connection closed by foreign host.