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
Related videos on Youtube
Comments
-
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 about 9 yearsI 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 about 9 yearsBut thanks for the answer.
-
Eric Renouf about 9 yearsYou 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' about 9 yearsActually, I think the problem now is that the OP needs
"
after/export/home/rgh/
; i.e., at the end of thescp
command. -
Tomas Hromnik about 9 yearsDid'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 about 9 years@G-Man is right, you want the
sh -c
to only be quoting the scp command itself, the rest areexpect
commands, notsh
ones -
Eric Renouf about 9 yearsI 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 about 9 yearsyes, 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'`