cancel
Showing results for 
Search instead for 
Did you mean: 
cancel
173287
Views
44
Helpful
29
Replies

Script to automate tasks

perpaal
Level 1
Level 1

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?

1 Accepted Solution

Accepted Solutions

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

Siddhartha

View solution in original post

29 Replies 29

siddhartham
Level 4
Level 4

try expect script, below is an example

http://www.corecoding.com/cisco-expect-script_c32.html

Siddhartha

Siddhartha

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?

You can use EEM to run scripts on the switch itself: https://supportforums.cisco.com/community/netpro/private/pilot/eem

Cheers

Sean

Thanks Sean.

Siddhartha

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

Siddhartha

It helped me much!

thạnks siddhartham

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  . 

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

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.

Is "/usr/bin/expect" where the expect interpreter is on your system ?

Jon

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!

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.

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

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

Review Cisco Networking for a $25 gift card