[英]How to list a directory with expect via sftp connection
我有这个期望脚本,但文件列表没有正确显示我。
期望.dat
set timeout 10000
spawn sftp -o ConnectTimeout=3 user@192.1.6.6
expect "*?assword*"
send "tech\r"
expect "sftp>"
send "cd /global/scripts/log\r"
expect "sftp>"
send "ls 20220703*\r"
expect "sftp>"
send "bye\r"
期望命令
expect expect.dat
结果
spawn sftp -o ConnectTimeout=3 user@192.1.6.6
Password:
Connected to user@192.1.6.6.
sftp> cd /global/scripts/log
sftp> ls 20220703*
20220703_A.log 20220703_A.xls
20220703_E.log 20220703_E_r.log
sftp> You have new mail in /var/spool/mail/dm
如何得到这个结果?
20220703_A.log
20220703_A.xls
20220703_E.log
20220703_E_r.log
首先,使用 sftp 命令ls -1 20220703*
在单列中生成输出。
接下来,使用log_user 0
和spawn -noecho sftp ...
隐藏所有多余的输出。
最后也是最棘手的部分,捕获ls
输出以便可以干净地打印出来:我们必须处理这样一个事实,即我们刚刚“键入”的命令将返回给我们。
set ls_cmd "ls -1 20220703*"
send "$ls_cmd\r"
# capture the command's output
expect -re "$ls_cmd\r\n(.*)\r\nsftp>"
puts $expect_out(1,string)
send "bye\r"
expect eof # wait for sftp to exit cleanly
expect 使用\r\n
行结尾为您提供进程的输出。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.