Jump to content

Acquiring command prompt output during a telnet session


Recommended Posts

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
Link to comment
Share on other sites

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,
 

Link to comment
Share on other sites

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


 

Link to comment
Share on other sites

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,
 

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