cancel
Showing results forĀ 
Search instead forĀ 
Did you mean:Ā 
cancel
6699
Views
0
Helpful
5
Replies

ssh expect script to login cisco devices from ucs

raindrop18
Level 1
Level 1

I have  ssh expect script using to login cisco devices called be another cgi-script. for long time I have used and working perfectly running on Solaris server. recently I have moved my script to UCS server running RedHut Linux and I have a problem with my script. my question is if any one out there using expect script to log cisco devices have any suggestion on my script highly appreciated. or if there bother forum within Netpro related to my question please let me know. thanks.  the error I got is - "Password: Error reading prompt -> Password:

and my script is

log_user 0  

set timeout 20

set userid  "xxxxx" 

set password  "xxxxxxxx" 

set device  [lindex $argv 0]  

set command [lindex $argv 1] 

spawn /usr/bin/ssh -l $userid $device 

match_max [expr 32 * 1024] 

expect {       -re "RSA key fingerprint" {send "yes\r"}     

timeout {puts "Host is known"} }

expect {        -re "username: " {send "$userid\r"}       

-re "(P|p)assword: " {send "$password\r"}     

  -re "Warning:" {send "$password\r"}     

-re "Connection refused" {puts "Host error -> $expect_out(buffer)";exit}     

-re "Connection closed"  {puts "Host error -> $expect_out(buffer)";exit}    

-re "no address.*" {puts "Host error -> $expect_out(buffer)";exit}     

timeout {puts "Timeout error. Is device down or unreachable?? ssh_expect";exit}

}

expect {      -re "\[#>]$" {send "term len 0\r"}   

timeout {puts "Error reading prompt -> $expect_out(buffer)";exit} 

}

expect {     -re "\[#>]$" {send "$command\r"}    

timeout {puts "Error reading prompt -> $expect_out(buffer)";exit} 

}  

-re "\[#>]$"   set output $expect_out(buffer) 

puts "$output\r\n" 

send "exit\r"

5 Replies 5

Joe Clarke
Cisco Employee
Cisco Employee

The script looks okay.  What may be happening here is the password is wrong.  When the script sends the wrong password the first time, it gets another password prompt.  Another thing you can try is to remove the "Warning:" regular expression.

Thanks Joe, I have removed "warning" regular expression and the script able to log to Cisco devices. the issue I have now is it take 30 second to log to one devices which is too slow. normally takes 3-5 second. I have checked the network actually the USC is on 10 Gig interface much fater than the old physical server which was inly 100M. so I bthink something going on with the script. actually at the end of the log say " write() failed to write anything -will sleep(1) and retry... so appears to be the script to write something and failed and waiting untill time out. I think that may cause delay but I couldn't figure out what's try to write. I have attached the expect log. please let me know if you able see anthing there. thanks again.

Try this:

log_user 0 

set timeout 20

set userid  "xxxxx"

set password  "xxxxxxxx"

set device  [lindex $argv 0] 

set command [lindex $argv 1]

spawn /usr/bin/ssh -l $userid $device

match_max [expr 32 * 1024]

while { 1 } {

expect {       -re "RSA key fingerprint" {send "yes\r"}    

-re "username: " {send "$userid\r"}      

-re "(P|p)assword: " {send "$password\r"}    

-re "Connection refused" {puts "Host error -> $expect_out(buffer)";exit}    

-re "Connection closed"  {puts "Host error -> $expect_out(buffer)";exit}   

-re "no address.*" {puts "Host error -> $expect_out(buffer)";exit} 

-re "\[#>]$" {break}  

timeout {puts "Timeout error. Is device down or unreachable?? ssh_expect";exit}

}

}

send "term len 0\r"

expect {     -re "\[#>]$" {send "$command\r"}   

timeout {puts "Error reading prompt -> $expect_out(buffer)";exit}

-re "\[#>]$"   set output $expect_out(buffer)

puts "$output\r\n"

send "exit\r"

with this one I am only able to get the banner on "sh ver" command and the error is

" invalid command name "-re"   

while executing "-re "\[#>]$" set output $expect_out(buffer)"  

  (file "/opt/apache/cgi-bin/ssh_expect" line 41)

write() failed to write anything - will sleep(1) and retry...

write() failed to write anything - will sleep(1) and retry... 

I have attached the full log.  thanks

I used your original example.  The part giving the error was not modified.  This version should work, but I'm not sure your working version looks like.

log_user 0

set timeout 20

set userid  "xxxxx"

set password  "xxxxxxxx"

set device  [lindex $argv 0]

set command [lindex $argv 1]

spawn /usr/bin/ssh -l $userid $device

match_max [expr 32 * 1024]

while { 1 } {

expect {       -re "RSA key fingerprint" {send "yes\r"}   

-re "username: " {send "$userid\r"}     

-re "(P|p)assword: " {send "$password\r"}   

-re "Connection refused" {puts "Host error -> $expect_out(buffer)";exit}   

-re "Connection closed"  {puts "Host error -> $expect_out(buffer)";exit}  

-re "no address.*" {puts "Host error -> $expect_out(buffer)";exit}

-re "\[#>]$" {break} 

timeout {puts "Timeout error. Is device down or unreachable?? ssh_expect";exit}

}

}

send "term len 0\r"

expect {     -re "\[#>]$" {send "$command\r"}  

timeout {puts "Error reading prompt -> $expect_out(buffer)";exit}

}

expect -re "\[#>]$" 

set output $expect_out(buffer)

puts "$output\r\n"

send "exit\r"