如何让 Expect -c 在单行而不是脚本中工作
运行:
my_machine~/opt/ams/data/ep/success$ expect -c "spawn /usr/bin/scp xmlEventLog_2010-03-22T14-28-36_PFS_1_2.xml [email protected]:/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 [email protected]:/opt/ams/epf_3_4/xmlEventLog_2010-03-22T14-28-36_PFS_1277900174_2.xml
[email protected]'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 [email protected]:/opt/ams/epf_3_4/xmlEventLog_2010-03-22T14-28-36_PFS_1277900174_2.xml
[email protected]'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 [email protected]:/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$
我想在一行命令而不是脚本中运行它。有人有什么想法吗?
预先感谢
A
我在下面回答了我自己的问题
Running:
my_machine~/opt/ams/data/ep/success$ expect -c "spawn /usr/bin/scp xmlEventLog_2010-03-22T14-28-36_PFS_1_2.xml [email protected]:/opt/ams/epf_3_4/xmlEventLog_2010-03-22T14-28-36_PFS_1277900174_2.xml; expect { '*password:*' { send 'ad'\r\n }}"
Does not seem to work as I am still asked for the password.
spawn /usr/bin/scp xmlEventLog_2010-03-22T14-28-36_PFS_1_2.xml [email protected]:/opt/ams/epf_3_4/xmlEventLog_2010-03-22T14-28-36_PFS_1277900174_2.xml
[email protected]'s password:
If I run it as ascript it runs ok.
my_machine~/opt/ams/data/ep/success$ ./try.sh
spawn /usr/bin/scp xmlEventLog_2010-03-22T14-28-36_PFS_1_2.xml [email protected]:/opt/ams/epf_3_4/xmlEventLog_2010-03-22T14-28-36_PFS_1277900174_2.xml
[email protected]'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 [email protected]:/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$
I would like to run this in a one line command rather than a script. Has anyone got any ideas?
Thanks in advance
A
I answered my own question below
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
知道了:
以下代码将一个名为 Sean_Lilly.zip 的文件从我的盒子传输到另一个盒子,无需输入密码:
我知道这可以通过在两个盒子之间设置无密码 ssh 访问来完成,但我想使用 Expect 在一个命令行中完成此操作。感谢模糊棒棒糖的灵感。请注意,如果您运行 Expect -d -c "spawn ...,您将获得有关正在发生的情况的出色调试,包括您的正则表达式是否足够好
Got it:
The following code scps a file called Sean_Lilly.zip from my box to another box without entering a password:
I know this can be done by setting passwordless ssh access between the two boxes but I wanted to do it in one command line using expect. Thanks fuzzy lollipop for the inspiration. Note if you run expect -d -c "spawn ... you get excellent debug on what is happening including whether your regex is good enough
您在最后一个命令末尾的第一行示例中缺少
;
。还有一种更好的方法来模式匹配密码。尝试以下操作:
You are missing a
;
on the first one line example at the end of the last command. And there is a better way to pattern match the password.try the following: