簡體   English   中英

如何通過 sftp 連接列出帶有期望的目錄

[英]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 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

expect 使用\r\n行結尾為您提供進程的輸出。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM