如何让期望-c在单行而不是脚本中工作

ama*_*ain 14 shell command-line scp expect

运行:

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 }}"
Run Code Online (Sandbox Code Playgroud)

似乎没有工作,因为我仍然被要求输入密码.

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: 
Run Code Online (Sandbox Code Playgroud)

如果我以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'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$
Run Code Online (Sandbox Code Playgroud)

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

提前致谢

一个

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

ama*_*ain 11

得到它:以下代码将我的框中名为Sean_Lilly.zip的文件scps到另一个框而不输入密码:

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%\";"
Run Code Online (Sandbox Code Playgroud)

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

  • 好的。在 OS X 上,将“\r\n”替换为“\r”。 (2认同)