Search code examples
linuxsshexpectscpsocks

How to download file through linux expect?


I face a trouble about linux expect script. I have a computer A which only have LAN connection, while another computer B in the same LAN which have Internet connection. What I want to do is writing a script which can login into computer B through ssh and download file to it and finally use scp to transfer the file to computer A. And below is my script.

    #!/bin/expect
    set url [lindex $argv 0];
    spawn ssh "user@computer-B"
    expect "password:"
    send "passwd\n"
    expect "Last login:"
    send "cd tmp\n"
    send "wget $url\n"
    expect "saved"
    send "scp * user@computer-A:~/\n"
    expect {
    "yes/no" { send "yes\n"; exp_continue }
    "password:" { send "passwd\n" }
    }
    expect "100%"
    send "rm *\n"
    send "exit\n"

But now the script will scp the file to A immediately not until wget finishing. Is my script close to the proper way to do it? If not what should I do? Thanks very much.


Solution

  • You don't need expect, and you could use key-based login to make your life easier.

    SOCKS

    You could use a socks proxy.

    From computer A :

    ssh -D 1080 address-of-B

    followed by

    export SOCKS_SERVER=127.0.0.1:1080

    You can now use wget from computer A.

    One liner

    From computer A :

    ssh computer-B 'wget -O - $url' >> filename_on_computer_a