我正在尝试使用WinSCP通过SFTP从远程目录下载一些文件。
直到现在我有了这个VBScript。请注意,SFTP服务器的连接是通过私钥(.ppk文件)设置的。
代码如下:
Function SFTPDownload(byVal sLocalPath, byVal sRemotePath, byVal sRemoteFile)
Set oFTPScriptFSO = CreateObject("Scripting.FileSystemObject")
Set oFTPScriptShell = CreateObject("WScript.Shell")
sRemotePath = Trim(sRemotePath)
sLocalPath = Trim(sLocalPath)
sOriginalWorkingDirectory = oFTPScriptShell.CurrentDirectory
oFTPScriptShell.CurrentDirectory = sLocalPath
sFTPScript = sFTPScript & "option batch on" & vbCRLF
sFTPScript = sFTPScript & "option confirm off"& vbCrLf
sFTPScript = sFTPScript & "option transfer binary" & vbCrLf
sFTPScript = sFTPScript & "open username:@sftp.server.com:22 -privatekey=mypriviatekey.ppk" & vbCrLf
sFTPScript = sFTPScript & "cd " & sRemotePath & vbCrLf
sFTPScript = sFTPScript & "get " & sRemoteFile & vbCRLF
sFTPScript = sFTPScript & "close" & vbCrLf
sFTPScript = sFTPScript & "exit" & vbCrLf
sFTPTemp = oFTPScriptShell.ExpandEnvironmentStrings("%TEMP%")
sFTPTempFile = sFTPTemp & "\" & oFTPScriptFSO.GetTempName
'Write the input file for the sftp command to a temporary file.
Set oFTPScript = oFTPScriptFSO.CreateTextFile(sFTPTempFile, True)
oFTPScript.WriteLine(sFTPScript)
oFTPScript.Close
Set oFTPScript = Nothing
sCmd = """C:\Program Files (x86)\WinSCP\WinSCP.com"" -script=" & sFTPTempFile
oFTPScriptShell.run sCmd
Wscript.Sleep 1000
' Get rid of temp file used for input to sftp
oFTPScriptFSO.DeleteFile(sFTPTempFile)
Set oFTPScriptFSO = Nothing
Set oFTPScriptShell = Nothing
End Function
Dim myResult
myResult = SFTPDownload("F:\WLMS_TEAM\TOUHID\TT Files\", "/done", "EE*.csv")
脚本打开WinSCP命令提示符并给我:
无法使用密钥文件(无法打开文件)
我知道如何让它发挥作用?
答案 0 :(得分:4)
我找到了解决方案。代码没有任何问题。
我刚刚修改了脚本文件以包含私钥的完整路径,如下所示:
option batch on
option confirm off
option transfer binary
open btwsale:@sftp.server.com:22 -privatekey="C:\Program Files (x86)\WinSCP\mypriviatekey.ppk"
cd /done
lcd "C:\Program Files (x86)\WinSCP\"
get EE*.csv
exit