Kit Ho
Kit Ho

Reputation: 26998

Bash: expect + scp : Problem on multiple files

function expect_password {
    expect -c "\
    set timeout 90
    set env(TERM)
    spawn $1
    expect \"password:\"
    send \"$password\r\"
    expect eof
  "
}

expect_password "scp /home/kit.ho/folder/file1 root@$IP:/usr/bin"

The above expect_password works perfect!

However, I want to transfer multiple files in that directory, so I tried:

expect_password "scp /home/kit.ho/folder/* root@$IP:/usr/bin"

But an error comes up:

/home/kit.ho/folder/*: No such file or directory
Killed by signal 1.

It seems that expect doesn't recognize *. How can I transfer files in that way? There is a possible answer using rsync but I can't use that.

Upvotes: 2

Views: 9322

Answers (3)

glenn jackman
glenn jackman

Reputation: 247082

Expect is an extension of Tcl, and Tcl does not speak shell-filename-globbing natively. Rather than shoe-horning a Tcl solution withing your framework, try

set -- /home/kit.ho/folder/* 
expect_password "scp $* root@$IP:/usr/bin"

Files with spaces won't work properly with this solution.

Upvotes: 1

thiton
thiton

Reputation: 36059

The manpage of expect says "If program cannot be spawned successfully because exec(2) fails", so I assume that expect uses exec internally. exec doesn't call any shell to do wildcard expansion and such magic, which means that your ssh sees the asterisk and can't handle it. Have you tried to call your shell explicitely like

expect_password "sh -c \"scp /home/kit.ho/folder/* root@$IP:/usr/bin\""

(maybe you need to omit the single quotes)?

edit: use \" instead of '

Upvotes: 4

glglgl
glglgl

Reputation: 91119

Can't you leave away the password stuff completely and work with SSH public keys?

Upvotes: 0

Related Questions