Jump to content

Search the Community

Showing results for tags '_ssh_sendcmd'.

  • Search By Tags

    Type tags separated by commas.
  • Search By Author

Content Type


Forums

  • General
    • Announcements and Site News
    • Administration
  • AutoIt v3
    • AutoIt Help and Support
    • AutoIt Technical Discussion
    • AutoIt Example Scripts
  • Scripting and Development
    • Developer General Discussion
    • Language Specific Discussion
  • IT Administration
    • Operating System Deployment
    • Windows Client
    • Windows Server
    • Office

Categories

  • AutoIt Team
    • Beta
    • MVP
  • AutoIt
    • Automation
    • Databases and web connections
    • Data compression
    • Encryption and hash
    • Games
    • GUI Additions
    • Hardware
    • Information gathering
    • Internet protocol suite
    • Maths
    • Media
    • PDF
    • Security
    • Social Media and other Website API
    • Windows
  • Scripting and Development
  • IT Administration
    • Operating System Deployment
    • Windows Client
    • Windows Server
    • Office

Find results in...

Find results that contain...


Date Created

  • Start

    End


Last Updated

  • Start

    End


Filter by number of...

Joined

  • Start

    End


Group


Member Title


Location


WWW


Interests

Found 1 result

  1. 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!
×
×
  • Create New...