2015-06-25

Telnet autologin with a short Expect script

Couldn't find anything concise, so here's my solution: a sweet short Expect (the CLI variety installed by default in Ubuntu) script to open a telnet session, feed in the user and password, AND continue to an interactive session.




If you need anything else, take a look at autoexpect, it does pretty much everything for you! (Ever tried recording in the AppleScript Editor? Ah, the memories…)

Obligatory warning: storing passwords as we’re doing here is BAD. We could improve things SLIGHTLY by feeding the password as a command line parameter, but that is still BAD. I am using this just because the systems to which I auto-log-in are devices in development on my own desktop, on a local network, whose passwords will be changed pre-shipping, and my computer is single-user. You've been warned. You will have deserved whatever happens to you!


Save this in a text file, say autologin.exp; make it executable with chmod u+x autologin.exp; run it as ./autologin.exp 11.22.33.44 .

#!/usr/bin/expect -f
set IP [lindex $argv 0]
set timeout -1
spawn telnet $IP
expect “login: “
send “mylogin\n”
expect “\nPassword: “
send “mypassword\n”
interact
send_user “EXPECT SCRIPT FINISHED\n”

No comments

Post a Comment