Jump to content

PuTTy (plink.exe) issue


Recommended Posts

Hi guys,

I had a script that worked fine for about a year. It used plink.exe which is "a command line interface to the PuTTy backend" to allow me to automate using ssh to do some light maintenance on some UNIX servers in our office. Yesterday someone asked me to tweak the script a bit, and when I was testing the script it all basically went to hell. I isolated the issue down to an issue retrieving the output from the run command that does the sending, and I am seeing some weird results.

Can someone who has access to a server running ssh download a copy of plink.exe from the link above (if you dont have it already) and confirm/dispute my findings?

#include <Constants.au3>

; This should sucessfully show a listing of files
ConsoleWrite("+++++++++++++++++++++" & @CRLF)
$ret = _MyRun("dir C:\")
ConsoleWrite($ret & @CRLF & "+++++++++++++++++++++" & @CRLF)

; This should sucessfully return syntax information from plink.exe
ConsoleWrite("+++++++++++++++++++++" & @CRLF)
$ret = _MyRun("C:\PuTTY\plink.exe")
ConsoleWrite($ret & @CRLF & "+++++++++++++++++++++" & @CRLF)

; This is supposed get a directory listing from the remote server
; instead I get nothing in the return, and the console error shows:
; 'Unable to read from standard input: The handle is invalid.'
ConsoleWrite("+++++++++++++++++++++" & @CRLF)
$ret = _MyRun("C:\PuTTy\plink.exe -pw password username@server ls")
ConsoleWrite($ret & @CRLF & "+++++++++++++++++++++" & @CRLF)

Func _MyRun($sCommand)
    $cmdreturn = ""
    Local $stream = Run(@ComSpec & " /c " & $sCommand, @SystemDir, @SW_HIDE, $STDOUT_CHILD)
    Local $line
    While 1
        $line = StdoutRead($stream)
        If @error Then ExitLoop
        $cmdreturn &= $line
    WEnd
    Return $cmdreturn
EndFunc   ;==>_MyRun

I can cut and paste "C:\PuTTy\plink.exe -pw password username@server ls" into a DOS prompt and it works perfectly.

The old version of the script (compiled with an earlier version of AutoIt) still works perfectly for the functions it had at the time.

However I cant seam to make the code work with v3.3.0.0

[u]Helpful tips:[/u]If you want better answers to your questions, take the time to reproduce your issue in a small "stand alone" example script whenever possible. Also, make sure you tell us 1) what you tried, 2) what you expected to happen, and 3) what happened instead.[u]Useful links:[/u]BrettF's update to LxP's "How to AutoIt" pdfValuater's Autoit 1-2-3 Download page for the latest versions of Autoit and SciTE[quote]<glyph> For example - if you came in here asking "how do I use a jackhammer" we might ask "why do you need to use a jackhammer"<glyph> If the answer to the latter question is "to knock my grandmother's head off to let out the evil spirits that gave her cancer", then maybe the problem is actually unrelated to jackhammers[/quote]

Link to comment
Share on other sites

Try to use Putty directly, not inside the DOS prompt:

Local $stream = Run($sCommand, @SystemDir, @SW_HIDE, $STDOUT_CHILD)

*GERMAN* [note: you are not allowed to remove author / modified info from my UDFs]My UDFs:[_SetImageBinaryToCtrl] [_TaskDialog] [AutoItObject] [Animated GIF (GDI+)] [ClipPut for Image] [FreeImage] [GDI32 UDFs] [GDIPlus Progressbar] [Hotkey-Selector] [Multiline Inputbox] [MySQL without ODBC] [RichEdit UDFs] [SpeechAPI Example] [WinHTTP]UDFs included in AutoIt: FTP_Ex (as FTPEx), _WinAPI_SetLayeredWindowAttributes

Link to comment
Share on other sites

Try to use Putty directly, not inside the DOS prompt:

Local $stream = Run($sCommand, @SystemDir, @SW_HIDE, $STDOUT_CHILD)

I get the same results

[u]Helpful tips:[/u]If you want better answers to your questions, take the time to reproduce your issue in a small "stand alone" example script whenever possible. Also, make sure you tell us 1) what you tried, 2) what you expected to happen, and 3) what happened instead.[u]Useful links:[/u]BrettF's update to LxP's "How to AutoIt" pdfValuater's Autoit 1-2-3 Download page for the latest versions of Autoit and SciTE[quote]<glyph> For example - if you came in here asking "how do I use a jackhammer" we might ask "why do you need to use a jackhammer"<glyph> If the answer to the latter question is "to knock my grandmother's head off to let out the evil spirits that gave her cancer", then maybe the problem is actually unrelated to jackhammers[/quote]

Link to comment
Share on other sites

I think, putty wnats the STDIN-Handle, too, so use

Func _MyRun($sCommand)
    $cmdreturn = ""
    Local $stream = Run($sCommand, @SystemDir, @SW_HIDE, $STDOUT_CHILD+$STDIN_CHILD)
    Local $line
    While 1
        $line = StdoutRead($stream)
        If @error Then ExitLoop
        $cmdreturn &= $line
    WEnd
    StdinWrite($stream,"")
    StdioClose($stream)
    Return $cmdreturn
EndFunc   ;==>_MyRun
Edited by ProgAndy

*GERMAN* [note: you are not allowed to remove author / modified info from my UDFs]My UDFs:[_SetImageBinaryToCtrl] [_TaskDialog] [AutoItObject] [Animated GIF (GDI+)] [ClipPut for Image] [FreeImage] [GDI32 UDFs] [GDIPlus Progressbar] [Hotkey-Selector] [Multiline Inputbox] [MySQL without ODBC] [RichEdit UDFs] [SpeechAPI Example] [WinHTTP]UDFs included in AutoIt: FTP_Ex (as FTPEx), _WinAPI_SetLayeredWindowAttributes

Link to comment
Share on other sites

Thank you sir :) seams to do the trick

[u]Helpful tips:[/u]If you want better answers to your questions, take the time to reproduce your issue in a small "stand alone" example script whenever possible. Also, make sure you tell us 1) what you tried, 2) what you expected to happen, and 3) what happened instead.[u]Useful links:[/u]BrettF's update to LxP's "How to AutoIt" pdfValuater's Autoit 1-2-3 Download page for the latest versions of Autoit and SciTE[quote]<glyph> For example - if you came in here asking "how do I use a jackhammer" we might ask "why do you need to use a jackhammer"<glyph> If the answer to the latter question is "to knock my grandmother's head off to let out the evil spirits that gave her cancer", then maybe the problem is actually unrelated to jackhammers[/quote]

Link to comment
Share on other sites

  • 1 month later...

I inserted this line to be able to have the output. It is not OK.

$line = StdoutRead($stream)
        If @error Then ExitLoop
        
                MsgBox(0, "STDOUT read:", $line)
        
                $cmdreturn &= $line
    WEnd
Edited by mentosan
Link to comment
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
 Share

  • Recently Browsing   0 members

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