Expecting
by Russ • 19 October 2006 • system administration • 0 Comments
As part of two positions, I often have to reboot a remote system, on a remote power server. I mean ‘power cycle,’ not just reboot. What this entails is telnetting into a remote server, and walking through a predictible set of menus and commands. I finally gave up and scripted it; I used ‘expect’ because it was, frankly, the easiest.
‘Expect’ is used to mimic a series of commands. It’s a scripting language like PHP or Perl, but older, and it has a different syntax. It’s also related to Tcl (tickle) programming… Here’s the example. Use it as you’d like; I’m sure there’s more ways to make it work (it’d be nice to add more options, so I can use the same script to reboot several servers).
set timeout 60
spawn telnet ip address
expect "User Name*"
send "username\r"
expect "Password*"
send "password\r"
expect ">"
send "1\r"
expect ">"
send "1\r"
expect ">"
send "1\r"
expect ">"
send "3\r"
expect "cancel*"
send "YES\r"
