脚本sftp与期待

时间:2014-06-19 13:12:04

标签: sftp expect spawn

我必须使用password-authentification编写sftp-access脚本。不幸的是,我们的客户无法在我们必须访问的服务器上创建密钥验证。

生成所有sftp-Commands并将其写入临时文件(tmp.txt)。以下命令在命令行上正常工作:

sftp OnlineRegTest_BS@ftp.b-n-c.ch < tmp.txt

当我想使用&#34;期待&#34;因为我必须编写脚本并使用密码。

EXPECTSCRIPT=`cat <<EOF
    spawn sftp user@server < tmp.txt
    expect "password"
    send ${pass}
    send "\r" 
EOF`
expect -c "$EXPECTSCRIPT"

如果我删除&#34;&lt; tmp.txt&#34;连接将建立。但我需要来自tmp.txt的命令。这是错误消息:

[root@xxx web]# ./transfer.sh
spawn sftp user@server < tmp.txt
usage: sftp [-1Cv] [-B buffer_size] [-b batchfile] [-F ssh_config]
            [-o ssh_option] [-P sftp_server_path] [-R num_requests]
            [-S program] [-s subsystem | sftp_server] host
       sftp [[user@]host[:file [file]]]
       sftp [[user@]host[:dir[/]]]
       sftp -b batchfile [user@]host
send: spawn id exp6 not open
    while executing
"send password"

有人有任何想法吗?

我也无法使用sftp -b运行...: - (

编辑@glenn jackman

EXPECTSCRIPT2=`cat <<EOF
        spawn sh -c {sftp user@server < tmp.txt}
        expect "password"
        send ${pass}
        send "\r"
        expect "sftp>"
        expect "sftp>"
        .....
        expect "sftp>"
        expect "sftp>"
        expect "sftp>"
EOF`

tmp.txt看起来像这样:

cd d
put d/d1
cd ../e
put e/e1
put e/e2
cd ../f
put f/f1

1 个答案:

答案 0 :(得分:0)

尝试生成一个shell来处理重定向:

spawn sh -c {sftp user@example.com < tmp.txt}