Bash:expect + scp:多个文件的问题
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7487964/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Bash: expect + scp : Problem on multiple files
提问by Kit Ho
function expect_password {
expect -c "\
set timeout 90
set env(TERM)
spawn
expect \"password:\"
send \"$password\r\"
expect eof
"
}
expect_password "scp /home/kit.ho/folder/file1 root@$IP:/usr/bin"
The above expect_passwordworks perfect!
以上expect_password工作完美!
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 expectdoesn't recognize *. How can I transfer files in that way?
There is a possible answer using rsyncbut I can't use that.
好像expect不认识*。我怎样才能以这种方式传输文件?有一个可能的答案 usingrsync但我不能使用它。
回答by thiton
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 的联机帮助页说“如果由于 exec(2) 失败而无法成功生成程序”,因此我假设 expect 在内部使用 exec。exec 不会调用任何 shell 来进行通配符扩展和此类魔术,这意味着您的 ssh 会看到星号并且无法处理它。你有没有试过像
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 '
编辑:使用 \" 而不是 '
回答by glenn Hymanman
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
Expect 是 Tcl 的扩展,并且 Tcl 本身不会说 shell-filename-globbing。与其用你的框架硬塞一个 Tcl 解决方案,不如试试
set -- /home/kit.ho/folder/*
expect_password "scp $* root@$IP:/usr/bin"
Files with spaces won't work properly with this solution.
带有空格的文件将无法使用此解决方案正常工作。
回答by glglgl
Can't you leave away the password stuff completely and work with SSH public keys?
您不能完全放弃密码内容并使用 SSH 公钥吗?

