eagle4life69 Posted June 2, 2016 Share Posted June 2, 2016 So I have been looking into this for some time and I have not found any good answers... I would like to connect to some switches at work via telnet. I need to connect to them then check if any are err-disabled if so I then need to send the commands to enable them. Normal way we connect to do this manually In a command prompt telnet <switchname> <username> <password> show int status to clear a locked port clear port-security all config t int <port> shut no shut end show int status wr mem now I am fine if I have to make many connections to the switch as long as I can get it to get me my information Any help will be great. And please don't just say go search it, give me sample code and I will do the rest thanks Link to comment Share on other sites More sharing options...
youtuber Posted June 2, 2016 Share Posted June 2, 2016 #RequireAdmin _Wow64FsRedirection(False) Run("C:\Windows\System32\telnet.exe") _Wow64FsRedirection(True) Func _Wow64FsRedirection($state) ; Disables or reverts the filesystem redirector for a 32 bit process running on 64bit OS If Not @AutoItX64 And @OSArch = 'X64' Then If $state Then DllCall("kernel32.dll", "int", "Wow64RevertWow64FsRedirection", "int", 0) Else DllCall("kernel32.dll", "int", "Wow64DisableWow64FsRedirection", "int", 0); or 1 as per help EndIf If @error Then Return SetError(1) EndIf EndFunc WinWait("[CLASS:ConsoleWindowClass]") WinActivate("[CLASS:ConsoleWindowClass]") WinWaitActive("[CLASS:ConsoleWindowClass]") Send("open 192.168.2.1{enter}") ;Send("quit{enter}") eagle4life69 1 Link to comment Share on other sites More sharing options...
eagle4life69 Posted June 2, 2016 Author Share Posted June 2, 2016 ^^ Thats great, but how can I read the information so I can automated looking for the err-disabled port. Also I would love to run this in the background so I can just push the information out to the user/admin letting them know that there is an error and if they want it unlocked.. If they do then I want it to know to go in and unlock the port Link to comment Share on other sites More sharing options...
rudi Posted June 3, 2016 Share Posted June 3, 2016 Hi. I'd like to recommend to use PuTTY, so that your communication is encrypted (SSH) rather than plain text (telnet.exe) $Output="" $PIDRun($Putty & $Params,$WD,@SW_HIDE,$STDERR_MERGED) $SearchForString="Your Password Please" $MyPwd="TotallyTopSecret*1234" $PwAnswerNotDone=True while ProcessExists($PIDRun) $Output&=StdoutRead($PIDRun) ToolTip($Output) if (StringRight($Output,StringLen($SearchForString))=$SearchForString) and $PwAnswerNotDone then ; the switch is waiting for your password StdinWrite($PIDRun,$MyPwd & @CRLF) $PwAnwerNotDone=False EndIf ; do more stuff WEnd REgards, Rudi. Note: That example code is not tested at all, copy/shake/paste from another script I use to do SCP backups of the switches running-confg Earth is flat, pigs can fly, and Nuclear Power is SAFE! Link to comment Share on other sites More sharing options...
TheDcoder Posted June 3, 2016 Share Posted June 3, 2016 Or you can use the native TCP functions which are built into AutoIt! TCP functions are just like telnet for AutoIt ! Except that you use TCPSend instead of pressing enter . The greatest example is interacting with a IRC Server, you can do it in both Telnet and in AutoIt via the TCP functions. You can check my IRC UDF which does that: Good luck, TD . EasyCodeIt - A cross-platform AutoIt implementation - Fund the development! (GitHub will double your donations for a limited time) DcodingTheWeb Forum - Follow for updates and Join for discussion Link to comment Share on other sites More sharing options...
eagle4life69 Posted June 3, 2016 Author Share Posted June 3, 2016 6 hours ago, rudi said: Hi. I'd like to recommend to use PuTTY, so that your communication is encrypted (SSH) rather than plain text (telnet.exe) These are switches and we have not implemented SSH on all of our switches 4 hours ago, TheDcoder said: Or you can use the native TCP functions which are built into AutoIt! TCP functions are just like telnet for AutoIt ! Except that you use TCPSend instead of pressing enter . The greatest example is interacting with a IRC Server, you can do it in both Telnet and in AutoIt via the TCP functions. You can check my IRC UDF which does that Good luck, TD . Thanks for the code I'll see if I can make it work Link to comment Share on other sites More sharing options...
TheDcoder Posted June 3, 2016 Share Posted June 3, 2016 @eagle4life69 Feel free to ask any questions regarding TCP... Its a little tricky to get into! EasyCodeIt - A cross-platform AutoIt implementation - Fund the development! (GitHub will double your donations for a limited time) DcodingTheWeb Forum - Follow for updates and Join for discussion Link to comment Share on other sites More sharing options...
eagle4life69 Posted June 3, 2016 Author Share Posted June 3, 2016 14 minutes ago, TheDcoder said: @eagle4life69 Feel free to ask any questions regarding TCP... Its a little tricky to get into! Could you give me a starter code so I can connect to via telnet and how I can read back any response sent from that connection? Link to comment Share on other sites More sharing options...
TheDcoder Posted June 3, 2016 Share Posted June 3, 2016 Just now, eagle4life69 said: Could you give me a starter code so I can connect to via telnet and how I can read back any response sent from that connection? Ok, I will write some starter code for you . Give me some time and I will post it here . eagle4life69 1 EasyCodeIt - A cross-platform AutoIt implementation - Fund the development! (GitHub will double your donations for a limited time) DcodingTheWeb Forum - Follow for updates and Join for discussion Link to comment Share on other sites More sharing options...
TheDcoder Posted June 3, 2016 Share Posted June 3, 2016 Here is a basic TCP usage example: Global Const $SERVER = "" ; IP or Address of the server Global Const $SERVER_IP = TCPNameToIP($SERVER) Global Const $PORT = 0 ; Port of the server to connect Global $iSocket = TCPConnect($SERVER_IP, $PORT) ; Do not change this TCPSend($iSocket, "") ; Use TCPSend to send commands Global $vData ; Recived Data is stored in this variable While 1 ; This is a infinite loop which recives data from the server $vData = TCPRecv($iSocket, 100) ; Recive the data (only the first 100 bytes will be recived!) If Not $vData = "" Then ConsoleWrite($vData) ; Write the data to the Console Output Sleep(10) ; Rest the CPU a little WEnd The code is very basic and might require some modifications to work with your situation... I only made the basic because I don't know what protocol you are trying to automate . Use the above code as a base , Let me know if it works . eagle4life69 1 EasyCodeIt - A cross-platform AutoIt implementation - Fund the development! (GitHub will double your donations for a limited time) DcodingTheWeb Forum - Follow for updates and Join for discussion Link to comment Share on other sites More sharing options...
eagle4life69 Posted June 3, 2016 Author Share Posted June 3, 2016 So using telnet on port 23, is there anyway to view what the connection is receiving so I can see whats going on? Normal Connection goes telnet <server> it then opens the server and asks for the username I answer it and press enter it then asks for password and I enter it and press enter I then can type the commands in my OP... I need to know if it is accepting my credintials Link to comment Share on other sites More sharing options...
TheDcoder Posted June 4, 2016 Share Posted June 4, 2016 Now that I know that you are trying to automate the Telnet protocol (note: Telnet protocol is not the same as telnet.exe which you use in command prompt!), I have looked into the Specifications (or Documentation) of it, RFC 854. After reading the basics, its a fairly complicated protocol to automate! (even for me). It would require a basic framework made in AutoIt to automate it... That would take some time to make... unfortunately I don't have that free time . Your best option now is to follow @youtuber's example as it directly interacts with Command Prompt . Sorry for letting you down here. Good luck, TD EasyCodeIt - A cross-platform AutoIt implementation - Fund the development! (GitHub will double your donations for a limited time) DcodingTheWeb Forum - Follow for updates and Join for discussion Link to comment Share on other sites More sharing options...
rudi Posted June 6, 2016 Share Posted June 6, 2016 On 3.6.2016 at 4:38 PM, eagle4life69 said: These are switches and we have not implemented SSH on all of our switches Thanks for the code I'll see if I can make it work Hi. Hoping I caught all the required lines from our template to enable encrypted communication for Cisco Switches: crypto key generate rsa line con 0 exec-timeout 0 0 line vty 0 4 exec-timeout 0 0 len 80 transport input ssh line vty 5 15 transport input ssh exec-timeout 60 0 len 80 transport input ssh Regards, Rudi. Earth is flat, pigs can fly, and Nuclear Power is SAFE! Link to comment Share on other sites More sharing options...
eagle4life69 Posted June 17, 2016 Author Share Posted June 17, 2016 since it seems that it is actually pretty hard to hook into telnet exactly. How can I send and receive info to a command prompt. If I could somehow send the commands needed in a command prompt and read what it says wouldn't that be easier to program. And if so then could I hide the command prompt window and still interact with it? Link to comment Share on other sites More sharing options...
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