bashsshexpectbusyboxdd-wrt

How to pass commands through ssh to dd-wrt with a loop using a variable from a text file?


So far I have been able to create a small script using ssh combined with expect to pass a single command through to the dd-wrt router that I am working with. Now that this has been accomplished I wish to pass the same command several times through the ssh log-in instead of just one from a text file, if it is possible.

The other way to accomplish this would be to create a loop and pass the command over, and over again. I would have to use a variable though because the data for the command in the text file changes.

Here is what I have so far

#!/bin/expect -f

set password password

    spawn ssh -l root x.x.x.x -p "command"
    expect "*password:*"
    send -- "$password\r"
    send -- "\r"

From what I can see creating a loop would be the easiest way, but I may be wrong. NOTE that the "command & variables" that I want to pass through are in a separate text file, and that it needs to read/take each line and insert each one into the loop. Unless there is a way to send them through all at once.

#!/bin/expect -f

set password password

    spawn ssh -l root x.x.x.x -p "command Variable" <-- Command to be passed through
    expect "*password:*"
    send -- "$password\r"
    send -- "\r"

It is the same command every time in the text file, only the variable changes.

test.txt

command xxxxxxx
command xxxxxxx
command xxxxxxx
command xxxxxxx

Thank-you


Solution

  • I think you should do something like this.

    start.sh

    #!/bin/bash
     password="your_password"
     cat test.txt|while read line
     do
       for i in $line
         do
           ssh.exp $i $password
         done
     done
    

    ssh.exp

    #!/usr/bin/expect
    set command [lrange $argv 0 0] 
    set password [lrange $argv 1 1] 
    spawn ssh -l root x.x.x.x -p "$command"
    expect "*password:*"
    send -- "$password\r"
    send -- "\r"
    

    And test.txt with list of your commands. Each on the different line.