Simple EXPECT script to execute remote command and displat output

57,971

Solution 1

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.

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

Share:
57,971

Related videos on Youtube

Wegged
Author by

Wegged

Sysadmin of a small network ~30pc's 2 servers. I like IT, and i'm doing this as an extra responsability at my job. Keeping IT in the company works great for us :)

Updated on September 17, 2022

Comments

  • Wegged
    Wegged over 1 year

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

  • Wegged
    Wegged over 13 years
    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)
  • Phil Hollenback
    Phil Hollenback over 13 years
    Ok... but does my suggestion to use autoexpect and/or expect -d help?
  • Wegged
    Wegged over 13 years
    that was it. the "set timeout 120" :) thank you so much !
  • Wegged
    Wegged over 13 years
    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 !