Jump to content

ssh and ControlSend


Recommended Posts

Hi Guys,

I have become stuck writing to ssh, I manage to do it if I am logged on - but the script doesnt complete while logged off. I have tried useing ControlSendPlus - but that doesnt help, and I hav also tried using a UnixCmd.au3 which someone put on the forum, that launches putty for you - that I can actually get to work - but the downfall there is that before that executes it lauches a silly little window with a list of my servers in which I cant seem to get control of.

Anyway below is my basic ssh script, and the UnixCmd.au3 which launches putty and the UnixCmd.ini which the program uses.

include "C:\AutoDeploy\ControlSendPlus.au3"

;my script

Run("C:\Program Files\OpenSSH\bin\ssh -l jde 10.56.154.1")

ProcessWait("ssh.exe")

sleep(5000)

$sshWind = WinGetHandle("C:\Program Files\OpenSSH\bin\ssh.exe")

ControlSend($sshWind, "", "", "1029{ENTER}")

sleep(2000)

ControlSend($sshWind, "", "", "rm -R /u03/jdedwardsoneworld/AUTODEPLOY{ENTER}")

sleep(2000)

ControlSend($sshWind, "", "", "exit{ENTER}")

;UnixCmd.au3

#Region Compiler directives section

;** This is a list of compiler directives used by CompileAU3.exe.

;** comment the lines you don't need or else it will override the default settings

;#Compiler_Prompt=y ;y=show compile menu

;** AUT2EXE settings

;#Compiler_AUT2EXE=

;#Compiler_Icon=Wizard.ico ;Filename of the Ico file to use

;#Compiler_OutFile= ;Target exe filename.

#Compiler_Compression=4 ;Compression parameter 0-4 0=Low 2=normal 4=High

#Compiler_Allow_Decompile=y ;y= allow decompile

;#Compiler_PassPhrase= ;Password to use for compilation

;** Target program Resource info

#Compiler_Res_Comment=Unix Shell Management

#Compiler_Res_Description=Unix Shell Management

#Compiler_Res_Fileversion=1.2

#Compiler_Res_LegalCopyright=Giuseppe Criaco

; free form resource fields ... max 15

#Compiler_Res_Field=Email|gcriaco@sogei.it ;Free format fieldname|fieldvalue

#Compiler_Res_Field=Release Date|1/25/2006 ;Free format fieldname|fieldvalue

;#Compiler_Res_Field=Name|Value ;Free format fieldname|fieldvalue

;#Compiler_Res_Field=Name|Value ;Free format fieldname|fieldvalue

#Compiler_Run_AU3Check=y ;Run au3check before compilation

; The following directives can contain:

; %in% , %out%, %icon% which will be replaced by the fullpath\filename.

; %scriptdir% same as @ScriptDir and %scriptfile% = filename without extension.

#Compiler_Run_Before= ;process to run before compilation - you can have multiple records that will be processed in sequence

#Compiler_Run_After=move "%out%" "%scriptdir%" ;process to run After compilation - you can have multiple records that will be processed in sequence

#EndRegion

;===================================================================================================

================================================

;

; Program Name: UnixCmd()

; Description: Unix Shell Management

; Parameter(s): None

; Requirement(s): UnixCmd.ini, PuTTY Executable

; Return Value(s): None

; Author(s): Giuseppe Criaco <gcriaco@quipo.it>

;

;===================================================================================================

================================================

;

#Region - Include and Declarations

AutoItSetOption("TrayIconDebug", 1) ;Debug: 0=no info, 1=debug line info

#include <string.au3>

#include <GuiConstants.au3>

Global Const $WM_COMMAND = 0x0111

Global Const $LBN_SELCHANGE = 1

Global Const $LBN_DBLCLK = 2

Dim $asCmd[50], $iCmd = 0, $ip, $sHostName, $sEncryptPwd, $sPwd, $sUser, $iSleep = 1000, $sPutty, $iSendKeyDelay = 50

#EndRegion - Include and Declarations

#Region - GUI objects creation

GuiCreate("UnixCmd v. 1.2", 235, 375,(@DesktopWidth-325)/2, (@DesktopHeight-285)/2 , $WS_OVERLAPPEDWINDOW + $WS_VISIBLE + $WS_CLIPSIBLINGS)

GUICtrlCreateLabel("Server List:", 10, 10, 135, 30)

$lstServer = GuiCtrlCreateList("", 10, 30, 215, 310)

$btnOk = GuiCtrlCreateButton("&Start", 75, 340, 70, 25, $BS_DEFPUSHBUTTON )

$btnExit = GuiCtrlCreateButton("&Exit", 155, 340, 70, 25)

HotKeySet("{ESC}", "_Terminate")

#EndRegion - GUI objects creation

#Region Management

If (Not FileExists(@ScriptDir & "\UnixCmd.ini")) Then

#Region --- CodeWizard generated code Start ---

;MsgBox features: Title=Yes, Text=Yes, Buttons=OK, Icon=Warning

MsgBox(48, "File Not Found", "Required file not found (" & @ScriptDir & "\UnixCmd.ini" & ")" & @CRLF & "" & @CRLF & "UnixCmd will now exit!!!")

#EndRegion --- CodeWizard generated code End ---

Exit

EndIf

;General Options

$sSection = IniReadSection(@ScriptDir & "\UnixCmd.ini", "General")

If @error Then

#Region --- CodeWizard generated code Start ---

;MsgBox features: Title=Yes, Text=Yes, Buttons=OK, Icon=Critical

MsgBox(16,"UnixCmd","Unable to read the section General")

#EndRegion --- CodeWizard generated code End ---

Exit

Else

;Read Loop on the Section

For $iSection = 1 To $sSection[0][0]

Select

Case $sSection[$iSection][0] = "Putty"

$sPutty = $sSection[$iSection][1]

EndSelect

Next

EndIf

$var = IniReadSectionNames(@ScriptDir & "\UnixCmd.ini")

If @error Then

MsgBox(4096, "", "Error occured, probably no INI file.")

Else

;Load ListBox

For $i = 1 To $var[0]

if $var[$i] <> "General" Then GUICtrlSetData($lstServer,$var[$i])

Next

EndIf

; create the symmetric decryption key

$sSymmetricKey = _StringToHex ("wef,5RDx")

;Register a user defined function for the Windows Message ID WM_COMMAND.

GUIRegisterMsg($WM_COMMAND, "MY_WM_COMMAND")

GuiSetState()

While 1

$msg = GuiGetMsg()

Select

Case $msg = $GUI_EVENT_CLOSE Or $msg = $btnExit

_Terminate()

Case $msg = $btnOk

_Start()

EndSelect

WEnd

;===============================================================================

; Function Name: _StartPuTTY($sIP)

; Description: Start a Telnet session by PuTTY

; Parameter(s): None

; Requirement(s): None

; Return Value(s): None

; Author(s): Giuseppe Criaco <gcriaco@quipo.it>

;===============================================================================

Func _StartPuTTY($sIP)

Run(Chr(34) & $sPutty & Chr(34) & " -load " & Chr(34) & $sHostName & Chr(34))

WinWait($sIP & " - PuTTY", "")

If Not WinActive($sIP & " - PuTTY","") Then WinActivate($sIP & " - PuTTY","")

WinWaitActive($sIP & " - PuTTY","")

Sleep($iSleep)

EndFunc ;==>_StartPuTTY

Func _Terminate()

Exit 0

EndFunc

;===============================================================================

; Function Name: _Start()

; Description: None

; Parameter(s): None

; Requirement(s): None

; Return Value(s): None

; Author(s): Giuseppe Criaco <gcriaco@quipo.it>

;===============================================================================

Func _Start()

$sHostName = GUICtrlRead($lstServer)

$sSection = IniReadSection(@ScriptDir & "\UnixCmd.ini", $sHostName)

If @error Then

#Region --- CodeWizard generated code Start ---

;MsgBox features: Title=Yes, Text=Yes, Buttons=OK, Icon=Critical

MsgBox(16,"UnixCmd","Unable to read the section " & $sHostName)

#EndRegion --- CodeWizard generated code End ---

Exit

Else

;Read Loop on the Section

For $iSection = 1 To $sSection[0][0]

Select

Case $sSection[$iSection][0] = "IP"

$sIP = $sSection[$iSection][1]

Case $sSection[$iSection][0] = "User"

$sUser = $sSection[$iSection][1]

Case $sSection[$iSection][0] = "Pwd"

$sEncryptPwd = $sSection[$iSection][1]

;Decrypt user password

$sPwd = _StringEncrypt(0,$sEncryptPwd,_StringToHex ("wef,5RDx"))

If @error = 1 Then

#Region --- CodeWizard generated code Start ---

;MsgBox features: Title=Yes, Text=Yes, Buttons=OK, Icon=Critical

MsgBox(16,"UnixCmd","_StringEncrypt of " & $sEncryptPwd & " Failed")

#EndRegion --- CodeWizard generated code End ---

Exit

EndIf

Case $sSection[$iSection][0] = "SendKeyDelay"

$iSendKeyDelay = $sSection[$iSection][1]

Case $sSection[$iSection][0] = "Sleep"

$iSleep = $sSection[$iSection][1]

Case Else

If StringLeft ($sSection[$iSection][0], 1) <> "#" Then

$sCmdTmp = $sSection[$iSection][1]

If StringIsXDigit($sCmdTmp) Then ;returns 1

;Decrypt Item

$asCmd[$iCmd] = _StringEncrypt(0,$sCmdTmp,_StringToHex ("wef,5RDx"))

If @error = 1 Then

;MsgBox features: Title=Yes, Text=Yes, Buttons=OK, Icon=Critical

MsgBox(16,"UnixCmd","_StringEncrypt of " & $sCmdTmp & " Failed")

Exit

EndIf

Else

$asCmd[$iCmd] = $sCmdTmp

EndIf

$iCmd = $iCmd + 1

EndIf

EndSelect

Next

BlockInput(1)

;Start a PuTTY Telnet/SSH Session

_StartPuTTY($sIP)

AutoItSetOption("SendKeyDelay", $iSendKeyDelay)

;Login to the server

ControlSend ($sIP & " - PuTTY", "", "", $sUser & "{ENTER}")

ControlSend ($sIP & " - PuTTY", "", "", $sPwd & "{ENTER}")

; Executes commands listed under the INI Section

If $iCmd > 0 Then

$iTotItems = $iCmd -1

$iCmd = 0

For $iCmd = 0 To $iTotItems

ControlSend ($sIP & " - PuTTY", "", "", $asCmd[$iCmd] & "{ENTER}")

Next

EndIf

BlockInput(0)

EndIf

_Terminate()

EndFunc

;===============================================================================

; Function Name: MY_WM_COMMAND($hWnd, $Msg, $wParam, $lParam)

; Description: None

; Parameter(s): None

; Requirement(s): None

; Return Value(s): None

; Author(s): Holger

;===============================================================================

Func MY_WM_COMMAND($hWnd, $Msg, $wParam, $lParam)

$nNotifyCode = BitShift($wParam, 16)

$nID = BitAnd($wParam, 0x0000FFFF)

$hCtrl = $lParam

If $nID = $lstServer Then

Switch $nNotifyCode

Case $LBN_DBLCLK

_Start()

Return 0

EndSwitch

EndIf

EndFunc

;UnixCmd.ini

[General]

Putty=C:\AutoDeploy\putty.exe

[szaordv1]

IP=10.56.154.1

User=jde

Pwd=D1501DA3A043DDFF5FC7C3EC1DE108003FA2

sleep=3000

SendKeyDelay=100

Cmd1=rm -R /u03/jdedwardsoneworld/b7334/packages/AUTODEPLOY{ENTER}

Cmd2=exit{ENTER}

Anyone got some ideas for me, my goal is to remove a folder on a unix box - openssh works but only if im logged on, UnixCmd.au3 launches a app which I cant seem to execute the controls on?

Thanks,

Stunt

Link to comment
Share on other sites

  • 4 months later...

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
 Share

  • Recently Browsing   0 members

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