您好我想通过自动SFTP脚本将目录中的文件传输到远程服务器。我试过下面的代码。但它没有用。
#! /bin/bash
i=0
for file in *; do
array[ $i ]="$file"
(( i++ ))
done
expect <<EOF
spawn sftp user1@10.121.121.121
expect "user1@10.121.121.121's password:"
send "password\n"
expect "sftp>"
send "cd /user/KBACS/test\n"
expect {
"sftp>" {
for {set j 0} {$j < $i} {incr j 1} {
send "put ${array[j]}\n";
}
send "bye\n";
}
EOF
}
EOF
我可以登录远程服务器。之后就会出现这样的错误。
sftp> cd /user/KBACS/test
sftp> missing operand at _@_
in expression " _@_< 8"
(parsing expression " < 8")
invoked from within
"for {set j 0} { < 8} {incr j 1} {
send "put 20171108.log\n";
}"
invoked from within
"expect {
"sftp>" {
for {set j 0} { < 8} {incr j 1} {
send "put 20171108.log\n";
}
send "bye\n";
}
EOF
}"
请帮我解决这个问题或建议任何替代解决方案。提前谢谢。
答案 0 :(得分:1)
我建议您设置ssh密钥,这样您就不需要提供密码,然后在批处理模式下使用sftp
{ printf "put %s\n" *; echo bye; } | sftp -b - user@host
如果这不可行,那么你不需要结合expect和bash:expect(和Tcl)可以找出当前目录中的文件:glob
#!/usr/bin/expect -f
spawn sftp user1@10.121.121.121
expect "user1@10.121.121.121's password:"
send "password\r"
expect "sftp>"
send "cd /user/KBACS/test\r"
expect "sftp>"
foreach filename [glob *] {
put "$filename\r"
expect "sftp>"
}
send "bye\r"
expect eof