Sign in to follow this  
Followers 0
PranavS

Creating a Telnet session with Console Telnet STDIN/STDOUT

23 posts in this topic

Hi everyone!

I'm very new to Autoit, so please bare with me. For a project I'm doing, I need to access another system on my network via telnet to run some executables (command-line programs). I've been researching the forums and google, and I feel like I'm very close to something that works. The problem is that I repeatedly get weird data from the server, and I don't know what to do with it.

I've been trying to use Console Telnet's STDIN/STDOUT functions to access the server. I've also tried opening a socket with TCP functions, but the data I get back from the server looks like "0xFFFD25FFFB01FFFB03FFFD27FFFD1FFFFD00FFFB00" and I have no clue what that means.

Anywho, here is the code I've been using with Console Telnet (the msgboxes are merely for troubleshooting). I owe this code in big part to BinaryBrother and DaveF from this post.

; Script Start

Dim $ourTelnet, $ourRead, $totalRead = ""
EnvSet("TELNET_REDIR", "1")
$ourTelnet = Run("Console telnet\telnet.exe", "console telnet", @SW_HIDE, 3)
$ourRead = StdoutRead($ourTelnet)
StdinWrite($ourTelnet, "open 10.1.32.24 23" & @CR)
Sleep(500)
While 1
    $ourRead = StdoutRead($ourTelnet)
    consolewrite($ourRead);what the server replies
    If StringInStr($ourRead, "login:") > 0 Then
        StdinWrite($ourTelnet, "Admin" & @CR)
        msgbox(0,"test1","")
    ElseIf StringInStr($ourRead, "password:") > 0 Then
        StdinWrite($ourTelnet, "******" & @CRLF)
        msgbox(0,"test2","")    
    ElseIf StringInStr($ourRead, "C:\Documents and Settings\Admin_2") > 0 Then
        msgbox(0,"test3","")
    ElseIf StringInStr($ourRead, "Connection Terminated") > 0 Then
        Run("notepad.exe")
        WinWait("Untitled")
        WinActivate("Untitled")
        send($totalRead)
        msgbox(4096, "Exit", "A log has been formed")
        ExitLoop
    ElseIf @error Then
        ExitLoop
    Else 
        msgbox(0,"test4","")
        ContinueLoop
    EndIf
    $totalRead &= $ourRead
WEnd

My output is in the attachment.

post-58336-12761958716674_thumb.jpg

I have no clue what to make of the output after the "password" line. My password is also never entered. After this point, the code just gets stuck and doesn't respond. It doesn't even seem to keep looping.

Thanks everyone, I really appreciate any help on this. :mellow:

Share this post


Link to post
Share on other sites

Hi,

before looking at your code.

You come from a Windows PC and you want what? Start scripts/programs something like that on ? another Windows PC?

Mega


Scripts & functions Organize Includes Let Scite organize the include files

Yahtzee The game "Yahtzee" (Kniffel, DiceLion)

LoginWrapper Secure scripts by adding a query (authentication)

_RunOnlyOnThis UDF Make sure that a script can only be executed on ... (Windows / HD / ...)

Internet-Café Server/Client Application Open CD, Start Browser, Lock remote client, etc.

MultipleFuncsWithOneHotkey Start different funcs by hitting one hotkey different times

Share this post


Link to post
Share on other sites

Hi,

before looking at your code.

You come from a Windows PC and you want what? Start scripts/programs something like that on ? another Windows PC?

Mega

Hi Mega,

Not too sure what you mean, but I'm using Windows XP and the server laptop is also Windows XP. I want to access the server with Telnet, and run a small command-line program that computes a checksum on various files. For example, the executable is fciv.exe, and all I need to do once I have access is go

C:\Documents and Settings\Admin_2>fciv test.txt
//
// File Checksum Integrity Verifier version 2.05.
//
1163b4dc827cbe808e4e11a269241ec4 test.txt

and I want to receive back that checksum "1163b4dc827cbe808e4e11a269241ec4" from the server.

I hope that helps,

Pranav

Share this post


Link to post
Share on other sites

Hi,

why then using telnet? Why not using a normal share e.g. in combination with PsExec from sysinternals for remoteRun?

Mega


Scripts & functions Organize Includes Let Scite organize the include files

Yahtzee The game "Yahtzee" (Kniffel, DiceLion)

LoginWrapper Secure scripts by adding a query (authentication)

_RunOnlyOnThis UDF Make sure that a script can only be executed on ... (Windows / HD / ...)

Internet-Café Server/Client Application Open CD, Start Browser, Lock remote client, etc.

MultipleFuncsWithOneHotkey Start different funcs by hitting one hotkey different times

Share this post


Link to post
Share on other sites

Hi Mega,

I've never heard of that option before, but I need to be able to run PsExec from within autoit, and use it to carry out commands like the one I mentioned. Is this possible? I've been looking into the program, and for some reason, I can't even get it to access my server using the command line. I don't think I know enough about the subject... Regardless, I kind of like the simplicity of Telnet, because I feel like I'm really close to something that works.

Thanks,

Pranav

Share this post


Link to post
Share on other sites

#6 ·  Posted (edited)

#include <Constants.au3>
#include <GuiEdit.au3>

Dim $PID = Run(@ComSpec, @SystemDir, @SW_HIDE, $STDIN_CHILD + $STDOUT_CHILD)
Dim $Line

$Debug = GUICreate("Debug", 500,500)
$EditCTRL = GUICtrlCreateEdit("",0,0,500,500)
GUISetState()

While 1
    $Line = StdoutRead($PID)
    _GUICtrlEdit_AppendText($EditCTRL, $Line)
    If StringInStr($line, "[Version 6.1.7600]") Then ; Event Trigger
        StdinWrite($PID, "dir" & @CRLF) ; Event Action
    EndIf
    sleep(500)
Wend

There is an example of 1 event and 1 trigger. I think you'll catch on... :mellow:

Of course it would be better to call telnet directly IMO.

What an old post there you've managed to dig up on me! That entire thread is a little embarrassing... Posted Image

I've got my black-belt now... haha Posted Image

Edited by BinaryBrother

Share this post


Link to post
Share on other sites

Hi BinaryBrother,

Thanks for the help! :mellow: I'll try and give it a shot.. I still have my white belt :P:party: , hehe

Share this post


Link to post
Share on other sites

#8 ·  Posted (edited)

Ok, so I revamped the example... You should enjoy this...

#include <Constants.au3>
#include <GuiEdit.au3>
#include <GUIConstantsEx.au3>

Dim $PID = Run(@ComSpec, @SystemDir, @SW_HIDE, $STDIN_CHILD + $STDOUT_CHILD)
Dim $Triggers[50]
Dim $Actions[50]

Opt("GUIOnEventMode", 1)
GUISetOnEvent($GUI_EVENT_CLOSE, "Close")
$Debug = GUICreate("Debug", 500,500)
$EditCTRL = GUICtrlCreateEdit("",0,0,500,500)
_GUICtrlEdit_SetLimitText($EditCTRL, 900000)
GUISetState()

AdlibRegister("CheckTriggers", 500)

; Define Triggers
$Triggers[0] = 1 ; Number of Triggers
$Triggers[1] = "[Version 6.1.7600]" ; This is one of the first things I see once CMD is loaded
;

;Define Actions
$Actions[0] = 1
$Actions[1] = "dir" ; This happens if $Trigger[1] is detected.
;

While 1
    Sleep(500)
WEnd



Func CheckTriggers()
    local $Line
    $Line = StdoutRead($PID)
    _GUICtrlEdit_AppendText($EditCTRL, $Line) ; For debugging
    For $N = 1 To $Triggers[0]
        If StringInStr($Line, $Triggers[$N]) Then
            StdinWrite($PID, $Actions[$N] & @CRLF)
        EndIf
    Next
EndFunc

Func Close()
    Exit
EndFunc
Edited by BinaryBrother

Share this post


Link to post
Share on other sites

I really like that example, it's very neat :mellow:

I tried using that code, but isn't that what I was doing before? (from my code in the original post) I read in a line from the server, and searched the string for a specific substring like "login" or "password", and based on that, I write a certain message to the server.

I tried your code and called telnet directly and I got it to work up to the password prompt, and again it dumps me that garbage data for some reason (seen in the output picture in the original post).

Not sure if you'd know why, or if I'm doing something differently..?

Anyways, thanks for the help BinaryBrother :P

Share this post


Link to post
Share on other sites

#10 ·  Posted (edited)

Can you post what the expected output is? Using telnet directly. :mellow:

Some output like that can occasionally be ignored, as it's often text formatting. Does the server force some sort of encryption?

Edited by BinaryBrother

Share this post


Link to post
Share on other sites

Hi!

So the expected output only differs once we reach the password prompt. After properly inputting the password, the expected output is:

*===============================================================
Welcome to Microsoft Telnet Server.
*===============================================================
C:\Documents and Settings\Admin_2>

Although, I've noticed that sometimes, the first 3 lines (header) do not show up. Would the header somehow cause this problem?

I don't think the server should be forcing any encryption. It's just the telnet server service on windows xp that I've enabled.

Thanks :mellow:

Share this post


Link to post
Share on other sites

#12 ·  Posted (edited)

Personally I think it's working. Telnet probably deciphers those characters and inputting it RAW to an edit control isn't displaying properly. Try parsing a command and getting the output once logged in... Like one of those hashing commands.

Edited by BinaryBrother

Share this post


Link to post
Share on other sites

I definitely think you're right, but I can't seem to get any proper output after I send commands. All it seems to see is that garbage I posted.

e.g. In this code here

$ourRead = StdoutRead($ourTelnet)
If StringInStr($ourRead, "1H*") Then
    StdinWrite($ourTelnet, "fciv Filezilla/test.txt" & @CRLF)
    msgbox(0,"test3","")
ElseIf StringInStr($ourRead, "File Checksum") Then
    msgbox(0,"test5","")
EndIf

I would never see msgbox "test5" which would signify that the tool fciv actually ran. "1H*" is part of the 'garbage' code and when I find that, I send the fciv command, which should have the text "File Checksum" somewhere if it was successful.

Sorry if that was a bit confusing. And again, I really appreciate your help on this. :mellow:

Share this post


Link to post
Share on other sites

#14 ·  Posted (edited)

I'm assuming it has something to do with character encoding. Either way, it's above my head. You have within your grasp the necessary building blocks for full automation, once you get around this issue. One of the senior Devs will have to help further. I'm actually looking forward to a solution, I created a MUD Automation bot one time, and never did figure out where that "garbage" was coming from.

Something else I found... :mellow:

http://www.autoitscript.com/forum/index.php?showtopic=114946&st=0&p=803176&hl=telnet&fromsearch=1&#entry803176

Edited by BinaryBrother

Share this post


Link to post
Share on other sites

Ack... Apparently I needed to read your topic a bit better... I see your TCP problem and found some info on it. (Link at Bottom of post)

The below link tells quite a bit about Terminal Emulation. I also found that the MS Telnet can't redirect STDIN or STDOUT, and took the time to realize you were using 3rd party software... Probably doesn't help much, but I tried. lol

http://translate.google.com/translate?hl=en&sl=zh-CN&u=http://bbs.chinaunix.net/archiver/%3Ftid-767617.html&ei=f_QYTNz4FsvsnQfvyOXCCg&sa=X&oi=translate&ct=result&resnum=1&ved=0CBUQ7gEwAA&prev=/search%3Fq%3DFFFD25FFFB01FFFB03FFFD27FFFD1FFFFD00FFFB00%26hl%3Den%26rlz%3D1C1GGGE_enUS378GB378

Share this post


Link to post
Share on other sites

#16 ·  Posted (edited)

Hey BinaryBrother,

No problem! haha :mellow: . I'll look into this more and see what I can do.. Thanks for all the help!

Edited by PranavS

Share this post


Link to post
Share on other sites

After days of research on terminal Emulation, escape sequences and ANSI... I've found the following utterly useful page on Wikipedia...

http://en.wikipedia.org/wiki/ANSI_escape_code

$Line = StringRegExpReplace($Line, "\[\d+;\d+H", "")

$Line = StringRegExpReplace($Line, "\[K", "")

Use the code above in your $Line = StdoutRead() loop and clean the $Line var... Doing so on my system cleans out unwanted escape sequences, which makes mine work near perfect...

Doing this will cause alignment issues etc, but at-least getting the data works for me now.

Share this post


Link to post
Share on other sites

Hey there!

That's great news! :P Farthest I was able to go was find out that it was about terminal emulation.. Plus I've been really bogged down with some other projects (put this one on the side for a bit). I will look into this as soon as I get a chance, but thanks for all of your help, you're the best man! (True black belt :mellow: )

Share this post


Link to post
Share on other sites

Hi,

so what do you mean by "clean the $Line var"? I ran these 3 lines to get rid of the terminal emulation:

$ourRead = StringRegExpReplace($ourRead, "\[\d+;\d+H", "")
$ourRead = StringRegExpReplace($ourRead, "\[K", "")
$ourRead = StringRegExpReplace($ourRead, "*W*C", "")

because there seemed to be some "*W*C" data left over.

So at this point I seem to get access, but I can't receive any more data.. seems like it stops looping, waiting for data. Guess I don't know what "cleaning" means :mellow:

Thanks!

Share this post


Link to post
Share on other sites

#20 ·  Posted (edited)

#include <Constants.au3>

EnvSet("TELNET_REDIR", "1")

Dim $PID = Run("C:\Program Files\telnet\telnet.exe", @SystemDir, @SW_HIDE, $STDIN_CHILD + $STDOUT_CHILD)
Dim $Triggers[50]
Dim $Actions[50]

AdlibRegister("CheckTriggers", 500)
_Connect($PID, "IP", 23, 3000)
_Login($PID, "BinaryBrother", "wrong-pass", 0) ; Fixes the "Invalid Handle" error I get randomly...
_Login($PID, "BinaryBrother", "right-pass", 500) ; Logs in.

; Define Triggers
$Triggers[0] = 1 ; Number of Triggers
$Triggers[1] = "C:\Users\BinaryBrother>" ; This is one of the first things I see once CMD is loaded
;$Triggers[2] = "password:"
;

;Define Actions
$Actions[0] = 0
$Actions[1] = "dir" ; This happens if $Trigger[1] is detected.
;$Actions[2] = ""
;

While 1
    Sleep(500)
WEnd



Func CheckTriggers()
    local $Line
    $Line = StdoutRead($PID)
    $Line = StringRegExpReplace($Line, "\[\d+;\d+H", "")
    $Line = StringRegExpReplace($Line, "\[K", "")
    ConsoleWrite($Line)
    For $N = 1 To $Triggers[0]
        If StringInStr($Line, $Triggers[$N]) Then
            StdinWrite($PID, $Actions[$N] & @CRLF)
        EndIf
    Next
EndFunc

Func _Connect($STD_Handle, $Server, $Port, $Sleep)
    StdinWrite($STD_Handle, "open " & $Server & " " & $Port & @CRLF)
    While 1
        $Line = StdoutRead($STD_Handle)
        If StringInStr($Line, "Connected to") Then
            Sleep($Sleep)
            Return 1
        EndIf
        If StringInStr($Line, "Error") Then
            Return 0
        EndIf
    WEnd
EndFunc

Func _LogIn($STD_Handle, $Username, $Password, $Sleep)
    StdinWrite($STD_Handle, $Username & @CRLF)
    Sleep($Sleep)
    StdinWrite($STD_Handle, $Password & @CRLF)
EndFunc


Func Close()
    AdlibUnRegister("CheckTriggers")
    ProcessClose("telnet.exe")
    Exit
EndFunc

That code works nearly perfect for me. I get an "Invalid Handle" error after login if I don't have 1 failed login attempt for some reason. So I attempt a login with the wrong password, then login correctly and it works every time. Of course the "formatting" is fried as well... but my triggers and actions system is virtually flawless...

Also, I'm running "Console Telnet" v2.1b2

http://consoletelnet.sourceforge.net/download/telnet-2.1b2-setup.exe

Edited by BinaryBrother

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  
Followers 0