Simple EXPECT script to execute remote command and displat output

I am trying to connect to a network router and execute show status on it.
Currently i am using:
spawn ssh -o StrictHostKeyChecking=no [email protected]
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 [email protected]'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 ... ?


Try doing it like this

#!/usr/bin/expect -f
set timeout 120
spawn ssh -o StrictHostKeyChecking=no [email protected]
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.


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.