Jump to content

StderrRead | StdoutRead , halting, or not receiving output...


Recommended Posts

Hi all, first of all, i have already posted a topic about this in the GUI-section, cause i thought it was a GUI-problem, but it isn't!

Here is a link:

http://www.autoitscript.com/forum/index.php?showtopic=66862

I have a problem with the StderrRead/StdoutRead, i am sure that this is the one that makes errors, cause i have tried writing to the log after i use the call, and before, and it halts!

I just don't get it why it is halting on the StderrRead, or the StdoutRead. I am sure that the server/plink is giving output, but it seams as if it's not catching it.

Plink can be fetched from:

http://the.earth.li/~sgtatham/putty/latest/x86/plink.exe

#include <Constants.au3>
#include <Date.au3>
#NoTrayIcon

Opt("TrayAutoPause", 0)
Opt("TrayMenuMode",1)   ; Default tray menu items (Script Paused/Exit) will not be shown.
Opt("TrayOnEventMode",1)
Opt("TCPTimeout", 100)
Opt("RunErrorsFatal", 0)
Opt("OnExitFunc", "CloseAll")

Global Const $settingsFile = "connection.ini"
Global Const $logFile = "log.log"
Global $actualHostName, $connected, $PID
Dim $hostname1, $hostname2, $user, $pass, $plinkDir, $plinkFile
Dim $timeOut, $numberHost
Dim $OUTERROR, $OUTPID, $waitTime
Dim $settingsitem, $aboutitem, $exititem, $startWithWindows

$hostname1 = IniRead($settingsFile, "ServerInfo", "key1", "someKey")
$hostname2 = IniRead($settingsFile, "ServerInfo", "key2", "someKey")

$user = IniRead($settingsFile, "UserInfo", "user", "default")
$pass = IniRead($settingsFile, "UserInfo", "pass", "default")

$plinkDir = IniRead($settingsFile, "FileInfo", "plinkdir", @ScriptDir)
If StringRight($plinkDir, 1) <> "\" Then $plinkDir = $plinkDir & "\"
If $plinkDir = "." Then $plinkDir = @ScriptDir & "\"
$plinkFile = IniRead($settingsFile, "FileInfo", "plinkfile", "plink.exe")
$waitTime = 200

Call("CreateLogFile")
Call("CreateMenu")
$connected = False
$numberHost = 2

While 1
    If $connected = False Then
        If $numberHost = 1 Then
            $actualHostName = $hostname1
        ElseIf $numberHost = 2 Then
            $actualHostName = $hostname2
        EndIf
        ClosePlink()
        $PID = Run(@ComSpec & " /c """ & $plinkDir & $plinkFile & """ -T -pw " & $pass & " -l " & $user & " -ssh " & $actualHostName, $plinkDir, @SW_HIDE, $STDIN_CHILD + $STDOUT_CHILD + $STDERR_CHILD)
        $timeOut = ProcessWait($PID, 1500)
    EndIf
    Call("CheckProcessPlink")
    Sleep($waitTime)
WEnd

Func CheckProcessPlink()
    $OUTERROR = ReadOut(1)
    Call("WriteLog", $OUTERROR)
    If StringInStr($OUTERROR, "The server's host key") > 0 Then
        $connected = True
        StdinWrite($PID, "n")
        Call("WriteLog", $OUTERROR & " -> 'n'")
    ElseIf StringInStr($OUTERROR, "FATAL ERROR") > 0 Then
        If $numberHost = 1 Then
            $numberHost = 2
        Else
            $numberHost = 1
        EndIf
        Call("WriteLog", "Switching host")
    Else
        Call("WriteLog", $OUTERROR)
    EndIf
    $OUTPID = ReadOut(0)
    Call("WriteLog", $OUTPID)
    If StringInStr($OUTPID, "You are already logged on") Then
        $connected = False
        Sleep(120000)
    ElseIf StringInStr($OUTPID, "Hello " & $user) Then
        $connected = True
    ElseIf StringInStr($OUTPID, "Unable ") Then
        $connected = False
    ElseIf StringInStr($OUTPID, "Using username ") Then
        $connected = True
    EndIf
EndFunc   ;==>CheckProcessPlink

Func ReadOut($method)
    Local $out
    $out = ""
    SetError(0,0,0)
    While 1
        If $method = 1 Then
            If StringLen(StderrRead($PID, 0, True)) > 0 Then 
                $out &= StderrRead($PID)
            Else
                ExitLoop
            EndIf
        ElseIf $method = 0 Then
            If StringLen(StdoutRead($PID, 0, True)) > 0 Then 
                $out &= StdoutRead($PID)
            Else
                ExitLoop
            EndIf
        EndIf
        If @error <> 0 Then 
            Return $out
        EndIf
    WEnd
    Return $out
EndFunc

Func CreateMenu()
    TraySetState()
    $settingsitem   = TrayCreateItem("Settings")
    TrayItemSetOnEvent($settingsitem,"SettingsEvent")
    TrayCreateItem("")
    $exititem       = TrayCreateItem("Exit")
    TrayItemSetOnEvent($exititem,"ExitEvent")
EndFunc

Func SettingsEvent()
    RunWait("notepad.exe " & $settingsFile ,"" ,@SW_MAXIMIZE)
EndFunc

Func ExitEvent()
    TraySetState(2)
    CloseAll()
EndFunc

Func CreateLogFile($fileName = $logFile)
    Local $file
    $file = FileOpen($fileName, 2)
    If $file = -1 Then
        MsgBox(0, "Error in the making of log-file...", "You have tried to make the file '" & $logFile & "', but there has been an error." & @CRLF & @CRLF & "Please try again.")
    Else
        FileWriteLine($file, "# Log-file" & @CRLF & "# Date: " & _NowDate() & @CRLF & "# Creationtime: " & _NowTime())
    EndIf
    FileClose($file)
EndFunc   ;==>CreateLogFile

Func WriteLog($str, $fileName = $logFile)
    Local $file
    $file = FileOpen($fileName, 1)
    FileWriteLine($file, _NowDate() & " -- " & _NowTime() & ": " & $str)
    FileClose($file)
EndFunc   ;==>WriteLog

Func ClosePlink()
    Local $p
    $p = 0
    $p = ProcessExists($plinkFile)
    If $p > 0 Then ProcessClose($p)
EndFunc   ;==>ClosePlink

Func CloseAll()
    ClosePlink()
    Exit
EndFunc   ;==>CloseAll

Hope you guys can help me!

Link to comment
Share on other sites

Try it with the latest 3.2.11.x Beta. There was a bug related to reading console streams in 3.2.10.0, I believe.

:)

Valuater's AutoIt 1-2-3, Class... Is now in Session!For those who want somebody to write the script for them: RentACoder"Any technology distinguishable from magic is insufficiently advanced." -- Geek's corollary to Clarke's law
Link to comment
Share on other sites

Hi again... First of all thanks for the help!!

I have tried the beta without any luck.. It seams as if it doesn't read the total contents still. I know that it gives an output (according to what i search for in the script), but it doesn't pick it up...

If i do plink in verbose mode (-v), the Stdoutread is capable of reading the lot, but still not the things i am interested in!

It simply leaves out that part...

I was wondering whether you could gather the same information that netstat -b (or tvpvcon from Sysinternals) via autoit system calls, in that way i would get out of the stderrread/stdoutread. I am only interrested in seing if the connection has been established...

Also the last way of getting this to work, is to write the information to a file, (plink -"options" > log.log)...

But the massive consecutive readings from a file is undesirable...

Thanks again for any help!!!

Link to comment
Share on other sites

Moin,

you have to query @error next of StdOutRead() or it will never return, read the helpfile slowly and the comments in the example.

While 1
        $line = StdoutRead($PID)
        If @error Then  ; EOF or nothing to read from cout, don't forget this line, or use the 'peek' parameter !
            ExitLoop
        Else
            ;;;
WEnd

Greetz

Greenhorn

Link to comment
Share on other sites

I am thinking about making an netstat > log and read from that file... But doesn't anybody have a suggestion?

This solution is not very good programming!!!

Or is there a way to check the connection via dll-calls?

I have no experience with dll-calls, so any help would be appreciable!!!

Link to comment
Share on other sites

Hi again...

Thanks for the info, but it doesn't change anything, it still doesn't catch the "Logged on.." or the "FATAL ERROR"...

Any suggestions?

take a look at my script which uses plink ... here

of course i have progressed somewhat since then ...

you need to PEEK at the values, then read them if there is something there ... that way things are not held up waiting for stdout or stderr ... it was a things that i got caught on too ... if you don't like the way i did it ... search for plink.exe, you will find other examples

PM me if you still have troubles (don't frequent this forum all that often)

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

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...