For Programmers: Free Programming Magazines  


Home > Archive > Tcl > November 2007 > Linux Expect script - spawn telnet error









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author Linux Expect script - spawn telnet error
mfreak

2007-11-12, 7:16 pm

I have a linux backup server that runs a big Except script that
telnets to switches, and has them send config files to the backup
server via TFTP. 10.10.0.1 is a router, 10.0.0.20 is my backup
server.The script has 25 or so of the following:

spawn telnet 10.10.0.1
expect "Login:"
send "admin\n"
expect "Password:"
send "mypasswd\n"
expect "Switch Prompt:"
send "save config file 10.0.0.20:10.10.0.1-config.cfg\n"
expect "Switch Prompt:"
send "exit\n"
close

Problem is, if a switch goes down, the spawn command errors, and the
rest of the script doesnt run. What can I do to make the script
continue at the next 'spawn telnet 10.10.x.x' section if it fails?
I'm a java programmer, ideally I'd like it to act like a try/catch if
that makes any sense. Thanks,

Cameron Laird

2007-11-12, 7:16 pm

In article <1194886709.304153.66540@22g2000hsm.googlegroups.com>,
mfreak <mfreak1171@yahoo.com> wrote:
>I have a linux backup server that runs a big Except script that
>telnets to switches, and has them send config files to the backup
>server via TFTP. 10.10.0.1 is a router, 10.0.0.20 is my backup
>server.The script has 25 or so of the following:
>
>spawn telnet 10.10.0.1
>expect "Login:"
>send "admin\n"
>expect "Password:"
>send "mypasswd\n"
>expect "Switch Prompt:"
>send "save config file 10.0.0.20:10.10.0.1-config.cfg\n"
>expect "Switch Prompt:"
>send "exit\n"
>close
>
>Problem is, if a switch goes down, the spawn command errors, and the
>rest of the script doesnt run. What can I do to make the script
>continue at the next 'spawn telnet 10.10.x.x' section if it fails?
>I'm a java programmer, ideally I'd like it to act like a try/catch if
>that makes any sense. Thanks,
>


set list_of_switches {10.10.0.1 ...}
set backup_server 10.0.0.20
foreach switch $list_of_switches {
if [catch {
spawn telnet $switch
expect "Login:"
send "admin\r"
expect "Password:"
send "mypasswd\r"
expect "Switch Prompt:"
send "save config file $backup_server:$switch-config.cfg\r"
expect "Switch Prompt:"
send "exit\r"
close
} result] {
puts "Switch $switch yielded error '$result'."
} else {
puts "Switch $switch did its duty uneventfully."
}
}

If I were writing it, I'd do it differently, but I think this
is the most risk-free way for you to move forward.
Glenn Jackman

2007-11-12, 7:16 pm

At 2007-11-12 12:37PM, "Cameron Laird" wrote:
> In article <1194886709.304153.66540@22g2000hsm.googlegroups.com>,
> mfreak <mfreak1171@yahoo.com> wrote:
[...][color=darkred]
> expect "Login:"
> send "admin\r"


Note Cameron's use of "\r" versus "\n". "\r" is the carriage return
character produced by the Enter key.

--
Glenn Jackman
"You can only be young once. But you can always be immature." -- Dave Barry
Cameron Laird

2007-11-12, 7:17 pm

In article <8kfm05-nht.ln1@lairds.us>, Cameron Laird <claird@lairds.us> wrote:
>In article <1194886709.304153.66540@22g2000hsm.googlegroups.com>,
>mfreak <mfreak1171@yahoo.com> wrote:
>
> set list_of_switches {10.10.0.1 ...}
> set backup_server 10.0.0.20
> foreach switch $list_of_switches {
> if [catch {
> spawn telnet $switch
> expect "Login:"
> send "admin\r"
> expect "Password:"
> send "mypasswd\r"
> expect "Switch Prompt:"
> send "save config file $backup_server:$switch-config.cfg\r"
> expect "Switch Prompt:"
> send "exit\r"
> close
> } result] {
> puts "Switch $switch yielded error '$result'."
> } else {
> puts "Switch $switch did its duty uneventfully."
> }
> }
>
>If I were writing it, I'd do it differently, but I think this
>is the most risk-free way for you to move forward.


Here's a more helpful suggestion:

set list_of_switches {10.10.0.1 password1
10.10.0.2 password2
...
}
set backup_server 10.0.0.20
foreach {switch password} $list_of_switches {
if [catch {
spawn telnet $switch
expect Login:
send admin\r
expect Password:
send $password\r
expect "Switch Prompt:"
send "save config file $backup_server:$switch-config.cfg\r"
expect "Switch Prompt:"
send exit\r
close
} result] {
puts "Switch $switch yielded error '$result'."
} else {
puts "Switch $switch did its duty uneventfully."
}
}
mfreak

2007-11-12, 7:17 pm

Excellent, thank you very much, that's exactly what I needed!

Also, Glenn, I beleive what a system interprets the carriage return as
is a function of the OS. The keyboard just sends the OS a scancode.
In this case '5A' when you press the enter key and 'FO5A' when you
release it. The OS determines if that's a CR, or a CR+LF, or
whatever else. That's why in the old unix days you'd be seeing ^M
after every line.

Glenn Jackman

2007-11-13, 7:17 pm

At 2007-11-12 04:02PM, "mfreak" wrote:
> Excellent, thank you very much, that's exactly what I needed!
>
> Also, Glenn, I beleive what a system interprets the carriage return as
> is a function of the OS. The keyboard just sends the OS a scancode.
> In this case '5A' when you press the enter key and 'FO5A' when you
> release it. The OS determines if that's a CR, or a CR+LF, or
> whatever else.


And then the C stdio library interprets it as \r (or so I understand).
At any rate, the Expect documentation is very clear: use \r where you
want to send a press of the enter key.


--
Glenn Jackman
"You can only be young once. But you can always be immature." -- Dave Barry
mfreak

2007-11-13, 7:17 pm

On Nov 13, 8:44 am, Glenn Jackman <gle...@ncf.ca> wrote:
> And then the C stdio library interprets it as \r (or so I understand).
> At any rate, the Expect documentation is very clear: use \r where you
> want to send a press of the enter key.


Ok, will do, thank you!

Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2009 codecomments.com