通过向 JScript 中的 StdIn 提供命令来编写 WinSCP 脚本 - 脚本挂起并且永远不会完成
WinSCP scripting by feeding commands to StdIn in JScript - script hangs and never finishes
我正在尝试从 SFTP 服务器下载大约 40 个文件,但它只下载了其中的 30 个文件并挂起。
请帮助解决问题。
这是我正在使用的脚本。
var exec = shell.Exec("winscp.com /xmllog=\"C:\download\log\download.xml\" ");
exec.StdIn.Write(
"option batch abort\n"+
"option confirm off\n"+
"open sftp://xxx:xxx@xxx -hostkey=\"xxxx\" \n"+
"get /download/* \"C:\download\\" \n"+
"exit");
请看下面的日志。
我无法在日志文件中找到任何问题:
> 2017-10-18 13:09:10.232 Type: SSH_FXP_REMOVE, Size: 54, Number: 99341
< 2017-10-18 13:09:10.233 Type: SSH_FXP_STATUS, Size: 24, Number: 99076
. 2017-10-18 13:09:10.233 Discarding reserved response
< 2017-10-18 13:09:10.235 Type: SSH_FXP_STATUS, Size: 24, Number: 99341
< 2017-10-18 13:09:10.235 Status code: 0
. 2017-10-18 13:09:10.236 File: '/compliance/2016_QTR2_DepreciationSummary.xlsx' [2017-10-18T17:07:08.000Z] [19819]
. 2017-10-18 13:09:10.236 Copying "/compliance/2016_QTR2_DepreciationSummary.xlsx" to local directory started.
. 2017-10-18 13:09:10.236 Binary transfer mode selected.
. 2017-10-18 13:09:10.236 Opening remote file.
> 2017-10-18 13:09:10.236 Type: SSH_FXP_OPEN, Size: 63, Number: 99587
< 2017-10-18 13:09:10.239 Type: SSH_FXP_HANDLE, Size: 13, Number: 99587
> 2017-10-18 13:09:10.239 Type: SSH_FXP_FSTAT, Size: 13, Number: 99848
< 2017-10-18 13:09:10.241 Type: SSH_FXP_ATTRS, Size: 37, Number: 99848
> 2017-10-18 13:09:10.242 Type: SSH_FXP_READ, Size: 25, Number: 100101
< 2017-10-18 13:09:10.250 Status code: 1
. 2017-10-18 13:09:10.250 3 skipped SSH_FXP_WRITE, SSH_FXP_READ, SSH_FXP_DATA and SSH_FXP_STATUS packets.
> 2017-10-18 13:09:10.250 Type: SSH_FXP_CLOSE, Size: 13, Number: 100612
. 2017-10-18 13:09:10.250 Preserving timestamp [2017-10-18T17:07:08.000Z]
. 2017-10-18 13:09:10.251 Transfer done: '/compliance/2016_QTR2_DepreciationSummary.xlsx' => 'C:\Users\Arman\Desktop\Work\pwc\final\servers\comerit_final_download_from_sftp\src\main\webapp\compliance16_QTR2_DepreciationSummary.xlsx' [19819]
. 2017-10-18 13:09:10.251 Deleting file "/compliance/2016_QTR2_DepreciationSummary.xlsx".
> 2017-10-18 13:09:10.251 Type: SSH_FXP_REMOVE, Size: 55, Number: 100877
< 2017-10-18 13:09:10.253 Type: SSH_FXP_STATUS, Size: 24, Number: 100612
. 2017-10-18 13:09:10.253 Discarding reserved response
< 2017-10-18 13:09:10.256 Type: SSH_FXP_STATUS, Size: 24, Number: 100877
< 2017-10-18 13:09:10.256 Status code: 0
. 2017-10-18 13:09:10.256 File: '/compliance/2016_QTR2_GainLossSummary.pdf' [2017-10-18T17:07:08.000Z] [120260]
< 2017-10-18 13:09:37.004 Script: Terminated by user.
. 2017-10-18 13:09:37.004 Copying "/compliance/2016_QTR2_GainLossSummary.pdf" to local directory started.
. 2017-10-18 13:09:37.004 Binary transfer mode selected.
. 2017-10-18 13:09:37.004 Checking existence of partially transferred file.
. 2017-10-18 13:09:37.005 Opening remote file.
> 2017-10-18 13:09:37.005 Type: SSH_FXP_OPEN, Size: 58, Number: 101123
< 2017-10-18 13:09:37.009 Type: SSH_FXP_HANDLE, Size: 13, Number: 101123
> 2017-10-18 13:09:37.009 Type: SSH_FXP_FSTAT, Size: 13, Number: 101384
< 2017-10-18 13:09:37.012 Type: SSH_FXP_ATTRS, Size: 37, Number: 101384
> 2017-10-18 13:09:37.013 Type: SSH_FXP_READ, Size: 25, Number: 101637
. 2017-10-18 13:09:37.047 9 skipped SSH_FXP_WRITE, SSH_FXP_READ, SSH_FXP_DATA and SSH_FXP_STATUS packets.
> 2017-10-18 13:09:37.047 Type: SSH_FXP_CLOSE, Size: 13, Number: 102916
> 2017-10-18 13:09:37.048 Script: exit
. 2017-10-18 13:09:37.048 Script: Exit code: 0
. 2017-10-18 13:09:37.048 Closing connection.
. 2017-10-18 13:09:37.048 Sending special code: 12
. 2017-10-18 13:09:37.048 Sent EOF message
这是添加 "var output = exec.StdOut.ReadAll();"
后的完整工作代码
var exec = shell.Exec("winscp.com /log=\"C:\log\download.log\" ");
exec.StdIn.Write(
"option batch abort\n"+
"option confirm off\n"+
"open sftp://xxx:xxx@xxx -hostkey=\"xxx\" \n"+
"get -delete /compliance/* \"C:\compliance\\" \n"+
"exit");
exec.StdIn.Close();
var output = exec.StdOut.ReadAll();
谢谢
阿曼
你没有告诉我们你在exec.StdIn.Write()
之后做了什么。
确保你阅读了程序输出,例如像这样(如官方JScript example for Access to Input/Output streams)所示:
var output = exec.StdOut.ReadAll();
另见 Capturing outputs of WinSCP process(同样的问题,但针对 C#):
You need to continuously collect the output while the script is running. The output stream has limited capacity. Once it gets filled, WinSCP hangs waiting for free space, never finishing.
除上述之外,您还必须使用 \n
终止 exit
命令。尽管在您的情况下,使用标准输入提供命令没有多大意义。只需使用 /command
switch on WinSCP command-line.
我正在尝试从 SFTP 服务器下载大约 40 个文件,但它只下载了其中的 30 个文件并挂起。
请帮助解决问题。
这是我正在使用的脚本。
var exec = shell.Exec("winscp.com /xmllog=\"C:\download\log\download.xml\" ");
exec.StdIn.Write(
"option batch abort\n"+
"option confirm off\n"+
"open sftp://xxx:xxx@xxx -hostkey=\"xxxx\" \n"+
"get /download/* \"C:\download\\" \n"+
"exit");
请看下面的日志。
我无法在日志文件中找到任何问题:
> 2017-10-18 13:09:10.232 Type: SSH_FXP_REMOVE, Size: 54, Number: 99341
< 2017-10-18 13:09:10.233 Type: SSH_FXP_STATUS, Size: 24, Number: 99076
. 2017-10-18 13:09:10.233 Discarding reserved response
< 2017-10-18 13:09:10.235 Type: SSH_FXP_STATUS, Size: 24, Number: 99341
< 2017-10-18 13:09:10.235 Status code: 0
. 2017-10-18 13:09:10.236 File: '/compliance/2016_QTR2_DepreciationSummary.xlsx' [2017-10-18T17:07:08.000Z] [19819]
. 2017-10-18 13:09:10.236 Copying "/compliance/2016_QTR2_DepreciationSummary.xlsx" to local directory started.
. 2017-10-18 13:09:10.236 Binary transfer mode selected.
. 2017-10-18 13:09:10.236 Opening remote file.
> 2017-10-18 13:09:10.236 Type: SSH_FXP_OPEN, Size: 63, Number: 99587
< 2017-10-18 13:09:10.239 Type: SSH_FXP_HANDLE, Size: 13, Number: 99587
> 2017-10-18 13:09:10.239 Type: SSH_FXP_FSTAT, Size: 13, Number: 99848
< 2017-10-18 13:09:10.241 Type: SSH_FXP_ATTRS, Size: 37, Number: 99848
> 2017-10-18 13:09:10.242 Type: SSH_FXP_READ, Size: 25, Number: 100101
< 2017-10-18 13:09:10.250 Status code: 1
. 2017-10-18 13:09:10.250 3 skipped SSH_FXP_WRITE, SSH_FXP_READ, SSH_FXP_DATA and SSH_FXP_STATUS packets.
> 2017-10-18 13:09:10.250 Type: SSH_FXP_CLOSE, Size: 13, Number: 100612
. 2017-10-18 13:09:10.250 Preserving timestamp [2017-10-18T17:07:08.000Z]
. 2017-10-18 13:09:10.251 Transfer done: '/compliance/2016_QTR2_DepreciationSummary.xlsx' => 'C:\Users\Arman\Desktop\Work\pwc\final\servers\comerit_final_download_from_sftp\src\main\webapp\compliance16_QTR2_DepreciationSummary.xlsx' [19819]
. 2017-10-18 13:09:10.251 Deleting file "/compliance/2016_QTR2_DepreciationSummary.xlsx".
> 2017-10-18 13:09:10.251 Type: SSH_FXP_REMOVE, Size: 55, Number: 100877
< 2017-10-18 13:09:10.253 Type: SSH_FXP_STATUS, Size: 24, Number: 100612
. 2017-10-18 13:09:10.253 Discarding reserved response
< 2017-10-18 13:09:10.256 Type: SSH_FXP_STATUS, Size: 24, Number: 100877
< 2017-10-18 13:09:10.256 Status code: 0
. 2017-10-18 13:09:10.256 File: '/compliance/2016_QTR2_GainLossSummary.pdf' [2017-10-18T17:07:08.000Z] [120260]
< 2017-10-18 13:09:37.004 Script: Terminated by user.
. 2017-10-18 13:09:37.004 Copying "/compliance/2016_QTR2_GainLossSummary.pdf" to local directory started.
. 2017-10-18 13:09:37.004 Binary transfer mode selected.
. 2017-10-18 13:09:37.004 Checking existence of partially transferred file.
. 2017-10-18 13:09:37.005 Opening remote file.
> 2017-10-18 13:09:37.005 Type: SSH_FXP_OPEN, Size: 58, Number: 101123
< 2017-10-18 13:09:37.009 Type: SSH_FXP_HANDLE, Size: 13, Number: 101123
> 2017-10-18 13:09:37.009 Type: SSH_FXP_FSTAT, Size: 13, Number: 101384
< 2017-10-18 13:09:37.012 Type: SSH_FXP_ATTRS, Size: 37, Number: 101384
> 2017-10-18 13:09:37.013 Type: SSH_FXP_READ, Size: 25, Number: 101637
. 2017-10-18 13:09:37.047 9 skipped SSH_FXP_WRITE, SSH_FXP_READ, SSH_FXP_DATA and SSH_FXP_STATUS packets.
> 2017-10-18 13:09:37.047 Type: SSH_FXP_CLOSE, Size: 13, Number: 102916
> 2017-10-18 13:09:37.048 Script: exit
. 2017-10-18 13:09:37.048 Script: Exit code: 0
. 2017-10-18 13:09:37.048 Closing connection.
. 2017-10-18 13:09:37.048 Sending special code: 12
. 2017-10-18 13:09:37.048 Sent EOF message
这是添加 "var output = exec.StdOut.ReadAll();"
后的完整工作代码var exec = shell.Exec("winscp.com /log=\"C:\log\download.log\" ");
exec.StdIn.Write(
"option batch abort\n"+
"option confirm off\n"+
"open sftp://xxx:xxx@xxx -hostkey=\"xxx\" \n"+
"get -delete /compliance/* \"C:\compliance\\" \n"+
"exit");
exec.StdIn.Close();
var output = exec.StdOut.ReadAll();
谢谢
阿曼
你没有告诉我们你在exec.StdIn.Write()
之后做了什么。
确保你阅读了程序输出,例如像这样(如官方JScript example for Access to Input/Output streams)所示:
var output = exec.StdOut.ReadAll();
另见 Capturing outputs of WinSCP process(同样的问题,但针对 C#):
You need to continuously collect the output while the script is running. The output stream has limited capacity. Once it gets filled, WinSCP hangs waiting for free space, never finishing.
除上述之外,您还必须使用 \n
终止 exit
命令。尽管在您的情况下,使用标准输入提供命令没有多大意义。只需使用 /command
switch on WinSCP command-line.