SFTP与代理转移?

时间:2013-02-01 14:38:06

标签: vbscript winscp

是否可以修改以下WinSCP VBScript文件以允许代理连接?

'###########################################################################
'# 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

3 个答案:

答案 0 :(得分:1)

代理必须充当Man in the MiddleSSH(协议SCP用于数据传输)旨在防止这种情况发生。

答案 1 :(得分:1)

在行中,添加open命令:

sFTPScript = sFTPScript & "open sftp://" & sUsername & ":" & sPassword & "@" & sSite & vbCrLf

...添加-rawsettings开关,然后添加与您的代理类型相关的选项:

例如:

sFTPScript = ... & " -rawsettings ProxyMethod=3 ProxyHost=proxy" & vbCrLf

详情请参阅:
https://winscp.net/eng/docs/scriptcommand_open
https://winscp.net/eng/docs/rawsettings

答案 2 :(得分:0)

要使用代理,请首先使用WINSCP GUI连接指定代理详细信息。这将在配置文件中添加代理的详细信息(与winscp.com位于同一目录中),然后它应该可以工作。然后,您可以将此配置文件的路径传递给执行进程任务,并调用WINSCP.com可执行文件。