CodeMaster Rapture Posted March 10, 2008 Posted March 10, 2008 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
spudw2k Posted March 10, 2008 Posted March 10, 2008 (edited) I helped a co-worker with the same issue. My solution isn't great, but it works. Here's a sample. expandcollapse popupGlobal $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 March 10, 2008 by spudw2k Spoiler Things I've Made: Always On Top Tool ◊ AU History ◊ Deck of Cards ◊ HideIt ◊ ICU ◊ Icon Freezer ◊ Ipod Ejector ◊ Junos Configuration Explorer ◊ Link Downloader ◊ MD5 Folder Enumerator ◊ PassGen ◊ Ping Tool ◊ Quick NIC ◊ Read OCR ◊ RemoteIT ◊ SchTasksGui ◊ SpyCam ◊ System Scan Report Tool ◊ System UpTime ◊ Transparency Machine ◊ VMWare ESX Builder Misc Code Snippets: ADODB Example ◊ CheckHover ◊ Detect SafeMode ◊ DynEnumArray ◊ GetNetStatData ◊ HashArray ◊ IsBetweenDates ◊ Local Admins ◊ Make Choice ◊ Recursive File List ◊ Remove Sizebox Style ◊ Retrieve PNPDeviceID ◊ Retrieve SysListView32 Contents ◊ Set IE Homepage ◊ Tickle Expired Password ◊ Transpose Array Projects: Drive Space Usage GUI ◊ LEDkIT ◊ Plasma_kIt ◊ Scan Engine Builder ◊ SpeeDBurner ◊ SubnetCalc Cool Stuff: AutoItObject UDF ◊ Extract Icon From Proc ◊ GuiCtrlFontRotate ◊ Hex Edit Funcs ◊ Run binary ◊ Service_UDF
CodeMaster Rapture Posted March 10, 2008 Author Posted March 10, 2008 Thank you, but I was looking for something a little more elegant. I was going to take this route myself, but hoping someone knew of a good API/DLL I could use for this instead. -CMR
AUTTRY Posted February 21, 2011 Posted February 21, 2011 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
JJayMeyer Posted August 12, 2016 Posted August 12, 2016 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.
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now