-2

我有一个脚本应该列出 SFTP 服务器中的所有文件并将其输出到一个文件中(供另一个脚本使用)。产生所需输出的 ​​cmd 是:

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

这将由 Windows 任务计划程序每天调用几次。出于某种原因,在调度此命令(在 .bat 文件中)并通过 user 运行时SYSTEM,输出文件将仅包含以下内容:

Remote working directory is /
psftp> quit

当使用另一个用户时,输出是预期的(所有文件的列表) -请参阅https://serverfault.com/questions/1084015/why-psftp-script-is-failing-when-ran-as-system。我需要一种编写脚本的方法,并且能够SYSTEM像我在该系统中的其他脚本一样运行它。我也尝试过以下方法:

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

和:

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

其中 lscmd.txt 包含以下内容:

ls

并且行为是相同的。编辑:正如下面的 Martin 所指出的,下面可能没有生成相同的输出,而是没有触及文件。然而并没有产生预期的结果。

我能做些什么来做需要的行为?

操作系统是 Windows Server 2012 R2。

4

1 回答 1

-1

事实证明,在第一次连接中增加了信任 sFTP 服务器的步骤(或将密钥添加到HKEY_CURRENT_USER\Software\SimonTatham\PuTTY\SshHostKeys)。检查相关的服务器故障问题(稍微)了解我是如何做到的。echo ls | psftp -l myusername -pw mycomplexpwd FTPServerHostname > C:/Users/myuser/Desktop/ls.txt和其他变体(例如)之后都-b起作用了。

编辑:我相信我最大的问题是获取 cmdSYSTEM以便我可以调试正在发生的事情,一旦我可以(请参阅姊妹服务器故障问题了解我是如何做到的),我就清楚了。为了参考和更好的清晰度(因为我没有解释就被否决了!),下面是添加密钥之前的命令输出,只是掩盖了敏感部分,表明我确实收到Remote working directory is /了输出文件中的消息:

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
于 2021-11-20T19:09:08.513 回答