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 ... ?

Phil Hollenback
  • 14,647
  • 4
  • 34
  • 51
s.mihai
  • 1,511
  • 6
  • 23
  • 27

2 Answers2

6

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.

user9517
  • 114,104
  • 20
  • 206
  • 289
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.

Phil Hollenback
  • 14,647
  • 4
  • 34
  • 51
  • 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 06:00
  • Ok... but does my suggestion to use autoexpect and/or `expect -d` help? – Phil Hollenback Jan 14 '11 at 06:29
  • i tried with expect -d and it worked... but no error message showed, after the sugestion from Iain to use set timeout it al works great now ! – s.mihai Jan 14 '11 at 13:47