Windows-Server-2012-R2

為什麼 PSFTP 腳本在作為系統執行時會失敗?

  • November 22, 2021

我有一個 Windows cmd 批處理文件,它應該使用 PuTTY PSFTP exe 通過 sFTP 檢索 FTP 伺服器上的文件列表,並將其用於進一步處理。命令是:

echo ls | psftp -l myusername -pw mycomplexpwd FTPServerHostname > C:/Users/myuser/Desktop/ls.txt

它應該在ls.txt我在其他腳本中使用的文件中輸出列表。以普通使用者身份執行時,我得到了列表。但是,當使用任務計劃程序並將腳本安排為SYSTEM使用者時,我在輸出文件中只得到這個:

遠端工作目錄是 /

psftp>quit

我猜想管道使用有問題|或其他什麼,所以我也嘗試使用這種語法但也有相同的輸出:

psftp -l myusername -pw mycomplexpwd FTPServerHostname < C:/Users/myuser/Desktop/lscmd.txt > C:/Users/myuser/Desktop/ls.txt

將作業安排為 SYSTEM 而不是我的使用者時可能導致問題的原因是什麼?以及如何獲得 SYSTEM 列表?請注意,當以管理員身份執行腳本時(右鍵點擊 - 以管理員身份執行),我得到的列表很好,問題僅在使用任務計劃程序並將任務安排為 SYSTEM 時。

作業系統是 Windows Server 2012 R2。

因此,經過一番探勘,結果證明 PSFTP 無法辨識 ftp 伺服器的密鑰。我打開 cmd 為SYSTEM這可能有點棘手!)並通過 PSFTP 連接,接受信任主機(sFTP 伺服器),然後腳本按預期工作!它沒有給其他使用者帶來問題,因為他們在測試中使用並且更早地得到了提示!

僅供參考,密鑰也儲存在系統資料庫中HKEY_CURRENT_USER\Software\SimonTatham\PuTTY\SshHostKeys,因此如果您不想手動登錄,可以為其他使用者添加它(我添加了檢查我的腳本以添加它,如果缺少執行腳本的任何使用者)..

編輯:我相信我最大的問題是獲取 cmdSYSTEM以便我可以調試正在發生的事情,一旦我可以(請參閱上面的連結了解我是如何做到的),我就清楚了。為了參考和更清晰,下面是添加密鑰之前的命令輸出,只是屏蔽了敏感部分:

C:\Users\myuser>echo ls | psftp -l myftpuser -pw mycomplexpswd sftp_server
The server's host key is not cached. You have no guarantee
that the server is the computer you think it is.
The server's rsa2 key fingerprint is:
ssh-rsa 2048 SHA256: thecomplexfancyhostkey
If you trust this host, enter "y" to add the key to
PuTTY's cache and carry on connecting.
If you want to carry on connecting just once, without
adding the key to the cache, enter "n".
If you do not trust this host, press Return to abandon the
connection.
Store key in cache? (y/n, Return cancels connection, i for more info) 
Using username "myftpuser".
Pre-authentication banner message from server:
| Company FTP Login - Please enter valid credentials to continue
End of banner message from server
Keyboard-interactive authentication prompts from server:
End of keyboard-interactive prompts from server
Remote working directory is /
psftp> quit

引用自:https://serverfault.com/questions/1084015