18

跑步:

my_machine~/opt/ams/data/ep/success$ expect -c "spawn /usr/bin/scp xmlEventLog_2010-03-22T14-28-36_PFS_1_2.xml adaptive@10.10.12.17:/opt/ams/epf_3_4/xmlEventLog_2010-03-22T14-28-36_PFS_1277900174_2.xml; expect { '*password:*' { send 'ad'\r\n }}"

似乎不起作用,因为我仍然被要求输入密码。

spawn /usr/bin/scp xmlEventLog_2010-03-22T14-28-36_PFS_1_2.xml adaptive@10.10.12.17:/opt/ams/epf_3_4/xmlEventLog_2010-03-22T14-28-36_PFS_1277900174_2.xml
adaptive@10.10.12.17's password: 

如果我将它作为脚本运行,它运行正常。

my_machine~/opt/ams/data/ep/success$ ./try.sh
spawn /usr/bin/scp xmlEventLog_2010-03-22T14-28-36_PFS_1_2.xml adaptive@10.10.12.17:/opt/ams/epf_3_4/xmlEventLog_2010-03-22T14-28-36_PFS_1277900174_2.xml
adaptive@10.10.12.17's password:
xmlEventLog_2010-03-22T14-28-36_PFS_1_2.xml                                                                      100%   13MB  13.2MB/s   00:01
my_machine~/opt/ams/data/ep/success$ cat try.sh
#!/bin/bash
expect -c "
        spawn /usr/bin/scp xmlEventLog_2010-03-22T14-28-36_PFS_1_2.xml adaptive@10.10.12.17:/opt/ams/epf_3_4/xmlEventLog_2010-03-22T14-28-36_PFS_1277900174_2.xml
        expect {
          "*password:*" { send "ad"\r\n; interact }
          eof { exit }
        }
        exit
        "

my_machine~/opt/ams/data/ep/success$

我想在一行命令而不是脚本中运行它。有没有人有任何想法?

提前致谢

一种

我在下面回答了我自己的问题

4

2 回答 2

15

明白了:以下代码在不输入密码的情况下将名为 Sean_Lilly.zip 的文件从我的盒子复制到另一个盒子:

expect -c "spawn /usr/bin/scp Sean_Lilly.zip adaptive@10.10.12.17:/opt/ams/epf_3_4/Sean_Lilly.zip; sleep 5; expect -re \"password\"; send \"ad\r\n\"; set timeout -1; expect -re \"100%\";"

我知道这可以通过在两个框之间设置无密码 ssh 访问来完成,但我想在一个命令行中使用 expect。感谢模糊棒棒糖的灵感。请注意,如果您运行 expect -d -c "spawn ... 您可以很好地调试正在发生的事情,包括您的正则表达式是否足够好

于 2010-07-29T20:40:59.747 回答
3

;在最后一个命令末尾的第一行示例中缺少一个。还有一种更好的方式来匹配密码。

尝试以下操作:

expect -c "spawn /usr/bin/scp xmlEventLog_2010-03-22T14-28-36_PFS_1_2.xml adaptive@10.10.12.17:/opt/ams/epf_3_4/xmlEventLog_2010-03-22T14-28-36_PFS_1277900174_2.xml; expect -re \".*password.*\"; send 'ad\r\n';"
于 2010-07-29T19:20:45.833 回答