如何通过SFTP连接列出具有期望的目录

发布于 2025-02-13 03:57:50 字数 1106 浏览 1 评论 0原文

我有这个期望的脚本,但是文件列表不能正确地显示我。

期待。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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

自控 2025-02-20 03:57:50

首先,使用SFTP命令LS -1 20220703*在单列中产生输出。

接下来,使用log_user 0spawn -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

期望使用\ r \ n行结尾为您提供该过程的输出。

First, use the sftp command ls -1 20220703* to produce output in a single column.

Next, use log_user 0 and spawn -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.

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文