2013-02-01 72 views
0

可以修改以下WinSCP VBScript文件以允許代理連接?帶代理的SFTP傳輸?

'########################################################################### 
'# Function: MISC_FTPUpload 
'# 
'# Description: 
'# Uses the FSO object to FTP a file to a remote server 
'# 
'# Parameters: 
'# (in) sSite    - The site to FTP to 
'# (in) sUsername  - The username to log in with 
'# (in) sPassword  - The password to log in with 
'# (in) sLocalFile  - The Locally stored file to FTP to the remote server 
'# (in) sRemotePath - The path to store the file in, on the remote server 
'# 
'# (out) - sError - The error output 
'# 
'# Return: 
'# True - File successfully sent 
'# False - File not successfully sent 
'########################################################################### 

Function MISC_FTPUpload(byVal sSite, byVal sUsername, byVal sPassword, byVal sLocalFile, byVal sRemotePath, byRef sError) 
'This script is provided under the Creative Commons license located 
'at http://creativecommons.org/licenses/by-nc/2.5/ . It may not 
'be used for commercial purposes with out the expressed written consent 
'of NateRice.com 

    Const OpenAsDefault = -2 
    Const FailIfNotExist = 0 
    Const ForReading = 1 
    Const ForWriting = 2 
    Dim oFTPScriptFSO 
    Dim oFTPScriptShell 
    Dim sOriginalWorkingDirectory 
    Dim sFTPScript 
    Dim sFTPTemp 
    Dim bRetCode 
    Dim sFTPTempFile 
    Dim oFTPScript 
    Dim sResults 
    Dim sOut 
    Dim sCmd 

    LOG_Write "MISC_FTPUpload called at: " & Now 

    Set oFTPScriptFSO = CreateObject("Scripting.FileSystemObject") 
    Set oFTPScriptShell = CreateObject("WScript.Shell") 

    sRemotePath = Trim(sRemotePath) 
    sLocalFile = Trim(sLocalFile) 

    '----------Path Checks--------- 
    'Here we will check the path, if it contains 
    'spaces then we need to add quotes to ensure 
    'it parses correctly. 
    If InStr(sRemotePath, " ") > 0 Then 
     If Left(sRemotePath, 1) <> """" And Right(sRemotePath, 1) <> """" Then 
       sRemotePath = """" & sRemotePath & """" 
     End If 
    End If 

    If InStr(sLocalFile, " ") > 0 Then 
     If Left(sLocalFile, 1) <> """" And Right(sLocalFile, 1) <> """" Then 
       sLocalFile = """" & sLocalFile & """" 
     End If 
    End If 

    'Check to ensure that a remote path was 
    'passed. If it's blank then pass a "\" 
    If Len(sRemotePath) = 0 Then 
     'Please note that no premptive checking of the 
     'remote path is done. If it does not exist for some 
     'reason, Unexpected results may occur. 
     sRemotePath = "\" 
    End If 

    'Check the local path and file to ensure 
    'that either the a file that exists was 
    'passed or a wildcard was passed. 
    If InStr(sLocalFile, "*") Then 
     If InStr(sLocalFile, " ") Then 
      sError = "Error: Wildcard uploads do not work if the path contains a space." & vbNewLine & "This is a limitation of the Microsoft FTP client." 
       LOG_Write sError 
       MISC_FTPUpload = False 
       Exit Function 
     End If 
    ElseIf Len(sLocalFile) = 0 Or Not oFTPScriptFSO.FileExists(sLocalFile) Then 
     'nothing to upload 
     sError = "Error: File Not Found." 
     LOG_Write sError 
     MISC_FTPUpload = False 
     Exit Function 
    End If 
    '--------END Path Checks--------- 

    'build input file for ftp command 
    sFTPScript = sFTPScript & "option batch on" & vbCRLF 
    sFTPScript = sFTPScript & "option confirm off"& vbCrLf 
    sFTPScript = sFTPScript & "option transfer binary" & vbCrLf 
    sFTPScript = sFTPScript & "open sftp://" & sUsername & ":" & sPassword & "@" & sSite & vbCrLf 
    sFTPScript = sFTPScript & "cd " & sRemotePath & vbCrLf 
    sFTPScript = sFTPScript & "put " & sLocalFile & vbCRLF 
    sFTPScript = sFTPScript & "close" & vbCrLf 
    sFTPScript = sFTPScript & "exit" & vbCrLf 

    LOG_Write "Script for FTP File: " & vbNewLine & sFTPScript 

    sFTPTemp = oFTPScriptShell.ExpandEnvironmentStrings("%TEMP%") 
    sFTPTempFile = sFTPTemp & "\" & oFTPScriptFSO.GetTempName 
    LOG_Write "FTP Input file stored at: " & sFTPTempFile 

    'Write the input file for the ftp command 
    'to a temporary file. 
    Set oFTPScript = oFTPScriptFSO.CreateTextFile(sFTPTempFile, True) 
    oFTPScript.WriteLine(sFTPScript) 
    oFTPScript.Close 
    Set oFTPScript = Nothing 

    sCmd = """C:\Program Files\WinSCP\WinSCP.com"" -script=" & sFTPTempFile 
    MISC_RunCmd sCmd, sOut, sError 
    LOG_Write sOut 

    Wscript.Sleep 1000 

    ' Get rid of temp file used for input to sftp 
    oFTPScriptFSO.DeleteFile(sFTPTempFile) 

    'Check results of transfer.  
    If sError = "" And InStr(sOut, "binary") >0 And InStr(sOut, "100%") >0 Then 
     MISC_FTPUpload = True 
    Else 
     sError = "Error: " & sError 
     LOG_Write sError 
     MISC_FTPUpload = False 
    End If 

    Set oFTPScriptFSO = Nothing 
    Set oFTPScriptShell = Nothing 
End Function 

'########################################################################### 
'# Function: MISC_FTPDownload 
'# 
'# Description: 
'# Uses the FSO object to FTP a file from a remote server 
'# 
'# Parameters: 
'# (in) sSite    - The site to FTP from 
'# (in) sUsername  - The username to log in with 
'# (in) sPassword  - The password to log in with 
'# (in) sLocalPath  - The path to store the file in, on the local drive 
'# (in) sLocalPath  - The path to get the file from, on the remote drive 
'# (in) sRemoteFile - The remotely stored file to FTP to the local drive 
'# 
'# (out) - sError - The error output 
'# 
'# Return: 
'# True - File successfully retrieved 
'# False - File not successfully retrieved 
'########################################################################### 

Function MISC_FTPDownload(byVal sSite, byVal sUsername, byVal sPassword, byVal sLocalPath, byVal sRemotePath, byVal sRemoteFile, byRef sError) 
'This script is provided under the Creative Commons license located 
'at http://creativecommons.org/licenses/by-nc/2.5/ . It may not 
'be used for commercial purposes with out the expressed written consent 
'of NateRice.com 

    Const OpenAsDefault = -2 
    Const FailIfNotExist = 0 
    Const ForReading = 1 
    Const ForWriting = 2 
    Dim oFTPScriptFSO 
    Dim oFTPScriptShell 
    Dim sOriginalWorkingDirectory 
    Dim sFTPScript 
    Dim sFTPTemp 
    Dim sFTPTempFile 
    Dim bRetCode 
    Dim oFTPScript 
    Dim sResults 
    Dim sCmd 
    Dim sOut 

    LOG_Write "MISC_FTPDownload called at: " & Now 

    Set oFTPScriptFSO = CreateObject("Scripting.FileSystemObject") 
    Set oFTPScriptShell = CreateObject("WScript.Shell") 

    sRemotePath = Trim(sRemotePath) 
    sLocalPath = Trim(sLocalPath) 

    '----------Path Checks--------- 
    'Here we will check the remote path, if it contains 
    'spaces then we need to add quotes to ensure 
    'it parses correctly. 
    If InStr(sRemotePath, " ") > 0 Then 
     If Left(sRemotePath, 1) <> """" And Right(sRemotePath, 1) <> """" Then 
      sRemotePath = """" & sRemotePath & """" 
     End If 
    End If 

    'Check to ensure that a remote path was 
    'passed. If it's blank then pass a "\" 
    If Len(sRemotePath) = 0 Then 
     'Please note that no premptive checking of the 
     'remote path is done. If it does not exist for some 
     'reason. Unexpected results may occur. 
     sRemotePath = "\" 
    End If 

    'If the local path was blank. Pass the current 
    'working direcory. 
    If Len(sLocalPath) = 0 Then 
     sLocalPath = oFTPScriptShell.CurrentDirectory 
    End If 

    If Not oFTPScriptFSO.FolderExists(sLocalPath) Then 
     'destination not found 
     sError = "Error: Local Folder Not Found." 
     LOG_Write sError 
     MISC_FTPDownload = False 
     Exit Function 
    End If 

    sOriginalWorkingDirectory = oFTPScriptShell.CurrentDirectory 
    oFTPScriptShell.CurrentDirectory = sLocalPath 
    '--------END Path Checks--------- 

    'build input file for ftp command 
    sFTPScript = sFTPScript & "option batch on" & vbCRLF 
    sFTPScript = sFTPScript & "option confirm off"& vbCrLf 
    sFTPScript = sFTPScript & "option transfer binary" & vbCrLf 
    sFTPScript = sFTPScript & "open sftp://" & sUsername & ":" & sPassword & "@" & sSite & vbCrLf 
    sFTPScript = sFTPScript & "cd " & sRemotePath & vbCrLf 
    sFTPScript = sFTPScript & "get " & sRemoteFile & vbCRLF 
    sFTPScript = sFTPScript & "close" & vbCrLf 
    sFTPScript = sFTPScript & "exit" & vbCrLf 

    LOG_Write "Script for FTP File: " & vbNewLine & sFTPScript 

    sFTPTemp = oFTPScriptShell.ExpandEnvironmentStrings("%TEMP%") 
    sFTPTempFile = sFTPTemp & "\" & oFTPScriptFSO.GetTempName 
    LOG_Write "FTP Input file stored at: " & sFTPTempFile 

    'Write the input file for the ftp command 
    'to a temporary file. 
    Set oFTPScript = oFTPScriptFSO.CreateTextFile(sFTPTempFile, True) 
    oFTPScript.WriteLine(sFTPScript) 
    oFTPScript.Close 
    Set oFTPScript = Nothing 

    sCmd = """C:\Program Files\WinSCP\WinSCP.com"" -script=" & sFTPTempFile 
    MISC_RunCmd sCmd, sOut, sError 
    LOG_Write sOut 

    Wscript.Sleep 1000 

    ' Get rid of temp file used for input to sftp 
    oFTPScriptFSO.DeleteFile(sFTPTempFile) 

    'Check results of transfer.  
    If sError = "" And InStr(sOut, "binary") >0 And InStr(sOut, "100%") >0 Then 
     MISC_FTPDownload = True 
    Else 
     sError = "Error: " & sError 
     LOG_Write sError 
     MISC_FTPDownload = False 
    End If 

    Set oFTPScriptFSO = Nothing 
    Set oFTPScriptShell = Nothing 
End Function 

回答

1

代理將不得不作爲Man in the Middle爲此。 SSH(協議SCP用於數據傳輸)旨在防止這種情況。

0

要使用代理,首先使用WINSCP GUI連接指定代理的信息。這將在配置文件(它與winscp.com位於同一目錄中)中添加代理的詳細信息,然後它應該可以正常工作。然後,您可以將此配置文件的路徑傳遞給您的執行進程任務,並調用WINSCP.com可執行文件。

+0

請填寫您的最後一句。 – demongolem

+0

嗨demongolem,謝謝你指出。我在不同的網站上發佈了相同查詢的答案,而複製文本時最後一行沒有正確粘貼 – Diwakar