Im writing a script to get a dump of the Procurve switch-settings

I start the script like this:

./script.sh | tee /home/user/telnet.log

Here the Script

#!/usr/bin/expect -f
set timeout 20
set  send_slow {5  .001}
set USERNAME "admin"
set PASSWORD "mypw"
spawn telnet 10.x.x.x
expect "Username: "
send "$USERNAME\r"
expect "Password:"
send "$PASSWORD\r"
expect "#"
send "show run\r"
send " "
send " "
send " "

The send " " is to let it scroll down to the next page so i can catch the whole settings.

It works so far but im not able to quit the telnet session with the script I tried to use send "exit \r" but it doesnt work. Anyone an idea how i could quit it?

Also when i dont use the interact the password doesnt seem to get through. The script doesnt work then. Whats the deal with that? I have tried to use sleep that didnt help aswell.

  • Try sending the command screen-length disable\r so it won't ask you all the MORE questions. – Mark Plotnick May 15 '18 at 14:22
  • While developing and expect script, add exp_internal 1 near the top to enable verbose debugging. Very useful to see why your patterns aren't matching. – glenn jackman May 15 '18 at 16:12
  • Also, a .sh extension for an expect script is misleading: use .exp – glenn jackman May 15 '18 at 16:13
  • with screen-length disable\r i get invalid command. the exp_internal 1 is a good tip thanks – batchn00b May 16 '18 at 6:32

In case you need press space more or less than 3 times, you would do something like

expect {
    "Press space" {    # or whatever the pager prompt is
        send " "
        exp_continue;  # this "loops" back to the expect statement
send "exit\r"
expect eof
  • This doesnt seem to work. It seems to get stuck after 1 or 2 spaces.To quit a "normal" connection to the Switch i have to write twice exit and confirm with yes. I tried to add another send exit and yes but didnt work either – batchn00b May 16 '18 at 6:27

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.