如何得到期望-c工作在单行而不是脚本 [英] How to get expect -c to work in single line rather than script
问题描述
正在运行:
my_machine〜/ opt / ams / data / ep / success $ expect -cspawn / 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\\\
}}
似乎无法正常工作,因为我仍需要输入密码。
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的密码:
如果我以ascript它运行确定。
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的密码:
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:*{sendad\r\\\
; interact}
eof {exit}
}
exit
my_machine〜/ opt / ams / data / ep / success $
我想在一行命令而不是脚本中运行它。 >
提前感谢
A
下面的问题
结果:
下面的代码将一个名为Sean_Lilly.zip的文件从我的框中移到另一个框无需输入密码:
expect -cspawn / usr / bin / scp Sean_Lilly.zip adaptive@10.10.12.17: opt / ams / epf_3_4 / Sean_Lilly.zip;睡眠5; expect -re \password\;发送\ad\r\\\
\;设置超时-1; expect -re \100%\;
我知道这可以通过设置无密码ssh访问两个框之间,但我想在一个命令行使用expect。感谢模糊棒棒糖的灵感注意如果你运行expect -d -cspawn ...你得到很好的调试,发生了什么,包括你的正则表达式是否够好
I was wondering if anyone had any examples of how to use expect with -c.
Running:
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 }}"
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 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:
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 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$
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
Got it: The following code scps a file called Sean_Lilly.zip from my box to another box without entering a password:
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%\";"
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
这篇关于如何得到期望-c工作在单行而不是脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!