Jump to content
Sign in to follow this  
Sethonator

Reading text out of the body of terminal sessions

Recommended Posts

Sethonator

Hey...

I have the problem, that AutoIT couldn't read the content of a terminal session like putty or dos-telnet.

This issue is often called in this board, but there were no answers, I can live with...

I want to write a script, that should wait, until a process in the terminal window is finished. So AutoIT should wait for example until the word "Login" is shown in the window. If the word "Login" is shown, the script can go on.....

I tried it with wingettext, but there are no informations...

I hope you know, what I mean and I excuse for my terrible english....

Thanks....

René

Share this post


Link to post
Share on other sites
Sethonator

use the clipboard:

1. mark all ctrl+a or other keystrokes for your terminal session

2. copy to clipboard ctrl+c or other keystrokes for your terminal session

3. If stringinStr(clipget(),"Login") then msgbox(0,"","found Login")

How can I mark the hole screen? WIth Telnet of Windows I can't mark everything. Or are there special settings?

Share this post


Link to post
Share on other sites
enaiman

The easiest way to do this is to use the "log" ability of telnet or other similar software.

Once telnet logs everything in a text file you can read that file (the last line is the significant one because it holds the prompt (login: in this case)

I use to work alot with network switches and I use frequently scripts which logs into them and do config changes and other stuff and I need to know permanently what is returned.

I'm using Tera Term and it is working great; I don't know if you're familiar with but you might take a look.

So: the answer is - get your telnet-like software to put everything in a text file and work with it.


SNMP_UDF ... for SNMPv1 and v2c so far, GetBulk and a new example script

wannabe "Unbeatable" Tic-Tac-Toe

Paper-Scissor-Rock ... try to beat it anyway :)

Share this post


Link to post
Share on other sites
Sethonator

Hey,

I tried it with shortcuts....

When the console is open, then write:

send ( "!{SPACE}")

send ( "B")

send ( "A")

sleep (3000)

send ( "!{SPACE}")

send ( "B")

send ( "k")

First you mark everything, and after the "sleep" command the whole screen of the box will be copied.

Share this post


Link to post
Share on other sites
bdb4269

I was having a hard time finding easy to understand info (for a newbie) for how to send and receive stuff from a SSH session, so I decided I would post this for other newb's like me to find.

This is how I ended up handling it. I used plink.exe (command line version of PuTTY)

NOTE: This is by no means, necessarily the best way. But easy to understand and use for newb's. IMHO

;this runs plink in a command window, and provides a handle that can be used for Stdin, Stdout, and Stderr 
$plinkhandle = Run(@comspec & ' /c x:\plink.exe -ssh 192.168.202.231 ','',@SW_HIDE,7)


$username = InputBox("username","Enter username")
$password = InputBox("password","Enter password",'','*')

;waits for "login"
While 1
    $text = StdoutRead($plinkhandle)
    $oktogo = StringRegExp($text,".*login.*")
    If $oktogo = 1 Then ExitLoop
Wend

;For some reason I had to put a space after each because the last character was being cut off.  
;But only on the login part, after that it worked fine with no extra space. 

StdinWrite($plinkhandle, $username & " " & @CR)

;waits for "password"
While 1
    $text = StdoutRead($plinkhandle)
    $oktogo = StringRegExp($text,".*password.*")
    If $oktogo = 1 Then ExitLoop
Wend

;For some reason I had to put a space after each because the last character was being cut off. 
;But only on the login part, after that it worked fine with no extra space. 

StdinWrite($plinkhandle, $password & " " & @CR)


;I have some stuff in my login script that I normall press enter (to accept default) a few times to get to my actual prompt
;so this block is just hitting enter until I get to /home/username

;note: if you use a while block that keeps Stdoutread'ing it splits up all the output 
;If you for instance wanted to see all what was displayed during the while block you 
;could un-comment the lines of code below - to pile it into $fulltext, each time you read

;Dim $fulltext
While 1
    StdinWrite($plinkhandle, @CR)                            
    Sleep(100)
    $text = StdoutRead($plinkhandle)
;$fulltext = $fulltext & @CRLF & $text
    $oktogo = StringRegExp($text,".*home.*")
    If $oktogo = 1 Then ExitLoop
Wend
;MsgBox(0, "Display", $Fulltext)


;I had to set terminal to "dumb" to make it work well

StdinWrite($plinkhandle,"setenv TERM dumb" & @CR)

While 1
       $text = StdoutRead($plinkhandle)
    $oktogo = StringRegExp($text,".*home.*")
       If $oktogo = 1 Then ExitLoop
Wend

StdinWrite($plinkhandle,"cd ./scripts" & @CR)
Sleep(500)

$text = StdoutRead($plinkhandle) 
;I do this to "clear" the output - otherwise, when I do it below 
;to see the output of my script - it includes the typing of, and the output from the cd ./scripts too. 


StdinWrite($plinkhandle,"script.to.run.csh" & @CR)
Sleep(1000) 

;As a side note I noticed, I needed to sleep for about 100-1000 after StdinWrite before using StdoutRead
;(even for something nearly instant like "cd ./scripts" and having a while block to wait for the prompt)
;or else it did not catch the output from that latest command. 

$text = StdoutRead($plinkhandle)
MsgBox(0, "Output from script.to.run.csh", $text)

;note: since I just sleep'd instead of a while loop Stdoutread'ing all the 
;mutli line output "piled up" and could be ready with a single Stdoutread. 
;This will also include the prompt your left at after the command runs.  
;I don't yet know easy wat to cut this off, because I dont care enough for my purposes
;to figure it out

StdinWrite($plinkhandle,"logout" & @CR)


;It's also worth noting that 
;$text = StdoutRead($plinkhandle)
;when there is no new data in Stdout seems to lock-up the script
Edited by bdb4269

Share this post


Link to post
Share on other sites
Armand

so as i understood from this post, there is no way to read the output of Telnet directly through AU3 if the TELNET is ran hidden ?!


[u]My Au3 Scripts:[/u]____________(E)Lephant, A Share download manager (RS/MU etc)Http1.1 Console, The Ez Way!Internet Reconnection Automation Suite & A Macro Recording Tool.SK's Alarm Clock, Playing '.MP3 & .Wav' Files._________________Is GOD a mistake of the Humanity Or the Humanity is a mistake of GOD ?!

Share this post


Link to post
Share on other sites
JDaus

so as i understood from this post, there is no way to read the output of Telnet directly through AU3 if the TELNET is ran hidden ?!

You CAN use STDOUT & STDIN to do what you ask (plink can be told to use telnet, of you can probably use telnet.exe provided with windows)

i have a different version of the PLINK script that creates a GUI, that contains three things

  • A readonly edit window for seeing STDOUT & STDERR
  • A SSH button, for starting SSH session (or telnet) using plink.exe
  • A Exit button to close the apps at any time

it is by no means complete, but is another example of STDOUT and STDIN for newbees (like myself)

it is attached for easy download, and below for easy searchability

http://sf.net/projects/vnc2me/ for my work in progress (end result will function similar to showmypc.com)

#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()
    StdinWrite($ourProcess, "exit" & @CR)
    Sleep(500)
    ProcessClose("jdsplink.exe")
    Exit
EndFunc   ;==>Vnc2MeExit

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

To all those people who will comment on my newbee code, I AM a NEWBEE ... so give me a break, and if you can do better, please let me know, or help me with my project on SF.net/projects/vnc2me/

testing_stdout5.au3


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  

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.