如何通过SFTP连接列出具有期望的目录
我有这个期望的脚本,但是文件列表不能正确地显示我。
期待。dat
set timeout 10000
spawn sftp -o ConnectTimeout=3 [email protected]
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 [email protected]
Password:
Connected to [email protected].
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
I have this expect script but the list of files does not present me correctly.
expect.dat
set timeout 10000
spawn sftp -o ConnectTimeout=3 [email protected]
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 [email protected]
Password:
Connected to [email protected].
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
首先,使用SFTP命令
LS -1 20220703*
在单列中产生输出。接下来,使用
log_user 0
和spawn -noecho sftp ...
隐藏所有多余的输出。最后,也是最棘手的部分,捕获
ls
输出,以便可以干净地打印出来:我们必须处理我们刚刚“键入”命令的事实。期望使用
\ r \ n
行结尾为您提供该过程的输出。First, use the sftp command
ls -1 20220703*
to produce output in a single column.Next, use
log_user 0
andspawn -noecho sftp ...
to hide all the excess output.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.expect gives you the process's output using
\r\n
line endings.