[英]How to list a directory with expect via sftp connection
I have this expect script but the list of files does not present me correctly.我有这个期望脚本,但文件列表没有正确显示我。
expect.dat期望.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 command期望命令
expect expect.dat
Result结果
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
how to get this result?如何得到这个结果?
20220703_A.log
20220703_A.xls
20220703_E.log
20220703_E_r.log
First, use the sftp command ls -1 20220703*
to produce output in a single column.首先,使用 sftp 命令
ls -1 20220703*
在单列中生成输出。
Next, use log_user 0
and spawn -noecho sftp ...
to hide all the excess output.接下来,使用
log_user 0
和spawn -noecho sftp ...
隐藏所有多余的输出。
Last, and the trickiest part, capture the ls
output so that can be printed out cleanly: we have to handle the fact that the command we just "typed" will be given back to us.最后也是最棘手的部分,捕获
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 gives you the process's output using \r\n
line endings. expect 使用
\r\n
行结尾为您提供进程的输出。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.