Jump to content

Recommended Posts

Posted

Maybe it will be useful to someone

To use UDF you need to copy 2 files PLINK.EXE and PSCP.EXE and edit the path if needed!

;#include <AutoItConstants.au3>
;Opt("MustDeclareVars", 1) ;0=no, 1=require pre-declaration
Global $Path_SSH_Plink_EXE = '"' & @ScriptDir & '\x86\plink.exe' & '"'
Global $Path_PSCP_EXE = '"' & @ScriptDir & '\x86\pscp.exe' & '"'

;~ Global $iHost = 'x.x.x.x'
;~ Global $iPort = '22'
;~ Global $iUserName = 'root'
;~ Global $iUserPassword = 'passssssssss'


;~ ;-----------------------------------------------------------------------------------------------
;~ _SSH_SendCmd($iHost, 22, $iUserName, $iUserPassword, 'ls -lS')

;~ ;-----------------------------------------------------------------------------------------------
;~ _EG_Run_Multi_Command($iHost, $iPort, $iUserName, $iUserPassword)

;~ ;-----------------------------------------------------------------------------------------------
;~ FileDelete(@ScriptDir & '\z.exe')
;~ _SSH_SendFile(@ScriptDir & '\putty.exe', '/tmp/z.exe', $iHost, 22, $iUserName, $iUserPassword)
;~ _SSH_SendCmd($iHost, 22, $iUserName, $iUserPassword, 'ls -lS /tmp/', False)
;~ _SSH_GetFile(@ScriptDir & '\z.exe', '/tmp/z.exe', $iHost, 22, $iUserName, $iUserPassword)
;~ _SSH_SendCmd($iHost, 22, $iUserName, $iUserPassword, 'rm -rf /tmp/*', False)
;~ _SSH_SendCmd($iHost, 22, $iUserName, $iUserPassword, 'ls -lS /tmp/', False)
;~ ConsoleWrite('- FileExists z.exe: ' & FileExists(@ScriptDir & '\z.exe') & @CRLF)
;~ FileDelete(@ScriptDir & '\z.exe')
;~ ;-----------------------------------------------------------------------------------------------

;~ DirRemove(@ScriptDir & '\_x88', 1)
;~ _SSH_SendCmd($iHost, 22, $iUserName, $iUserPassword, 'rm -rf /tmp/*', False)
;~ _SSH_SendCmd($iHost, 22, $iUserName, $iUserPassword, 'ls -lS /tmp/', False)
;~ _SSH_SendCmd($iHost, 22, $iUserName, $iUserPassword, 'mkdir /tmp/x86/', False)
;~ _SSH_SendFolder(@ScriptDir & '\x86', '/tmp/x86', $iHost, 22, $iUserName, $iUserPassword)
;~ _SSH_SendCmd($iHost, 22, $iUserName, $iUserPassword, 'ls -lS /tmp/', False)
;~ _SSH_SendCmd($iHost, 22, $iUserName, $iUserPassword, 'ls -lS /tmp/x86/', False)
;~ FileDelete(@ScriptDir & '\_x88')
;~ DirCreate(@ScriptDir & '\_x88')
;~ _SSH_GetFolder(@ScriptDir & '\_x88', '/tmp/x86', $iHost, 22, $iUserName, $iUserPassword)
;~ _SSH_SendCmd($iHost, 22, $iUserName, $iUserPassword, 'rm -rf /tmp/*', False)
;~ _SSH_SendCmd($iHost, 22, $iUserName, $iUserPassword, 'ls -lS /tmp/', False)
;~ ConsoleWrite('- FileExists _x88: ' & DirGetSize(@ScriptDir & '\_x88') & @CRLF)
;~ DirRemove(@ScriptDir & '\_x88', 1)

;~ ;-----------------------------------------------------------------------------------------------

;~ DirRemove(@ScriptDir & '\_x99', 1)
;~ _SSH_SendCmd($iHost, 22, $iUserName, $iUserPassword, 'rm -rf /tmp/*', False)
;~ _SSH_SendCmd($iHost, 22, $iUserName, $iUserPassword, 'ls -lS /tmp/', False)
;~ _SSH_SendCmd($iHost, 22, $iUserName, $iUserPassword, 'mkdir /tmp/_xx/', False)
;~ _SSH_SendFolder(@ScriptDir & '\_xx', '/tmp/', $iHost, 22, $iUserName, $iUserPassword, True)
;~ _SSH_SendCmd($iHost, 22, $iUserName, $iUserPassword, 'ls -lS /tmp/', False)
;~ _SSH_SendCmd($iHost, 22, $iUserName, $iUserPassword, 'ls -lS /tmp/_xx/', False)
;~ FileDelete(@ScriptDir & '\_x99')
;~ DirCreate(@ScriptDir & '\_x99')
;~ _SSH_GetFolder(@ScriptDir & '\_x99', '/tmp/_xx', $iHost, 22, $iUserName, $iUserPassword, True)
;~ ConsoleWrite('- FileExists _x99: ' & DirGetSize(@ScriptDir & '\_x99') & @CRLF)
;~ _SSH_SendCmd($iHost, 22, $iUserName, $iUserPassword, 'rm -rf /tmp/*', False)
;~ _SSH_SendCmd($iHost, 22, $iUserName, $iUserPassword, 'ls -lS /tmp/', False)
;~ DirRemove(@ScriptDir & '\_x99', 1)

;~ ;-----------------------------------------------------------------------------------------------

Func _EG_Run_Multi_Command($iHost, $iPort, $iUserName, $iUserPassword)
    Local $Plink_PID = _SSH_Plink_Connect($iHost, $iPort, $iUserName, $iUserPassword)
    ConsoleWrite('! $Plink_PID: ' & $Plink_PID & @CRLF)
    Local $sOutput, $sOutput_tmp
    If $Plink_PID > 0 Then
        _SSH_Plink_SendCmd($Plink_PID, @CR)
        $sOutput_tmp = _SSH_Plink_SendCmd($Plink_PID, 'ls /tmp/')
        ConsoleWrite('- ' & $sOutput_tmp & @CRLF)
        For $i = 1 To 5
            $sOutput_tmp = _SSH_Plink_SendCmd($Plink_PID, ' mkdir /tmp/Dir_' & $i)
            ConsoleWrite('- ' & $sOutput_tmp & @CRLF)
            $sOutput &= $sOutput_tmp & @CRLF
        Next
        ;$sOutput_tmp = _SSH_Plink_SendCmd($Plink_PID, 'rm -rf /tmp/*')
        ;ConsoleWrite('- ' & $sOutput_tmp & @CRLF)
        ;$sOutput &= $sOutput_tmp & @CRLF
        $sOutput_tmp = _SSH_Plink_SendCmd($Plink_PID, 'ls /tmp/')
        ConsoleWrite('- ' & $sOutput_tmp & @CRLF)
        $sOutput &= $sOutput_tmp & @CRLF
        If ProcessExists($Plink_PID) Then ProcessClose($Plink_PID)
    EndIf
    ClipPut($sOutput)
EndFunc   ;==>_EG_Run_Multi_Command


Func _SSH_GetFile($LocalFilePath, $RemoteFilePath, $sHost, $sPort, $sUserName, $sUserPassword) ;Copy single file
    Local $sCommand = ' -P ' & $sPort & ' -l ' & $sUserName & ' -pw ' & $sUserPassword & ' ' & $sHost & ':' & $RemoteFilePath & ' "' & $LocalFilePath & '" '
    Return __PSCP_Execute($sCommand)
EndFunc   ;==>_SSH_GetFile

Func _SSH_GetFolder($LocalFolderPath, $RemoteFolderPath, $sHost, $sPort, $sUserName, $sUserPassword, $sRecur = False)
    If $sRecur Then ;Copy all files & folders in a folder
        Local $sCommand = ' -P ' & $sPort & ' -l ' & $sUserName & ' -pw ' & $sUserPassword & ' -r ' & $sHost & ':"' & $RemoteFolderPath & '" "' & $LocalFolderPath & '"'
        Return __PSCP_Execute($sCommand)
    Else ;Copy all files in a folder
        Local $sCommand = ' -P ' & $sPort & ' -l ' & $sUserName & ' -pw ' & $sUserPassword & ' ' & $sHost & ':"' & $RemoteFolderPath & '/*" "' & $LocalFolderPath & '"'
        Return __PSCP_Execute($sCommand)
    EndIf
EndFunc   ;==>_SSH_GetFolder

Func _SSH_SendFile($LocalFilePath, $RemoteFilePath, $sHost, $sPort, $sUserName, $sUserPassword) ;Copy single file
    Local $sCommand = ' -P ' & $sPort & ' -l ' & $sUserName & ' -pw ' & $sUserPassword & ' "' & $LocalFilePath & '" ' & $sHost & ':' & $RemoteFilePath
    Return __PSCP_Execute($sCommand)
EndFunc   ;==>_SSH_SendFile

Func _SSH_SendFolder($LocalFolderPath, $RemoteFolderPath, $sHost, $sPort, $sUserName, $sUserPassword, $sRecur = False)
    If $sRecur Then ;Copy all files & folders in a folder
        Local $sCommand = ' -P ' & $sPort & ' -l ' & $sUserName & ' -pw ' & $sUserPassword & ' -r "' & $LocalFolderPath & '" ' & $sHost & ':"' & $RemoteFolderPath & '"'
        Return __PSCP_Execute($sCommand)
    Else ;Copy all files in a folder
        Local $sCommand = ' -P ' & $sPort & ' -l ' & $sUserName & ' -pw ' & $sUserPassword & ' "' & $LocalFolderPath & '\*" ' & $sHost & ':"' & $RemoteFolderPath & '"'
        Return __PSCP_Execute($sCommand)
    EndIf
EndFunc   ;==>_SSH_SendFolder

Func _SSH_SendCmd($sHost, $sPort, $sUserName, $sUserPassword, $sRequest, $sleep = True)
    Local $sCommand = '-ssh -no-antispoof ' & $sHost & ' -P ' & $sPort & ' -l ' & $sUserName & ' -pw ' & $sUserPassword & ' ' & $sRequest
    If $sleep Then $sCommand &= ' ; sleep 1'  ; sleep prevents truncation of output by fast server disconnect
    ConsoleWrite($Path_SSH_Plink_EXE & ' ' & $sCommand & @CRLF)
    Local $Plink_PID = Run($Path_SSH_Plink_EXE & ' ' & $sCommand, '', @SW_HIDE, 9)
    Local $sStdoutRead, $sData
    While ProcessExists($Plink_PID)
        $sStdoutRead = StdoutRead($Plink_PID)
        If @error Then ExitLoop
        If $sStdoutRead <> '' Then $sData &= $sStdoutRead
        $sStdoutRead = ''
    WEnd
    $sStdoutRead = ''
    While ProcessExists($Plink_PID)
        $sStdoutRead = StderrRead($Plink_PID)
        If @error Then ExitLoop
        If $sStdoutRead <> '' Then $sData &= $sStdoutRead
        $sStdoutRead = ''
    WEnd
    ConsoleWrite($sData & @CRLF)
    If ProcessExists($Plink_PID) Then ProcessClose($Plink_PID)
    Return __SanitizeOutput($sData) ; returns the full plink command output
EndFunc   ;==>_SSH_SendCmd

Func _SSH_Plink_Connect($sHost, $sPort, $sUserName, $sUserPassword)
    Local $sCommand = '-ssh -no-antispoof ' & $sHost & ' -P ' & $sPort & ' -l ' & $sUserName & ' -pw ' & $sUserPassword ; sleep prevents truncation of output by fast server disconnect
    ConsoleWrite($Path_SSH_Plink_EXE & ' ' & $sCommand & @CRLF)
    Local $Plink_PID = Run($Path_SSH_Plink_EXE & ' ' & $sCommand, '', @SW_HIDE, 7)
    Local $sStdoutRead, $sData, $error1, $error2
    While True
        $sStdoutRead = StdoutRead($Plink_PID)
        $error1 = @error
        If $sStdoutRead <> '' Then $sData &= $sStdoutRead
        $sStdoutRead = ''
        $sStdoutRead = StderrRead($Plink_PID)
        $error2 = @error
        If $sStdoutRead <> '' Then $sData &= $sStdoutRead
        ;ConsoleWrite($sData & @CRLF)
        If StringInStr(StringRight($sData, 1), "#") Then Return $Plink_PID
        If StringInStr(StringRight($sData, 2), "# ") Then Return $Plink_PID
        If StringInStr($sData, "Welcome") Then Return $Plink_PID
        If $error2 And $error2 Then ExitLoop
        $sStdoutRead = ''
        $error1 = ''
        $error2 = ''
    WEnd
    Return SetError(1, 0, $Plink_PID)
EndFunc   ;==>_SSH_Plink_Connect

Func _SSH_Plink_SendCmd($Plink_PID, $sCMD)
    Local $sStdoutRead, $sData, $error1, $error2
    ConsoleWrite('>  _SSH_Plink_SendCmd [' & $Plink_PID & ']: ' & $sCMD & @CRLF)
    StdinWrite($Plink_PID, $sCMD & @CR)
    Sleep(250)
    While True
        Sleep(100)
        $sStdoutRead = StdoutRead($Plink_PID)
        $error1 = @error
        ;If $error Then ExitLoop
        If $sStdoutRead <> '' Then $sData &= $sStdoutRead
        $sStdoutRead = ''
        $sStdoutRead = StderrRead($Plink_PID)
        $error2 = @error
        If $error2 And $error2 Then ExitLoop
        If $sStdoutRead <> '' Then $sData &= $sStdoutRead
        $sStdoutRead = ''
        $error1 = ''
        $error2 = ''
        ;ConsoleWrite($sData & @CRLF)
        If StringInStr(StringRight($sData, 1), "#") Then Return __SanitizeOutput($sData)
        If StringInStr(StringRight($sData, 2), "# ") Then Return __SanitizeOutput($sData)
    WEnd
    Return __SanitizeOutput($sData)
EndFunc   ;==>_SSH_Plink_SendCmd

Func __SanitizeOutput($str)
    Local $sBell = BinaryToString("0x07", 4), $sESC = BinaryToString("0x1B", 4)
    $str = StringReplace($str, BinaryToString("0xC3A2E282ACCB9C", 4), "'") ; '‘'
    $str = StringReplace($str, BinaryToString("0xC3A2E282ACE284A2", 4), "'") ;  '’'
    $str = StringReplace($str, $sESC & "[?2004h", "")
    $str = StringReplace($str, $sESC & "[?2004l" & @CR, "")
    If StringInStr($str, $sBell) Then ; �
        Local $lines = StringSplit($str, @CRLF)
        Local $p1, $p2
        For $i = 1 To $lines[0]
            $p1 = StringInStr($str, $sESC & "]0;") ; �
            If $p1 <> 0 Then
                $p2 = StringInStr($str, $sBell, 1, 1, $p1)
                If $p2 <> 0 Then
                    $str = StringLeft($str, $p1 - 1) & StringRight($str, StringLen($str) - $p2)
                EndIf
            EndIf
        Next
    EndIf
    ; Matching Pattern: ESC\[[0-107][;0-107][;0-255]m               ; leading zeros are matched
    ; Matches ANSI Codes: SGR, 3-bit color, 4-bit color and 8-bit color
    $str = StringRegExpReplace($str, $sESC & "\[([0-1]0[0-7]|\d?\d)?(;[0-1]0[0-7]|;\d?\d)?(;[0-1]?\d?\d|;(0|2)[0-5][0-5])?m", "")
    ; Matching Pattern: ESC\[(38|48);2;[0-255];[0-255];[0-255]m     ; leading zeros are matched
    ; Matches ANSI Codes: True Color (24-bit)
    $str = StringRegExpReplace($str, $sESC & "\[(38|48);2;([0-1]?\d?\d|(0|2)[0-5][0-5]);([0-1]?\d?\d|(0|2)[0-5][0-5]);([0-1]?\d?\d|(0|2)[0-5][0-5])m", "")
    Return $str
EndFunc   ;==>__SanitizeOutput

Func __PSCP_Execute($sCommand)
    Local $sStdoutRead, $sData, $error1, $error2
    Local $PSCP_PID = Run($Path_PSCP_EXE & ' ' & $sCommand, '', @SW_HIDE, 7)
    ConsoleWrite('>  __PSCP_Execute [' & $PSCP_PID & ']: ' & $sCommand & @CRLF)
    Sleep(250)
    While True
        Sleep(100)
        $sStdoutRead = StdoutRead($PSCP_PID)
        $error1 = @error
        ;If $error Then ExitLoop
        If $sStdoutRead <> '' Then $sData &= $sStdoutRead
        $sStdoutRead = ''
        $sStdoutRead = StderrRead($PSCP_PID)
        $error2 = @error
        If $error2 And $error2 Then ExitLoop
        If $sStdoutRead <> '' Then $sData &= $sStdoutRead
        $sStdoutRead = ''
        $error1 = ''
        $error2 = ''
        ;ConsoleWrite($sData & @CRLF)
    WEnd
    Return __SanitizeOutput($sData)
EndFunc   ;==>__PSCP_Execute

> Any suggestions and contributions are welcome!

 

 

 

Regards,
 

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...