Sign in to follow this  
Followers 0
tlambrechts

Autoit expect ssh / telnet help

6 posts in this topic

Hello,

I have successfully created a telnet / ssh script by using putty - but are having some problems creating a expect sequense.

After loggin into the device, I run a command though SSH and need to answer a question with the script, any ideas on how this can be done? I know this can be done with expect scripting or advanced python, tcl scripting. Any ideas on how I can acomplish this in autoit?

Share this post


Link to post
Share on other sites



Could you be more specific? Is the target computer Linux? What command are you running that's asking a question?


Gerard J. Pinzonegpinzone AT yahoo.com

Share this post


Link to post
Share on other sites

Look at this where I used plink to connect to an iMac using SSH. The function _PlinkConnect answers the question, "Store key in cache? (y/n)" by sending a command a "y".

Hopefully the example helps.

Adam

Share this post


Link to post
Share on other sites

This is to controll a cisco device, unfortunately I dont have the option of setting AAA priviledge 15 so I need to enter the enable state before I can past the commands. I can sucessfully login to the device, and then send the enable command - the device then asks for the enable password and this is where Im lost.

I will try your sugestion Adam.

Share this post


Link to post
Share on other sites

I tried the following script, but without luck ;) can you help me see what is wrong?

Call ("_PlinkConnect", "10.1.0.1", "admin", "admin")

Func _PlinkConnect($sHostName, $sUserName, $sPassword)

; $sEXE = @ScriptDir & "plink.exe"

$sEXE = "plink.exe"

If Not FileExists($sEXE) Then Return SetError(1, 0, 0)

$iPID = Run('"' & $sEXE & '" -ssh -pw ' & $sPassword & " " & $sUserName & "@" & $sHostName, @ScriptDir, @SW_HIDE, 0x1 + 0x8) ;Run SSH.EXE

If Not $iPID Then Return SetError(2, 0, 0)

$iPIDCurrent = $iPID

$sReturn = _PlinkRead($iPID) ;Check for Login Success - Prompt

If StringInstr($sReturn, "FreshRouter>") Then

_PlinkSend($iPID, "enable" & @CR)

sleep 100

_PlinkSend($iPID, $sPassword & @CR)

sleep 100

_PlinkSend($iPID, "reload" & @CR)

;~ _PlinkSend($iPID, "n" & @CR) ;For Testing.

$sReturn = _PlinkRead($iPID)

EndIf

If StringInstr($sReturn, "Access denied") Or StringInstr($sReturn, "FATAL") Then Return SetError( 3, 0, 0)

Return $iPID

EndFunc

Func _PlinkRead($iPID)

If Not $iPID Then Return SetError(1, 0, -1)

Local $sDataA

Local $sDataB

Do

$sDataB = $sDataA

Sleep(100)

$sDataA &= StdOutRead($iPID)

If @error Then ExitLoop

Until $sDataB = $sDataA And $sDataA And $sDataB

Return $sDataA

EndFunc

Share this post


Link to post
Share on other sites

There are a few things. Is plink.exe in your system PATH? Since you edited the _PlinkConnect function, instead of just using the function. The script might not be able to find plink. Also, what is then expected text for the password prompt, and other return from the commands that you are tying to run? You need to parse the return for each command, so the script knows what to do, instead of just sending commands.

Try editing this script to see if you can get it to work for you. Some of the information was taken from your script, and it is not complete since I do not know all your information, and expected returns.

Global $sPassword = "password"

Global $iPIDPlink = _PlinkConnect("10.1.0.1", "admin", "admin")
If @error Then Exit 1

_PlinkSend($iPIDPlink, "enable" & @CR)
Global $sPlinkReturn = _PlinkRead($iPIDPlink)
If StringInStr($sPlinkReturn, "Password") Then ;Expected password prompt text.
_PlinkSend($iPIDPlink, $sPassword & @CR)
$sPlinkReturn = _PlinkRead($iPIDPlink) ;Get return text from entering the password.
If StringInstr($sPlinkReturn, "Access denied") Or StringInstr($sPlinkReturn, "FATAL") Then
Exit 3
_PlinkExit($iPIDPlink)
EndIf
_PlinkSend($iPIDPlink, "reload" & @CR)
$sPlinkReturn = _PlinkRead($iPIDPlink)
EndIf
_PlinkExit($iPIDPlink)

; #FUNCTION# ====================================================================================================================
; Name ..........: _PlinkConnect
; Description ...: Use Plink to connect to a remote server using SSH.
; Syntax ........: _PlinkConnect($sHostName, $sUserName, $sPassword)
; Parameters ....: $sHostName - A string of the host server name or IP Address.
;                 $sUserName - A string of the SSH User Name.
;                 $sPassword - A string of the SSH Password.
; Return values .: Success - $iPID - the PID of the Plink session.
;                 Failure - 0, sets @error to:
;                 |1 - Plink.exe not found in @ScriptDir.
;                 |2 - Error running Plink.exe.
; Author ........: spudw2k
; Modified ......: Adam Lawrence (AdamUL)
; Remarks .......:
; Related .......: _PlinkExit
; Link ..........: http://www.autoitscript.com/forum/topic/...uter-via-ssh/page__p__910252#e
; Example .......: No
; ===============================================================================================================================
Func _PlinkConnect($sHostName, $sUserName, $sPassword)
    $sEXE = @ScriptDir & "plink.exe"
    If Not FileExists($sEXE) Then Return SetError(1, 0, 0)
$iPID = Run('"' & $sEXE & '" -ssh -pw ' & $sPassword & " " & $sUserName & "@" & $sHostName, @ScriptDir, @SW_HIDE, 0x1 + 0x8)  ;Run SSH.EXE
    If Not $iPID Then Return SetError(2, 0, 0)
    $iPIDCurrent = $iPID
    $sReturn = _PlinkRead($iPID)  ;Check for Login Success - Prompt
    If StringInstr($sReturn, "Store key in cache? (y/n)") Then
        _PlinkSend($iPID, "y" & @CR)
        $sReturn = _PlinkRead($iPID)
    EndIf
    If StringInstr($sReturn, "Access denied") Or StringInstr($sReturn, "FATAL") Then Return SetError( 3, 0, 0)
    Return $iPID
EndFunc

; #FUNCTION# ====================================================================================================================
; Name ..........: _PlinkRead
; Description ...: Read text data returned from the connected server.
; Syntax ........: _PlinkRead($iPID)
; Parameters ....: $iPID - PID returned from _PlinkConnect.
; Return values .: Success - String returned from StdOutRead of Plink.
;                 Failure - -1, sets @error to:
;                 |1 - Invaild Plink PID.
; Author ........: spudw2k
; Modified ......: Adam Lawrence (AdamUL)
; Remarks .......:
; Related .......: _PlinkSend
; Link ..........: http://www.autoitscript.com/forum/topic/...uter-via-ssh/page__p__910252#e
; Example .......: No
; ===============================================================================================================================
Func _PlinkRead($iPID)
    If Not $iPID Then Return SetError(1, 0, -1)
    Local $sDataA
    Local $sDataB
    Do
        $sDataB = $sDataA
        Sleep(100)
        $sDataA &= StdOutRead($iPID)
        If @error Then ExitLoop
    Until $sDataB = $sDataA And $sDataA And $sDataB
    Return $sDataA
EndFunc

; #FUNCTION# ====================================================================================================================
; Name ..........: _PlinkSend
; Description ...: Send text data to the connected server.
; Syntax ........: _PlinkSend($iPID, $sCmd)
; Parameters ....: $iPID - PID returned from _PlinkConnect.
;                 $sCmd - A string of the command to send.
; Return values .: Success - 1
;                 Failure - 0, sets @error to:
;                 |StdinWrite @error code.
; Author ........: spudw2k
; Modified ......: Adam Lawrence (AdamUL)
; Remarks .......:
; Related .......: _PlinkRead
; Link ..........: http://www.autoitscript.com/forum/topic/...uter-via-ssh/page__p__910252#e
; Example .......: No
; ===============================================================================================================================
Func _PlinkSend($iPID, $sCmd)
    $iChars = StdinWrite($iPID,$sCmd)
Return SetError(@error, 0, $iChars)
EndFunc

; #FUNCTION# ====================================================================================================================
; Name ..........: _PlinkExit
; Description ...: End a Plink session.
; Syntax ........: _PlinkExit($iPID)
; Parameters ....: $iPID - PID returned from _PlinkConnect.
; Return values .: Success - 1
;                 Failure - 0, sets @error to:
;                 |ProcessClose @error code.
; Author ........: spudw2k
; Modified ......: Adam Lawrence (AdamUL)
; Remarks .......:
; Related .......: _PlinkConnect
; Link ..........: http://www.autoitscript.com/forum/topic/...uter-via-ssh/page__p__910252#e
; Example .......: No
; ===============================================================================================================================
Func _PlinkExit($iPID)
    $iClosed = ProcessClose($iPID)
Return SetError(@error, 0, $iClosed)
EndFunc

Hopefully, it can get you started.

Adam

Share this post


Link to post
Share on other sites

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
Sign in to follow this  
Followers 0