cancel
Showing results for 
Search instead for 
Did you mean: 
cancel
11500
Views
0
Helpful
8
Replies

Improving Expect script

gasparmenendez
Level 3
Level 3

Hi folks, I don't know if this is the right place to post this but I'll take the shot...

I'm using a script to change the ip route in some 1841's Cisco Routers and the script is working fine. As far as I know all routers has the same user/pass and enable password too. But, what happens if any of the routers has a different user/pass or enable password??? There are 3 possible scenarios of error: different username, different password or different enable password. I have a 1841 for testing purposes so I can do any test.

What I need to do is modifify that script in order to achieve two things: the first one is that when the script finds a bad user/pass or enable pass, that Router's ip goes to a log file called error.txt for example. And the second thing is that the script stops and continue with next ip on the list. Is it possible to do these??? Is it very complicated??? Here're my 3 scripts:

lista.txt:

Router's ip addresses

iproute.sh:


#!/usr/bin/expect -f


        set timeout 20
        set IPaddress [lindex $argv 0]
        set Username "extra"
        set Password "GGLextra10"
        set enablepassword "NETSistemas10"
        set Directory /home/gaspar/logs


        log_file -a $Directory/session_$IPaddress.log
        send_log "### /START-SSH-SESSION/ IP: $IPaddress @ [exec date] ###\r"


        spawn telnet$IPaddress

        expect "*username: "
        send "$Username\r"
        expect "*assword: "
        send "$Password\r"
        expect ">"
        send "en\r"
        expect "*assword: "
        send "$enablepassword\r"
        expect "#"
        send "conf t\r"
        expect "(config)#"
        send "no ip route 0.0.0.0 0.0.0.0 10.227.224.1\r"
        expect "(config)#"
        send "ip route 0.0.0.0 0.0.0.0 10.227.224.3\r"
        expect "(config)#"
        send "end\r"
        expect "#"
        send "wr mem\r"
        expect "#"
        send "disable\r"
        expect ">"
        send "exit\r"
        sleep 1
        send_log "\r### /END-SSH-SESSION/ IP: $IPaddress @ [exec date] ###\r"
exit

ssh.sh:

#!/bin/bash

while read  ipadd
do
#echo $ipadd
export ipadd
./iproute.sh $ipadd
done < lista.txt

Can anybody helpme please??

Thanks in advance.

BR.

1 Accepted Solution

Accepted Solutions

Try this one.

#!/usr/bin/expect -f


        set timeout 20
        set IPaddress [lindex $argv 0]
        set Username "extra"
        set Password "GGLextra10"
        set enablepassword "NETSistemas10"
        set Directory /home/gaspar/logs


        log_file -a $Directory/session_$IPaddress.log
        send_log "### /START-SSH-SESSION/ IP: $IPaddress @ [exec date] ###\r"
        
        set fd [open $Directory/error.txt "a"]


        spawn telnet$IPaddress

        expect "*username: "
        send "$Username\r"
        expect "*assword: "
        send "$Password\r"
        expect {
            -re ">" {}
            timeout {puts $fd $IPaddress; close $fd; exit 1}
        }
        send "en\r"
        expect "*assword: "
        send "$enablepassword\r"
        expect {
            -re "#" {}
            timeout {puts $fd $IPaddress; close $fd; exit 1}
        }
        send "conf t\r"
        expect "(config)#"
        send "no ip route 0.0.0.0 0.0.0.0 10.227.224.1\r"
        expect "(config)#"
        send "ip route 0.0.0.0 0.0.0.0 10.227.224.3\r"
        expect "(config)#"
        send "end\r"
        expect "#"
        send "wr mem\r"
        expect "#"
        send "disable\r"
        expect ">"
        send "exit\r"
        sleep 1
        send_log "\r### /END-SSH-SESSION/ IP: $IPaddress @ [exec date] ###\r"
        
        close $fd
exit

View solution in original post

8 Replies 8

Joe Clarke
Cisco Employee
Cisco Employee

This should work for you.  The continue will just happen based on your wrapper script.

#!/usr/bin/expect -f


        set timeout 20
        set IPaddress [lindex $argv 0]
        set Username "extra"
        set Password "GGLextra10"
        set enablepassword "NETSistemas10"
        set Directory /home/gaspar/logs


        log_file -a $Directory/session_$IPaddress.log
        send_log "### /START-SSH-SESSION/ IP: $IPaddress @ [exec date] ###\r"
        
        set fd [open $Directory/error.txt "a"]


        spawn telnet$IPaddress

        expect "*username: "
        send "$Username\r"
        expect "*assword: "
        send "$Password\r"
        expect {
            -re ">"
            timeout {puts $fd $IPaddress; close $fd; exit 1}
        }
        send "en\r"
        expect "*assword: "
        send "$enablepassword\r"
        expect {
            -re "#"
            timeout {puts $fd $IPaddress; close $fd; exit 1}
        }
        send "conf t\r"
        expect "(config)#"
        send "no ip route 0.0.0.0 0.0.0.0 10.227.224.1\r"
        expect "(config)#"
        send "ip route 0.0.0.0 0.0.0.0 10.227.224.3\r"
        expect "(config)#"
        send "end\r"
        expect "#"
        send "wr mem\r"
        expect "#"
        send "disable\r"
        expect ">"
        send "exit\r"
        sleep 1
        send_log "\r### /END-SSH-SESSION/ IP: $IPaddress @ [exec date] ###\r"
        
        close $fd
exit

thank you very much!!!

I'll try it and post results...

sorry Joe, didn't work...when I run the script I get this:

Prueba>invalid command name "timeout"
    while executing
"timeout"
    invoked from within
"expect {
            -re ">"
            timeout {puts $fd $IPaddress; close $fd; exit 1}
        }"
    (file "./iproutenew.sh" line 24)

Try this one.

#!/usr/bin/expect -f


        set timeout 20
        set IPaddress [lindex $argv 0]
        set Username "extra"
        set Password "GGLextra10"
        set enablepassword "NETSistemas10"
        set Directory /home/gaspar/logs


        log_file -a $Directory/session_$IPaddress.log
        send_log "### /START-SSH-SESSION/ IP: $IPaddress @ [exec date] ###\r"
        
        set fd [open $Directory/error.txt "a"]


        spawn telnet$IPaddress

        expect "*username: "
        send "$Username\r"
        expect "*assword: "
        send "$Password\r"
        expect {
            -re ">" {}
            timeout {puts $fd $IPaddress; close $fd; exit 1}
        }
        send "en\r"
        expect "*assword: "
        send "$enablepassword\r"
        expect {
            -re "#" {}
            timeout {puts $fd $IPaddress; close $fd; exit 1}
        }
        send "conf t\r"
        expect "(config)#"
        send "no ip route 0.0.0.0 0.0.0.0 10.227.224.1\r"
        expect "(config)#"
        send "ip route 0.0.0.0 0.0.0.0 10.227.224.3\r"
        expect "(config)#"
        send "end\r"
        expect "#"
        send "wr mem\r"
        expect "#"
        send "disable\r"
        expect ">"
        send "exit\r"
        sleep 1
        send_log "\r### /END-SSH-SESSION/ IP: $IPaddress @ [exec date] ###\r"
        
        close $fd
exit

Can you please let me know, how you executed the script from command line.

gasparmenendez
Level 3
Level 3

excellent my friend, now the script worked as expected!!!

I have another question: is there anyway for me to know dhcp pool's name in Router??? Let me explain:

each Router has a dhcp pool and I need to change dns-server's on every pool, but the problem is that in some of the Routers the pool has a different name...do you know what I mean??

thanks a lot!!

You can save the output of a command after an expect block.  For example:

send "command\r"

expect {

  -re "#" {}

  timeout {...}

}

set out $expect_out(buffer)

Then you can parse the $out variable to extract what you need.

I think I can do this:

.

.

.

send "sh ip dhcp pool | i Pool\r"

expect {

  -re "Pool * :" {}

  timeout {...}

}

set out $expect_out(buffer)

.

.

.

where the " * " is the name of the pool, that can be different for each Router, but the question is: how can I parse the $out variable to extract the name of the pool??? I need that NAME to do then:

send "ip dhcp pool NAME \r"
        expect "(dhcp-config)#"
        send "no dns-server\r"
        expect "(dhcp-config)#"
        send "dns-server 8.8.8.8 4.4.4.4\r"
        expect "(dhcp-config)#"
        send "exit\r"
        expect "(config)#"

and then continue with the script

thanks!