Jump to content
Sign in to follow this  
JDaus

stdin and stdout with plink.exe (SOLVED)

Recommended Posts

I am having troubles with getting a plink.exe app to start using stdin, stdout & stderr.

I have solved this problem and provide a basic (sort of) script to assist those that have problems with plink.exe (putty.exe) or any other app with STDOUT, STDIN & STDERR

Found a post that led me to find the problem HERE ...

the problem was @CR ... that simple ...

the following things need to be edited to get it to work:

  • rename "plink.exe" to jdsplink.exe (or change the code @ line 124 & 169)
  • Change "somehost.com" (line 124) to your SSH Host address
  • Change "@SW_MINIMIZE" to "@SW_HIDE" when finished
#Region ;**** Directives created by AutoIt3Wrapper_GUI ****
#AutoIt3Wrapper_outfile=testing stdout5.exe
#AutoIt3Wrapper_Run_Tidy=y
#EndRegion ;**** Directives created by AutoIt3Wrapper_GUI ****
; JDaus created this script to get around some limitations found while working with putty
; It is posted on the autoitscript.com website in the hope that it will be useful to others
; This script forms the basis of part of the Vnc2Me project (http://sf.net/projects/vnc2me/


; this script was derived from the following post (among others)
; http://www.autoitscript.com/forum/index.php?s=&showtopic=12828&view=findpost&p=88305
; A script for AutoIt3. Jon is The Man.
; Script generated by AutoBuilder 0.5 Prototype. CyberSlug rocks.
; Edited with SciTE and Tidy. Go JdeB.

#include <GuiConstants.au3>

Dim $ourProcess
Dim $username
Dim $password
Dim $loop


GUICreate("STDIO Window", 425, 322, (@DesktopWidth - 425) / 2, (@DesktopHeight - 362) / 2, $WS_OVERLAPPEDWINDOW + $WS_VISIBLE + $WS_CLIPSIBLINGS)

; Create a read-only edit control
$eOutput = GUICtrlCreateEdit("", 0, 10, 423, 250, BitOR($ES_WANTRETURN, $WS_VSCROLL, $WS_HSCROLL, $ES_AUTOVSCROLL, $ES_AUTOHSCROLL, $ES_READONLY))
; Ping spawn button
$bSSH = GUICtrlCreateButton("SSH", 260, 285, 60, 20)
; Notepad spawn button
;$bNotepad = GuiCtrlCreateButton("Notepad", 180, 285, 60, 20)
; Exit button
$bExit = GUICtrlCreateButton("Exit", 340, 285, 60, 20)

; Show the GUI window
GUISetState()
JDs_debug("Vnc2Me - GUI Starting")

; Loop and update the edit control unitl we hit EOF (and get an error)
While 1
    ; We assign the process ID of ping to $ourProcess below...
    If $ourProcess Then
        ; Calling StdoutRead like this returns the characters waiting to be read
        $ReadCharsWaiting = StdoutRead($ourProcess, 0, 1)
        ; Calling StderrRead like this returns the characters waiting to be read
        If @error = -1 Then
            JDs_debug("Vnc2Me - STDOUT unable to be read")
            ; Set $ourProcess to zero so we don't try to read nothing...
            $ourProcess = 0
            MsgBox(0, "App Exited", "Process has exited...")
            ; ContinueLoop means "don't exit the While loop, just start over without doing anything else"
            ContinueLoop
        EndIf
        $ErrCharsWaiting = StderrRead($ourProcess, 0, 1)
        ; If there was an error reading, the most likely cause us that the child process has quit...
        If @error = -1 Then
            JDs_debug("Vnc2Me - STDERR unable to be read")
            ; Set $ourProcess to zero so we don't try to read nothing...
            $ourProcess = 0
            MsgBox(0, "App Exited", "Process has exited...")
            ; ContinueLoop means "don't exit the While loop, just start over without doing anything else"
            ContinueLoop
        EndIf

        ; Since we got here there was no error, but were there characters to be read?
        If $ReadCharsWaiting Then
            ; Read all available
            $currentRead = StdoutRead($ourProcess)
            $Vnc2MeReadUsername = StringRegExp($currentRead, ".*ogin.*")
            $Vnc2MeReadPassword = StringRegExp($currentRead, ".*assword.*")

            If $Vnc2MeReadUsername = 1 Then
                JDs_debug("Vnc2Me - AUTH - 'login' detected")
                Vnc2MeUsername()
                ContinueLoop
            ElseIf $Vnc2MeReadPassword = 1 Then
                JDs_debug("Vnc2Me - AUTH - 'password' detected")
                Vnc2MePassword()
                ContinueLoop
            Else
                ; Add what we read to the editbox
                GUICtrlSetData($eOutput, $currentRead, 1)
                JDs_debug("Vnc2Me - STDOUT updates fed to GUI")
            EndIf
            Sleep(100)
        ElseIf $ErrCharsWaiting Then
            ; Read all available
            $currentErr = StderrRead($ourProcess)
            $Vnc2MeErrHostKey = StringRegExp($currentErr, ".*host key is not cached.*")
            If $Vnc2MeErrHostKey = 1 Then
                JDs_debug("Vnc2Me - STDERR Host key no cached")
                Vnc2MeAddHostKey()
                ;               GUICtrlSetData($eOutput, $currentErr, 1)
                ContinueLoop
            Else
                GUICtrlSetData($eOutput, $currentErr, 1)
                JDs_debug("Vnc2Me - STDERR updates fed to GUI")
            EndIf
        EndIf ; => no program output waiting.
    Else ; => $ourProcess not exist.
        JDs_debug("Vnc2Me - SSH process not started")
        Sleep(200)
    EndIf ; => End $ourProcess


    ; Get any messages from the GUI
    $msg = GUIGetMsg()
    Select
        Case $msg = $GUI_EVENT_CLOSE
            ; X button on window was clicked
            JDs_debug("Vnc2Me - GUI exiting (windows closed)")
            Vnc2MeExit()
        Case $msg = $bExit
            ; Exit button was clicked
            JDs_debug("Vnc2Me - GUI exiting (exit button)")
            Vnc2MeExit()
        Case $msg = $bSSH
            ; SSH button was clicked
            JDs_debug("Vnc2Me - SSH button pressed")
            ; Run child process and provide console i/o for it.
            ;   1 ($STDIN_CHILD) = Provide a handle to the child's STDIN stream
            ;   2 ($STDOUT_CHILD) = Provide a handle to the child's STDOUT stream
            ;   4 ($STDERR_CHILD) = Provide a handle to the child's STDERR stream
            $ourProcess = Run("jdsplink.exe -C -L 25501:127.0.0.1:5500 -R 5500:127.0.0.1:5900 somehost.com -v", @ScriptDir, @SW_MINIMIZE, 7)
            JDs_debug("Vnc2Me - SSH process started")
            ;       Case $msg = $bNotepad
            ;           JDs_debug("Vnc2Me - Notepad button pressed")
            ;           ; Notepad button was clicked
            ;           Run("notepad.exe", @SystemDir)
        Case Else
            ;;;
    EndSelect
WEnd
JDs_debug("Vnc2Me - ")

Func Vnc2MeUsername($username = "")
    ;if $username not passed in func call, ask for it.
    If $username = "" Then
        $username = InputBox("username", "Enter username")
        If @error = 1 Then
            Exit
        EndIf
    EndIf
    ;writes "login"
    JDs_debug("Vnc2Me - AUTH - Passing username")
    StdinWrite($ourProcess, $username & " " & @CR)
EndFunc   ;==>Vnc2MeUsername

Func Vnc2MePassword($password = "")
    ;if $password not passed in func call, ask for it.
    If $password = "" Then
        $password = InputBox("password", "Enter password for " & $username, '', '*')
        If @error = 1 Then
            Exit
        EndIf
    EndIf
    ;writes "password"
    JDs_debug("Vnc2Me - AUTH - Passing password")
    StdinWrite($ourProcess, $password & " " & @CR)
EndFunc   ;==>Vnc2MePassword

Func Vnc2MeAddHostKey()
    ;Add Host key to knownhosts
    JDs_debug("Vnc2Me - STDERR found")
    $msgbox = MsgBox(4, "The host is not cached", "This Host is not known, do you want to add it to known hosts ???")
    If $msgbox = 6 Then
        StdinWrite($ourProcess, "y " & @CR)
    EndIf
EndFunc   ;==>Vnc2MeAddHostKey

Func Vnc2MeExit()
    GUICtrlSetData($eOutput, @CRLF & "Exiting" & @CRLF, 1)
    StdinWrite($ourProcess, "exit" & @CR)
    Sleep(2000)
    ProcessClose("jdsplink.exe")
    Exit
EndFunc   ;==>Vnc2MeExit

Func JDs_debug($msg)
    DllCall("kernel32.dll", "none", "OutputDebugString", "str", $msg)
EndFunc   ;==>JDs_debug

testing_stdout5.au3

Edited by JDaus

ask a silly question and remain a fool for 5 minutes...don't ask, and remain a fool for life__JD - YTS | VNC2Me - Secure remote Desktop Support Solutions

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  

×
×
  • Create New...