07-15-2012 04:52 PM - edited 03-07-2019 07:47 AM
Hi
I`m looking to automate some functions in a web application.
Functions like enable/disable interfaces on a Cisco 3550 switch.
Anybody know if there are some good scrips, or examples anywhere to get me started?
Solved! Go to Solution.
07-16-2012 07:11 AM
Perpaal,
Below are the example scripts that shuts down an interface and logs the session into a directory.
File1- IP-list -- contains a list of IPs that you want to execute the script
File2- ssh.sh -- reads the IP-list and execute File3 which is enableint script
File3 - enableint.sh --- script to shuts down an interface
Make ssh.sh and enableint.sh files as executable with chmod +x and you just need to run ./ssh.sh
File1-IP-list
10.10.1.1
10.2.3.2
File2- ssh.sh
#!/bin/bash
while read ipadd
do
#echo $ipadd
export ipadd
./enableint.sh $ipadd
done < IP-list
File3- enableint.sh
#!/usr/bin/expect -f
set timeout 20
set IPaddress [lindex $argv 0]
set Username "username"
set Password "password"
set Directory /home/Desktop/logs
log_file -a $Directory/session_$IPaddress.log
send_log "### /START-SSH-SESSION/ IP: $IPaddress @ [exec date] ###\r"
spawn ssh -o "StrictHostKeyChecking no" $Username@$IPaddress
expect "*assword: "
send "$Password\r"
expect "#"
send "conf t\r"
expect "(config)#"
send "int g0/0\r"
expect "(config-if)#"
send "shut\r"
expect "(config-if)#"
send "exit"
expect "(config)#"
send "exit"
expect "#"
send "wr mem\r"
expect "#"
send "exit\r"
sleep 1
send_log "\r### /END-SSH-SESSION/ IP: $IPaddress @ [exec date] ###\r"
exit
Siddhartha
07-15-2012 08:01 PM
try expect script, below is an example
http://www.corecoding.com/cisco-expect-script_c32.html
Siddhartha
07-16-2012 04:49 AM
Thanks a lot.
That really helped me a lot.
I also found a example for backing up config via TFTP that worked great.
Do you know any other resources with more examples, example enable, disable interfaces etc?
07-16-2012 06:35 AM
You can use EEM to run scripts on the switch itself: https://supportforums.cisco.com/community/netpro/private/pilot/eem
Cheers
Sean
07-16-2012 07:02 AM
Thanks Sean.
07-16-2012 07:11 AM
Perpaal,
Below are the example scripts that shuts down an interface and logs the session into a directory.
File1- IP-list -- contains a list of IPs that you want to execute the script
File2- ssh.sh -- reads the IP-list and execute File3 which is enableint script
File3 - enableint.sh --- script to shuts down an interface
Make ssh.sh and enableint.sh files as executable with chmod +x and you just need to run ./ssh.sh
File1-IP-list
10.10.1.1
10.2.3.2
File2- ssh.sh
#!/bin/bash
while read ipadd
do
#echo $ipadd
export ipadd
./enableint.sh $ipadd
done < IP-list
File3- enableint.sh
#!/usr/bin/expect -f
set timeout 20
set IPaddress [lindex $argv 0]
set Username "username"
set Password "password"
set Directory /home/Desktop/logs
log_file -a $Directory/session_$IPaddress.log
send_log "### /START-SSH-SESSION/ IP: $IPaddress @ [exec date] ###\r"
spawn ssh -o "StrictHostKeyChecking no" $Username@$IPaddress
expect "*assword: "
send "$Password\r"
expect "#"
send "conf t\r"
expect "(config)#"
send "int g0/0\r"
expect "(config-if)#"
send "shut\r"
expect "(config-if)#"
send "exit"
expect "(config)#"
send "exit"
expect "#"
send "wr mem\r"
expect "#"
send "exit\r"
sleep 1
send_log "\r### /END-SSH-SESSION/ IP: $IPaddress @ [exec date] ###\r"
exit
Siddhartha
03-08-2016 08:04 PM
It helped me much!
thạnks siddhartham
03-16-2016 12:45 PM
Hi guys. I was trying to run these scripts but i have errors: command not found for each line of enableint.sh , I have packages make and expect installed on my cygwin (running from windows 7), scripts have exe permission.
When i try to run another way :
expect ssh.sh
invalid bareword "read"
in expression "read";
should be "$read" or "{read}" or "read(...)" or ...
(parsing expression "read")
invoked from within
"while read ipadd"
(file "ssh.sh" line 3)
Can you help please hot to fix this ? I have more than 100 R&S .
12-26-2016 06:28 AM
Maybe your Expect library is not called correctly at the beginning of your script. You can make sure it is where you're calling it from, in this case /usr/bin/expect, by typing "which expect".
For your second method, you should be running it as ./ssh.sh
04-29-2017 11:30 AM
hi Laura, I have already modified these scripts according to my needs (change ip route in some 1841's Routers) but I'm getting this error: ./ssh.sh: ./iproute.sh: /usr/bin/expect: intérprete erróneo: No existe el fichero o el directorio. Can you help me please?? I know this is an old thread but is the only one about scripting in Cisco that I found so far.
Thanks in advance.
BR.
04-29-2017 12:21 PM
Is "/usr/bin/expect" where the expect interpreter is on your system ?
Jon
04-29-2017 01:29 PM
hi again Jon!
I´m totally new in scripting Cisco equipment. About yor question, I guess not. Let me check and get back to you.
Thanks!
04-29-2017 09:31 PM
after been solved the problem with the expect interpreter, I ran the script and all went fine!!!
Now I have a question: what about if any of my Routers has a different user/pass from specified in the script and I don't know it??? the script stucks??? how can I avoid this?? the script can generate a file with ip addresses of these Routers in order for me to know wich are them??
Thanks in advance.
04-30-2017 03:18 AM
The script would need a bit of modification but yes you could log an unsuccessful password attempt to a file.
It's been a while since I used Expect unfortunately so can't help with exact syntax.
Jon
04-30-2017 03:48 AM
Forgot to add that Expect is based on TCL and there is an EEM forum (which uses TCL) on this site so you could post there.
With Expect you can write the code to take account of multiple things. The above script expects to see a password prompt ie. expect "*assword: " but you can also add code to expect to see a password denied or whatever the router says when you enter a bad password and take action on that ie. write something to a log file.
So the script above is not doing any error checking ie. it is assuming everything works well which it may not do (no offense intended to the author, I have done the same thing myself many times).
Should also say that if you are familiar with Perl or Python scripting languages they have an Expect extension so you don't have to use TCL if you don't want to.
Jon
Discover and save your favorite ideas. Come back to expert answers, step-by-step guides, recent topics, and more.
New here? Get started with these tips. How to use Community New member guide