Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

connecting to switch via ssh using expect

Tags:

ssh

expect

I'm trying to run an script to connect to a Procurve 4204vl switch using expect. This is the code I created:

#!/usr/bin/expect -f
set timeout 20
spawn ssh -l user 192.168.0.10 
expect "[email protected]'s password:"
send "1234"
send "\r"
expect "Press any key to continue" 
send "j\r"
send "conf"
send "\r"
send "no ip route 89.19.238.2 255.255.255.255 192.168.0.12"
send "\r"
send "exit"
send "\r"
send "exit"
send "\r"
send "exit"
send "\r"
expect "Do you want to log out [y/n]?"
send "y"

I run this using simply expect script.exp, and the problem is I got these errors:

  1. the route is not deleted
  2. I got the following error on screen after the script execution is finished:

    Press any key to continue invalid command name "y/n" while executing "y/n" invoked from within "expect "Do you want to log out [y/n]?"" (file "script.exp" line 19)

So, how could I solve this problem? Thank you.

PS: if I comment all the "exit" lines and also the log out question, then add a last line with the "interact" command, the script works fine.

like image 915
Argie Avatar asked Sep 02 '25 10:09

Argie


1 Answers

For route not deleted, what output is the program giving you? Do you see any errors from the router?

In expect and Tcl the square brackets are the syntax to execute a command, quite like backticks in the shell. The simplest solution is to use braces instead of double quotes to prevent command interpolation:

expect {Do you want to log out [y/n]?}

Braces act like single quotes in the shell.

like image 165
glenn jackman Avatar answered Sep 04 '25 03:09

glenn jackman