[wplug] Bash Expect usage - Automating telnet

scoob8000 scoob8000 at gmail.com
Thu May 8 08:09:09 EDT 2014


So I've never used expect before and I'm really struggling.   I'm
trying to come up with a script to telnet into a bunch of devices, run
a few commands then log it.

I'm eventually going to have it read a list of IP's, and using the
exit status of the telnet put the IP address in either a pass.txt or
fail.txt logfile.   That part I should be able to figure out.

Here's what I have now based off of some examples I found online.

I think I'm missing something pretty simple here, but I'm stumped.  :)


#!/bin/bash
host=$1
export HISTIGNORE="expect*";
expect -c "
set timeout 20
if [catch "spawn telnet $host" reason] {
send_user "failed to spawn program: $reason\n"
exit 1
}

expect \"Password:\"
send \"oldpasswordr\"
expect \"Router>\"
send \"sys password newpassword\"
expect \"save ok, new password saved...\"
send \"sys cur2def\r\"
expect \"OK"\"
send \"exit\r\"
expect eof"
export HISTIGNORE="";


More information about the wplug mailing list