>cat routerscript
#used to log in to a router
#!/bin/expect -f
send_user "USAGE: \> routerscript \[routername or IP\] "
send_user "\n"
set hostname [lindex $argv 0]
spawn ssh $hostname
expect -exact "\:"
send -- "password"
send -- "\r"
expect -exact "\>"
send -- "\enable"
send -- "\r"
interact
I want to take a long text file such that has a list of routernames
(all have the same login PW etc) followed by commands and have them
run one by one.
such as
source text:
routerscript router1.domain.com
show int f0/2
show int f0/4
exit
routerscript router2.domain.com
show ver
exit
routerscript router3.domain.com
do-other-stuff
exit
Here is my question:
I found that I can simply paste in the below and it all works.
routerscript router1.domain.com
show int f0/2
show int f0/4
exit
(the script logs into the router and the commands will work and it
exits to the unix prompt).
but I have a text long list of routers and commands, is there a way I
can go through this list line by line, call the script log into each
device, run the command, then when it exits to the unix prompt, log
into the next device, and run the next set of commands etc.?
How can I get these to work together?
Thanks
Crzzy1
First things first, though; does Listing 4 in <URL:
http://www.ibm.com/developerworks/aix/library/au-expect/ > give
you enough of a hint?
-----------
Actually I did not know about the concurrent login ability.. That is
exciting and I definitely want to follow up on that.
I think here however it won't be concurrent since that appears to work
when you have the same commands to run for each device. they used a
password update as an example.
My text file in this case has different commands to run for each
device so what I need is to
read text line by line
If {^routerscript*}
then use the router login script to telnet into device.
then run the commands
IF you run into another device.
Exit the device you are in
then use the router login script to telnet into device.
then run the commands
So my problem is now how do I get my login script to read the text
file?
maybe I need to use a bash or awk script etc that I can use to read
the text file and then call the expect script and then run the
commands?
---------
No. Well, yes, you *could* complexify your Expect scripting
with bash or awk, but I don't recommend it. As the article
already mentions, Expect is widely misunderstood in several
regards, including that
Expect is ... a powerful, general-purpose language
Expect can read stuff from a text file without outside help:
set fp [open $filename]
set content [read $fp]
close $fp
puts "Here's everything in $filename:\n--------------\n$content"
You can even be a bit brutal and write
set content [exec cat $filename]
'Think you need just a line of the contents at a time?
foreach line [split $content \n] {
puts "This line is '$line'."
}