Automate Telnet Login in BASH

This method will basically automate telnet login and run a command on a router. It doesn’t use TELNET, it uses ncat. The alternative to this approach would be to use the expect command and create a script. The following command connects to a router via port 23 (telnet port) and issues a sh clock command.

printf "username
password
sh clock
exit
" | ncat 192.168.10.1 23


root@xyzzy:~# printf "admin
s3cr3t
sh clock
exit
" | ncat 192.168.10.1 23
User Access Verification
Username: admin
Password:
cisco#sh clock
12:17:54.924 EDT Mon Jun 2 2014
cisco#exit

Note: I haven’t figured out a method to put a delay before the exit command. So if you have a slow link or you’re requesting a lot of information, like a sh run , it’ll bomb out early.

#Cisco