Jump to content

Hyperterminal/Telnet parsing?


Recommended Posts

Hey all,

I'm working on a project where I need to hyperterminal and/or telnet into a series of routers and check configurations and make changes. Any suggestions on APIs and/or DLLs I can use to achieve this? I've already written most of the front end stuff in Au3, just need the Telnet/Hyperterminal code to finish it off.

Thanx,

CMR

Link to comment
Share on other sites

I helped a co-worker with the same issue. My solution isn't great, but it works. Here's a sample.

Global $conn = False
Global $host
Global $output

StartCMD()  ;Starts CMD Prompt
    Connect("192.168.1.150")  ;Opens connect to host
    Command("username","Password:")   ;Sends Command to CMD Window, and waits for "Password:" to appears in CMD windows before sending command "username"
    Command("ver","#")  ;Send command but waits for "#" to appear in window first
    SaveOutput("ver","version info") ;Captures output after the string of the last command, after waiting for "version info"
    Disconnect()  ;Disconnect from client
QuitCMD()  ;Closes CMD Prompt

$file = FileOpen("TelnetScriptOutput.txt",2)
FileWrite($file,$output)  ;Saves caputred output gathered from the SaveOutput() func




Func CMDWinOutputToClip() ;Capture CMD Prompt Output
    If Not WinActive("Telnet " & $host) Then WinWaitActive("Telnet " & $host)
    Send("!{space}{down 6}{right}{down 3}{enter 2}")
    $clip = ClipGet()
    sleep(500)
    ClipPut("")
    Return $clip
EndFunc

Func Command($cmd,$waitstr = "") ;Execute command against Telnet Connection, Supports wait for desired output before sending.
    If $conn = True and WinActive("Telnet " & $host) = 1 Then
        If $waitstr <> "" Then
            Do
                If Not WinActive("Telnet " & $host) Then WinWaitActive("Telnet " & $host)
                $clip = CMDWinOutputToClip()
                sleep(2500)
            Until StringInstr($clip,$waitstr)
        EndIf
        Send($cmd & "{enter}")
    EndIf
    sleep(500)
EndFunc

Func Connect($ip) ;Starts Telnet session with host
    If WinActive(@ComSpec) Then
        Send("telnet " & $ip & "{enter}")
        Sleep(1000)
        If Not WinActive("Telnet " & $host) Then WinWaitActive("Telnet " & $host)
            
        $clip = CMDWinOutputToClip()
        If StringInstr($clip,"Connecting to") Then 
            $conn = False
        Else
            $conn = True
            $host = $ip
            $output = $output & $host & @CRLF
        EndIf
        $clip = ""
    EndIf
EndFunc

Func Disconnect() ;Disconnects active Telnet session
    If $conn = True and WinActive("Telnet " & $host) = 1 Then
        Send("exit{enter}")
        Sleep(500)
        Send("{space}")
        $conn = False
        $output = $output & @CRLF & @CRLF & @CRLF
    EndIf
EndFunc

Func QuitCMD() ;Close CMD Prompt
    Sleep(500)
    WinKill(@ComSpec)
EndFunc

Func SaveOutput($str,$waitstr = "") ;Gather specific output for File Output, Supports wait for desired output before continuing.
    If $conn = True and WinActive("Telnet " & $host) = 1 Then
        If $waitstr <> "" Then
            Do
                If Not WinActive("Telnet " & $host) Then WinWaitActive("Telnet " & $host)
                $clip = CMDWinOutputToClip()
                sleep(2500)
            Until StringInstr($clip,$waitstr)
        EndIf
        Do
            If Not WinActive("Telnet " & $host) Then WinWaitActive("Telnet " & $host)
            $clip = CMDWinOutputToClip()
            sleep(2500)
        Until StringInstr($clip,$str)
        $clip = StringRight($clip,StringLen($clip)-StringInstr($clip,$str)+1)
        $output = $output & $clip & @CRLF
        $clip = ""
    EndIf
    Sleep(500)
EndFunc

Func StartCMD($vis = True) ;Start a CMD Prompt
    Select
    Case $vis = True
        $pid = Run(@Comspec,@WorkingDir,@SW_SHOW)
    Case $vis = False
        $pid = Run(@Comspec,@WorkingDir,@SW_HIDE)
    EndSelect
    WinWaitActive(@ComSpec)
EndFunc

It's a little confusing if you don't know what you're looking at, so feel free to ask me WTF?

Edited by spudw2k
Link to comment
Share on other sites

  • 2 years later...

Make a little bit change for the "Command" function above to make it returned faster if desired output is detected.

Func Command($cmd,$waitstr = "") ;Execute command against Telnet Connection, Supports wait for desired output before sending.
    If $conn = True and WinActive("Telnet " & $host) = 1 Then
        If $waitstr <> "" Then
            While 1
                If Not WinActive("Telnet " & $host) Then WinWaitActive("Telnet " & $host)
                $clip = CMDWinOutputToClip()
                If StringRight($clip, 1) = $waitstr Then ExitLoop  ;cmd prompt is generally at the end of the string
                sleep(2500)
            WEnd
        EndIf
        Send($cmd & "{enter}")
    EndIf
    sleep(500)
EndFunc
Link to comment
Share on other sites

  • 5 years later...

It could save a little time to explain the basis of this solution.
I thought it was very clever, and a very interesting demo of some of the power of autoit - this coming from a complete newcomer.

The crux of the program is that after firing up telnet, it uses a send command that activates items from the menu bar with these commands:

  Send("!{space}{down 6}{right}{down 3}{enter}")
   Send("{enter}")

(the original came with {enter 2} but that did not seem to work for me.)

This sequence selects the entire terminal window and copies it to the clipboard.
Local $clip = ClipGet()

harvests the clipboard contents.

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