eibwen

Acquiring command prompt output during a telnet session

6 posts in this topic

#1 ·  Posted

Hi, 

I'm trying to capture the command prompt's feedback during a telnet session, so that the system know what's happening. But it doesn't work like how i hope for. 
I probably have wrong concept about the dos. So is there a proper way to acquire the cmd lines during a telnet session?
Thank you.

local $server = ""

$iPid = Run(@ComSpec & " /k telnet " & $server, "", "", $STDOUT_CHILD)

Func readcmd($PIDs) ; read cmd lines with stdout
   local $datas
   
   while 1
      $line = StdoutRead($iPid)
      If @error Then ExitLoop
      If Not $line = "" Then $datas=$datas & $line
   WEnd
    return $datas
EndFunc

Share this post


Link to post
Share on other sites



#2 ·  Posted (edited)

Maybe:
 

Local $iServer = "xxx.xxx.xxx.xxx"
Local $iCommand = "telnet " & $iServer
MsgBox(0, "", _StreamCMD($iCommand))


Func _StreamCMD($sCMD, $WorkingDir = Default, $iStreamType = Default, $iShowFlag = Default, $iDelay = Default)
    If StringStripWS($sCMD, 8) = "" Then Return ""
    If $WorkingDir = Default Then $WorkingDir = @SystemDir ;@WindowsDir & '\System32'
    If $iStreamType = Default Then $iStreamType = $STDERR_CHILD + $STDOUT_CHILD
    If $iShowFlag = Default Then $iShowFlag = False
    If $iDelay = Default Then $iDelay = 250
    ConsoleWrite("! Execute: " & $sCMD & @CRLF)
;~  Local $sTMP = '', $sSTD = '', $sCOM = '"' & @WindowsDir & '\System32\cmd.exe"' & ' /c ' & $sCMD
    Local $sTMP = '', $sSTD = '', $sCOM = @ComSpec & ' /c ' & $sCMD
    Local $iWin = $iShowFlag ? @SW_SHOW : @SW_HIDE
    Local $iPID = Run($sCOM, $WorkingDir, $iWin, $iStreamType)
    While 1
        $sTMP = StdoutRead($iPID, False, False)
        If @error Then ExitLoop
        If $sTMP <> "" Then
            $sTMP = StringReplace($sTMP, @CR & @CR, '')
            $sSTD &= $sTMP
            ConsoleWrite($sTMP)
            Sleep($iDelay)
        EndIf
    WEnd
    While 1
        $sTMP = StderrRead($iPID, False, False)
        If @error Then ExitLoop
        If $sTMP <> "" Then
            $sTMP = StringReplace($sTMP, @CR & @CR, '')
            $sSTD &= $sTMP
            ConsoleWrite($sTMP)
            Sleep($iDelay)
        EndIf
    WEnd
    If $sSTD <> "" Then ConsoleWrite(@CRLF)
    Return SetError(@error, @extended, $sSTD)
EndFunc   ;==>_StreamCMD
; * -----:|  Dao Van Trong - TRONG.WIN

 

Edited by You

Regards,
 

Share this post


Link to post
Share on other sites

#3 ·  Posted

Hi @You,

Thanks for helping, is the script workable on your side?
I tried with minor changes, but it ends with blank output.
The WinAPIFiles is used for the purpose of avoiding the redirection to the cmd in SysWOW64, as telnet only installed in System32. ( Could this be the issue:blink: ?)

#include <constants.au3>
#include <WinAPIFiles.au3>

Local $iServer = "XX.XX.XX.XX"
Local $iCommand = "telnet " & $iServer
MsgBox(0, "whyT.T", _StreamCMD($iCommand))


Func _StreamCMD($sCMD, $WorkingDir = Default, $iStreamType = Default, $iShowFlag = Default, $iDelay = Default)
    If StringStripWS($sCMD, 8) = "" Then Return ""
    If $WorkingDir = Default Then $WorkingDir = @SystemDir ;@WindowsDir & '\System32'
    If $iStreamType = Default Then $iStreamType = $STDERR_CHILD + $STDOUT_CHILD
    If $iShowFlag = Default Then $iShowFlag = False
    If $iDelay = Default Then $iDelay = 250
    ConsoleWrite("! Execute: " & $sCMD & @CRLF)
;~  Local $sTMP = '', $sSTD = '', $sCOM = '"' & @WindowsDir & '\System32\cmd.exe"' & ' /c ' & $sCMD
    Local $sTMP = '', $sSTD = '', $sCOM = @ComSpec & ' /c ' & $sCMD
    Local $iWin = $iShowFlag ? @SW_SHOW : @SW_HIDE
    _WinAPI_Wow64EnableWow64FsRedirection(False); Stop the redirection to 64 bit process when running in 32bit.
    Local $iPID = Run($sCOM, $WorkingDir, $iWin, $iStreamType)
    _WinAPI_Wow64EnableWow64FsRedirection(True); re-enable the 64 bit process redirection.
    While 1
        $sTMP = StdoutRead($iPID, False, False)
        If @error Then ExitLoop
        If $sTMP <> "" Then
            $sTMP = StringReplace($sTMP, @CR & @CR, '')
            $sSTD &= $sTMP
            ConsoleWrite($sTMP)
            Sleep($iDelay)
        EndIf
    WEnd
    While 1
        $sTMP = StderrRead($iPID, False, False)
        If @error Then ExitLoop
        If $sTMP <> "" Then
            $sTMP = StringReplace($sTMP, @CR & @CR, '')
            $sSTD &= $sTMP
            ConsoleWrite($sTMP)
            Sleep($iDelay)
        EndIf
    WEnd
    If $sSTD <> "" Then ConsoleWrite(@CRLF)
    Return SetError(@error, @extended, $sSTD)
EndFunc   ;==>_StreamCMD
; * -----:|  Dao Van Trong - TRONG.WIN


 

Share this post


Link to post
Share on other sites

#4 ·  Posted (edited)

i think it not work for telnet command, try this script:
 

#include <constants.au3>
#include <WinAPIFiles.au3>
#include <GUIConstants.au3>

If @OSArch = "X64" And Not @AutoItX64 Then _WinAPI_Wow64EnableWow64FsRedirection(False); Stop the redirection to 64 bit process when running in 32bit.
Local $iCheck = FileExists(@WindowsDir & '\System32\telnet.exe')
If Not $iCheck Then Exit MsgBox(48, "Error", "Telnet not installed!")

Opt("GUIOnEventMode", 1)

Global $iServer = "rainmaker.wunderground.com"
Global $iCommand = "telnet " & $iServer

Global $Main_GUI = GUICreate("Embed Command Line Prompt", 550, 300, 10, 10)
GUISetOnEvent($GUI_EVENT_CLOSE, "Quit")
GUIRegisterMsg(0xF, "WM_PAINT")

Global $iCmd_PID = Run('"' & @WindowsDir & '\System32\cmd.exe"' & ' /k ' & $iCommand, "", @SW_HIDE)
ProcessWait($iCmd_PID)

Global $Embed_hWnd = _GetHWndByPID($iCmd_PID)
WinMove($Embed_hWnd, "", -2, -23, 549, 342)
WinSetState($Embed_hWnd, "", @SW_SHOWMINIMIZED)
GUISetState(@SW_SHOW, $Main_GUI)

DllCall("user32.dll", "hwnd", "SetParent", "hwnd", $Embed_hWnd, "hwnd", $Main_GUI)


While 1
    Sleep(100)
    If WinActive($Main_GUI) Then WinActivate($Embed_hWnd)
WEnd

Func Quit()
    ProcessClose($iCmd_PID)
    Exit
EndFunc   ;==>Quit

Func _GetHWndByPID($iPID)
    Local $aWinList = WinList()

    For $i = 1 To UBound($aWinList) - 1
        If WinGetProcess($aWinList[$i][1]) = $iPID Then Return $aWinList[$i][1]
    Next

    Return 0
EndFunc   ;==>_GetHWndByPID

Func WM_PAINT($hWnd, $Msg, $wParam, $lParam)
    DllCall("user32.dll", "int", "InvalidateRect", "hwnd", $hWnd, "ptr", 0, "int", 0)
EndFunc   ;==>WM_PAINT

 

Edited by You

Regards,
 

Share this post


Link to post
Share on other sites

#5 ·  Posted

Sadly...
What could be the issue?
I'm clueless, and don't know what to look for :wacko:

Capture.PNG

Share this post


Link to post
Share on other sites

#6 ·  Posted

.... also have a look to following link, seems native telnet have problems on I/O redirections. I'm using this http://consoletelnet.sourceforge.net/ instead

 


small minds discuss people average minds discuss events great minds discuss ideas.... and use AutoIt....

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