scp command behaves differently when used with expect utility

5,720

Solution 1

I think the ~ and * are expanded by the shell, but I bet expect invokes scp directly, bypassing the shell so those don't get expanded. You could try spawning sh -c the scp command.

If it's an option, it might also just be easier to share your key with the server though so you don't need expect for this at all.

Using the sh technique the command will end up looking like:

expect -c 'spawn sh -c "scp -C -o CompressionLevel=9 ~/partFiles/* [email protected]:/export/home/abc/"; sleep 10; expect password; send "secretPassword\n";interact'

Solution 2

An alternative to Eric's good answer: Tcl can do glob expansion

expect <<'END_EXPECT'
    set timeout -1      # use this instead of sleep
    set files [glob -nocomplain ~/partFiles/*]
    if {[llength $files]} {
        spawn scp -C -o CompressionLevel=9 {*}$files [email protected]:/export/home/abc/
        expect password
        send "secretPassword\r"
        expect eof
    }
END_EXPECT
Share:
5,720

Related videos on Youtube

Tomas Hromnik
Author by

Tomas Hromnik

Air of mystery

Updated on September 18, 2022

Comments

  • Tomas Hromnik
    Tomas Hromnik almost 2 years
    expect -c 'spawn scp -C -o CompressionLevel=9 ~/partFiles/* [email protected]:/export/home/abc/; sleep 10; expect password; send "secretPassword\n";interact'
    

    throws - ~/partFiles/*: No such file or directory

    Just

    scp -C -o CompressionLevel=9 ~/partFiles/* [email protected]:/export/home/abc/

    however, works perfectly.

    Why? How to fix this?

  • Tomas Hromnik
    Tomas Hromnik about 9 years
    I didn't get it. What did you mean by You could try spawning sh -c the scp command. I am a little new to all this. Would you kindly explain?
  • Tomas Hromnik
    Tomas Hromnik about 9 years
    But thanks for the answer.
  • Eric Renouf
    Eric Renouf about 9 years
    You have quotes around the password, which close the quotes for the sh -c You'll need to escape those to use this solution
  • G-Man Says 'Reinstate Monica'
    G-Man Says 'Reinstate Monica' about 9 years
    Actually, I think the problem now is that the OP needs " after /export/home/rgh/; i.e., at the end of the scp command.
  • Tomas Hromnik
    Tomas Hromnik about 9 years
    Did't work. I tried ` expect -c 'spawn sh -c "scp -C -o CompressionLevel=9 ~/partFiles/* [email protected]:/export/home/rgh/; sleep 10; expect password; send \"password\n\";interact"'`
  • Eric Renouf
    Eric Renouf about 9 years
    @G-Man is right, you want the sh -c to only be quoting the scp command itself, the rest are expect commands, not sh ones
  • Eric Renouf
    Eric Renouf about 9 years
    I just updated my answer with that command line too, glad it worked, and thanks again to @G-Man for that last push for the quoting help
  • Tomas Hromnik
    Tomas Hromnik about 9 years
    yes, thanks a lot. The correct command should be: ` expect -c 'spawn sh -c "scp -C -o CompressionLevel=9 ~/partFiles/* [email protected]:/export/home/abc/"; sleep 10; expect password; send "passowrd\n";interact'`