Automating with expect

i just worked a bit on getting access to lcli automated using expect.

  • to avoid having to go through the retarded menu login to access lcli

(although it should somehow be possible to make lcli the default shell instead of "menu"…?)

  • to automate lcli interactions

(all i really wanted was to get a list of MAC addresses connected to each port, to automtically generate labels for traffic graphs…)

please check some basic docs on expect first:
[http://expect.nist.gov/]

this basic expect script answers the menu login for you (password and switch ip hardcoded) and drops you into lcli
(without clobbering the screen!)
(pretty rough still… should get rid of hardcoding stuff, and allow exiting cleanly…)

#!/usr/bin/expect --
log_user 0
spawn ssh -tt 172.16.128.8
expect Password:
send "USER\tPASSWORD\r"
expect logout
send "\032"
expect "The Telnet/SSH session may still be connected."
send "\n"
expect ">" 
send "lcli\n"
expect "User Name:"
send "USER\n"
expect "Password:"
send "PASSWORD\n"
log_user 1
interact

and this gets me the mac list (some parsing still needed, i don't know enough expect yet to do it inside expect):

#!/usr/bin/expect --
log_user 0
spawn ssh -tt 172.16.128.8
expect Password:
send "USER\tPASSWORD\r"
expect logout
send "\032"
expect "The Telnet/SSH session may still be connected."
send "\n"
expect ">"
send "lcli\n"
expect "User Name:"
send "USER\n"
expect "Password:"
send "PASSWORD\n"
expect "console#"
send "show bridge addr\n"
expect -- "-------- --------------------- ------ ----------"
log_user 1
expect_before {
    "console#" {
        send "exit\n"
        exit
    }
}
expect {
    "More: <space>" {
        send " "
        exp_continue
    }
}
Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution-ShareAlike 3.0 License