Question

My code :

spawn telnet $ip
expect "Password: "
exp_send "$paswd\r"
expect "Router>"
exp_send "en\r"
expect "Password: "
exp_send "$paswd\r"
expect "Router#"
exp_send "\r"
expect "Router#"
exp_send "\r"
expect "Router#"
exp_send "\r"
expect "Router#"
exp_send "\r"
expect "Router#"
exp_send "conf t\r"
expect "Router(config)#"
exp_send "\r"
expect "Router(config)#"
exp_send "ip ftp username name\r"
expect "Router(config)#"
exp_send "ip ftp password name123\r"
expect "Router(config)#"
exp_send "end"
expect "Router#"
exp_send "\r"
expect "Router#"
exp_send "\r"
expect "Router#"
exp_send "\r"
expect "Router#"
exp_send "copy ftp: filename\r"
expect "Address or name of remote host []? "
exp_send "$remote_ip\r"
expect "Source filename []? "
exp_send "router-config1\r"
expect "Destination filename []? "
exp_send "/new/location\r"
expect "Router#"

output:

Router(config)#ip ftp username name
Router(config)#ip ftp password name123
Router(config)#endTcpping succeeded for port 443.
Tcpping succeeded for port 443.

Router#
Router#
Router#copy ftp: filename
Address or name of remote host []? Tcpping succeeded for port 443.
Tcpping succeeded for port 443.
$remote_ip
Source filename []? Tcpping succeeded for port 443.
Tcpping succeeded for port 443.
filename
Destination filename [filename]? Tcpping succeeded for port 443.
Tcpping succeeded for port 443.
/new/location
%Warning:There is a file already existing with this name
Do you want to over write? [confirm]Tcpping succeeded for port 443.
Tcpping succeeded for port 443.

Accessing ftp://$remote_ip//filename...
%Error opening ftp://$remote_ip//router-config (No such file or directory)
Router#copy t1_working startup-config
Destination filename [startup-config]? Tcpping succeeded for port 443.

can someone tell me how to catch the exception for NO SUCH FILENAME ?

Was it helpful?

Solution

You can make Expect wait for two (or more) different strings at once.

expect {
    "Thing for success" {
         # little script to handle the success case
         puts "hooray, going to do next step"
    }
    "Thing for failure 1" {
         puts "oh dear; didn't work"
         exit 1
    }
    "Thing for failure 2" {
         send "something to try to recover\r"
         exp_continue; # Try this expect again
    }
}

Of course, you'll need to pick appropriate things to match against (check out using glob or regex matching for this) and you'll need to work out how to actually handle the error cases (your choices are usually variations on “ignore”, “fail out” and “try to recover”). Also consider putting in explicit handling of timeouts.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top