Cisco Support Community
cancel
Showing results for 
Search instead for 
Did you mean: 
Announcements

Welcome to Cisco Support Community. We would love to have your feedback.

For an introduction to the new site, click here. If you'd prefer to explore, try our test area to get started. And see here for current known issues.

New Member

Script to automate tasks

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?

Everyone's tags (3)
1 ACCEPTED SOLUTION

Accepted Solutions
Silver

Script to automate tasks

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
24 REPLIES
Silver

Re: Script to automate tasks

try expect script, below is an example

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

Siddhartha

Siddhartha
New Member

Script to automate tasks

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?

Script to automate tasks

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

Cheers

Sean

Silver

Script to automate tasks

Thanks Sean.

Siddhartha
Silver

Script to automate tasks

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
New Member

It helped me much!

It helped me much!

thạnks siddhartham

Hi guys. I was trying to run

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  . 

New Member

Maybe your Expect library is

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

New Member

hi Laura, I have already

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.

Hall of Fame Super Blue

Is "/usr/bin/expect" where

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

Jon

New Member

hi again 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!

New Member

after been solved the problem

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.

Hall of Fame Super Blue

The script would need a bit

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

Hall of Fame Super Blue

Forgot to add that Expect is

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

New Member

thank you very much Jon, you

thank you very much Jon, you helped me a lot!!!! 

I{ll keep looking and testing.

Thanks again!

New Member

Re: Script to automate tasks

hi

i am not that doog a scripting but part "while read  ipadd" in the ssh.sh file isn´t working for me.

 

New Member

how did you run it what are

how did you run it what are the setups and connection to the cisco devices and the computer?please help me.

New Member

sorry prabhuprakash781, I can

New Member

Hi,I

Hi,I

I want to know that, before running the script how should I connect the PC and cisco switch (either through the console or ethernet port) and any other setups needed on the PC.

how the script communicate with the switch?

New Member

PC must be on the same

PC must be on the same network that Switch, Routers, etc. Connect PC via ethernet cable to Swith where the other equipments are. Are you using Linux or Windows??

New Member

I'm Linux user...

I'm Linux user...

follow the answer posted by siddhartham that's marked as the correct answer. It's very simple.

In linux you have to install expect and create the files in the main directoy....simple as that. Just keep in mind that you need to modify the script according to yor needs.

New Member

Thank you so much for your

Thank you so much for your responses.i will try with your tips. 

New Member

I have both .tell me for both

I have both .tell me for both.

New Member

hi, thanks for the example,

hi, thanks for the example, it works fine but how can i execute commans that needs an input like this one:

        send "crypto key generate rsa\r"
        expect "Do you really want to replace them? [yes/no]:"
        send "y\r"
        expect "*[512]:"

i get this error:

sw3-foptop(config)#invalid command name "yes/no"
    while executing
"yes/no"
    invoked from within
"expect "% Do you really want to replace them? [yes/no]:""

53348
Views
15
Helpful
24
Replies