3

I am trying to connect to a network router and execute show status on it.
Currently i am using:
spawn ssh -o StrictHostKeyChecking=no admin@192.168.20.254
expect " * ? password:*\r"
send -- "secretPassword\r"
sleep 5
send -- "show status\r"
sleep 10
send -- "exit\r"

It dosen't work, i get stuck at admin@192.168.20.254's password:i've tried entering the password but it does not work, i get:
server1:~# secretPassword
-bash: server1: command not found
server1:~#


What am i doing so wrong here ... ?

1
  • I think you're just missing a suitable timeout see the edit to my answer.
    – user9517
    Jan 14 '11 at 11:28
5

Try doing it like this

#!/usr/bin/expect -f
set timeout 120
spawn ssh -o StrictHostKeyChecking=no admin@192.168.20.254
expect "*?assword:*"
send -- "secretPassword\r"
sleep 5
send -- "show status\r"
sleep 10
send -- "exit\r"
expect eof

If your device is slow to respond you probably need to set a suitable timeout.

1
  • that was it. the "set timeout 120" :) thank you so much !
    – s.mihai
    Jan 14 '11 at 13:45
2

First you should look at automating the whole process of collecting and tracking router information using RANCID instead of doing a one off solution.

For this particular issue, take a look at autoexpect to automate the creation of your expect script. That should give you a working expect script to start from. To fix your existing script, try running expect with the -d argument. That will show you exactly what expect is looking to match, and should hopefully tell you what is wrong in your match expression.

3
  • RANCID is not up to the job as it only monitors current configuration and i want to get some information regarding current bandwidth usage with this and then save it into a dv. (i can not use SNMP)
    – s.mihai
    Jan 14 '11 at 6:00
  • Ok... but does my suggestion to use autoexpect and/or expect -d help? Jan 14 '11 at 6:29
  • i tried with <code>expect -d</code> and it worked... but no error message showed, after the sugestion from Iain to use <code>set timeout</code> it al works great now !
    – s.mihai
    Jan 14 '11 at 13:47

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.