Using expect with the F5

Now this one is a little weird.  While it is a *NIX box you can ssh into, the ‘command shell’ tmsh adds some bells and whistles to make it more ‘fun’ for interactive users.  Things like colour, and a pager.  Even worse is that it embeds a bunch of ANSI control sequences in there.

So instead of fighting with a lot of post scrubbing, I googled around, and found you can can invoke tmsh with the -e flag to remove a bunch of the ‘nice features’.  However the pager still embeds itself in the stream.  Apparently a bunch of people re-config the devices to the page length is insanely huge.  I don’t like the idea of making changes just to pull a config, or poke around in an automated fashion.

So luckily there is also the -c flag which let’s us submit a command and get back the results in a nice batch fashion.  And we don’t have to bang the space bar like a crazed lunatic.

#!/usr/local/bin/expect —
set MYUSER “root”
set MYPASS “g00Dp455w0rd#”

set HOST [lindex $argv 0];
set timeout 90
if {$argc!=1} {
puts “Usage is scritpname <ip address>\r”
exit 1
}

#
#
puts “Connecting to $HOST\r”
# turn off stdout
#log_user 0

spawn ssh $HOST -l $MYUSER
# Deal with hosts we’ve never talked to before
# or just logon
#
expect {
“*yes/no*” {send “yes\r” ; exp_continue }
“*assword:” {send “${MYPASS}\r” }
}
expect “*# “

send “tmsh -e -c \”show running-config\”\r”
expect “*(y/n)*”
send “y\r”

#Let’s get out of here
#send “quit\r”
expect “*~ #”
send “exit\r”
expect eof
exit 0

And that’s it!  This one is really simple, compared to the others.

Leave a Reply