eagle4life69

Telnet

14 posts in this topic

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

Share this post


Link to post
Share on other sites



#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}")

 

1 person likes this

Share this post


Link to post
Share on other sites

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

Share this post


Link to post
Share on other sites

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!

Share this post


Link to post
Share on other sites

Or you can use the native TCP functions which are built into AutoIt! TCP functions are just like telnet for AutoIt :o! 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 :D.


Spoiler

My contributions to the AutoIt Community

Some messages & Apologizes:

If I hurt you, Please accept my apologies, I never (regardless of the situation) mean to hurt anybody!!!

Also, I am very busy with my project so I will appear in the last row of the online list, if you want to contact me: Email@TheDcoder.xyz

Or you can have a nice chat with me in freenode, I use the same nick on freenode too!

3fHNZJ.gif

PLEASE JOIN ##AutoIt AND HELP THE IRC AUTOIT COMMUNITY!

Share this post


Link to post
Share on other sites
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 :o! 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 :D.

Thanks for the code I'll see if I can make it work

Share this post


Link to post
Share on other sites

@eagle4life69 Feel free to ask any questions regarding TCP... Its a little tricky to get into! ;)


Spoiler

My contributions to the AutoIt Community

Some messages & Apologizes:

If I hurt you, Please accept my apologies, I never (regardless of the situation) mean to hurt anybody!!!

Also, I am very busy with my project so I will appear in the last row of the online list, if you want to contact me: Email@TheDcoder.xyz

Or you can have a nice chat with me in freenode, I use the same nick on freenode too!

3fHNZJ.gif

PLEASE JOIN ##AutoIt AND HELP THE IRC AUTOIT COMMUNITY!

Share this post


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

 

Share this post


Link to post
Share on other sites
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 :D. Give me some time and I will post it here :).

1 person likes this

Spoiler

My contributions to the AutoIt Community

Some messages & Apologizes:

If I hurt you, Please accept my apologies, I never (regardless of the situation) mean to hurt anybody!!!

Also, I am very busy with my project so I will appear in the last row of the online list, if you want to contact me: Email@TheDcoder.xyz

Or you can have a nice chat with me in freenode, I use the same nick on freenode too!

3fHNZJ.gif

PLEASE JOIN ##AutoIt AND HELP THE IRC AUTOIT COMMUNITY!

Share this post


Link to post
Share on other sites

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

 

Use the above code as a base :), Let me know if it works :D.

1 person likes this

Spoiler

My contributions to the AutoIt Community

Some messages & Apologizes:

If I hurt you, Please accept my apologies, I never (regardless of the situation) mean to hurt anybody!!!

Also, I am very busy with my project so I will appear in the last row of the online list, if you want to contact me: Email@TheDcoder.xyz

Or you can have a nice chat with me in freenode, I use the same nick on freenode too!

3fHNZJ.gif

PLEASE JOIN ##AutoIt AND HELP THE IRC AUTOIT COMMUNITY!

Share this post


Link to post
Share on other sites

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

Share this post


Link to post
Share on other sites

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 :D. Sorry for letting you down here.

 

Good luck, TD :)


Spoiler

My contributions to the AutoIt Community

Some messages & Apologizes:

If I hurt you, Please accept my apologies, I never (regardless of the situation) mean to hurt anybody!!!

Also, I am very busy with my project so I will appear in the last row of the online list, if you want to contact me: Email@TheDcoder.xyz

Or you can have a nice chat with me in freenode, I use the same nick on freenode too!

3fHNZJ.gif

PLEASE JOIN ##AutoIt AND HELP THE IRC AUTOIT COMMUNITY!

Share this post


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

Share this post


Link to post
Share on other sites

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?

 

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

  • Similar Content

    • Jefrey
      By Jefrey
      Hi guys/girls!
      I'm gonna share this UDF I made today.
      It allows you to easily create TCP servers and set actions depending on three events: OnConnect, OnDisconnect and OnReceive.
      It is also multi client (you can set the clients limit) and you can also bind a Console-based executable to the socket (similar to -e parameter in NetCat). This feature is useful if you want to use some Console UDF to create your TCP server and don't want to mix it with the TCP functions. Also, as it runs on background just firing events, it won't pause your script while listening/receiving, so you can do anything else (stop and restart the server, allow the user to click buttons or just wait on an infinite loop) that your callbacks will be called once the event is fired.
      It's also very easy to use. See this examples:
      Example #1: A basic server
      By running this (then connecting to the server using Netcat), you will receive a message box telling you when some user connects or disconnects (the socket ID and his IP address is passed as parameter to your callback function) and also when the user sends something over the TCP socket (the data sent is passed as parameter).
      #cs Download netcat at https://eternallybored.org/misc/netcat/ Execute this script Run in CMD: nc -vv 127.0.0.1 8081 #ce #include "TCPServer.au3" ; First we set the callback functions for the three events (none of them is mandatory) _TCPServer_OnConnect("connected") _TCPServer_OnDisconnect("disconnect") _TCPServer_OnReceive("received") ; And some parameters _TCPServer_DebugMode(True) _TCPServer_SetMaxClients(10) ; Finally we start the server at port 8081 at any interface _TCPServer_Start(8081) Func connected($iSocket, $sIP) MsgBox(0, "Client connected", "Client " & $sIP & " connected!") _TCPServer_Broadcast('new client connected guys', $iSocket) _TCPServer_Send($iSocket, "Hey! Write something ;)" & @CRLF) _TCPServer_SetParam($iSocket, "will write") EndFunc ;==>connected Func disconnect($iSocket, $sIP) MsgBox(0, "Client disconnected", "Client " & $sIP & " disconnected from socket " & $iSocket) EndFunc ;==>disconnect Func received($iSocket, $sIP, $sData, $sPar) MsgBox(0, "Data received from " & $sIP, $sData & @CRLF & "Parameter: " & $sPar) _TCPServer_Send($iSocket, "You wrote: " & $sData) _TCPServer_SetParam($iSocket, 'will write again') EndFunc ;==>received While 1 Sleep(100) WEnd Example #2: A basic HTTP server (just one page, as it is just an example)
      In this example, we run this code and point our browser to the address mentioned on the comments. A basic "It works!" page is show.
      #cs Run this script Point your browser to http://localhost:8081/ #ce #include "TCPServer.au3" _TCPServer_OnReceive("received") _TCPServer_DebugMode(True) _TCPServer_SetMaxClients(10) _TCPServer_Start(8081) Func received($iSocket, $sIP, $sData, $sParam) _TCPServer_Send($iSocket, "HTTP/1.0 200 OK" & @CRLF & _ "Content-Type: text/html" & @CRLF & @CRLF & _ "<h1>It works!</h1>" & @CRLF & _ "<p>This is the default web page for this server.</p>" & @CRLF & _ "<p>However this server is just a 26-lines example.</p>") _TCPServer_Close($iSocket) EndFunc ;==>received While 1 Sleep(100) WEnd Example #3: A telnet-like server (Command Prompt bound to the socket after password requesting)
      By running this example and connecting with Netcat, we will be asked for a password, which is 12345 as we set on the script. If the password is correct, we will see the Command Prompt live-updated (try running a ping to some server, for example).
      #cs Download netcat at https://eternallybored.org/misc/netcat/ Execute this script Run in CMD: nc -vv 127.0.0.1 8081 #ce #include "TCPServer.au3" Global $sPassword = "12345" ; input server password here _TCPServer_OnConnect("connected") _TCPServer_OnDisconnect("disconnect") _TCPServer_OnReceive("received") _TCPServer_DebugMode(True) _TCPServer_SetMaxClients(10) _TCPServer_Start(8081) Func connected($iSocket, $sIP) _TCPServer_Send($iSocket, "Welcome! Please input password: ") _TCPServer_SetParam($iSocket, 'login') EndFunc ;==>connected Func disconnect($iSocket, $sIP) MsgBox(0, "Client disconnected", "Client " & $sIP & " disconnected from socket " & $iSocket) EndFunc ;==>disconnect Func received($iSocket, $sIP, $sData, $sParam) If $sParam = "login" Then If $sData <> $sPassword Then _TCPServer_Send($iSocket, "Wrong password. Try again: ") Return Else _TCPServer_SetParam($iSocket, 'command') _TCPServer_BindAppToSocket($iSocket, 'cmd.exe') EndIf ElseIf $sParam = "command" Then _TCPServer_SendToBound($iSocket, $sData) EndIf EndFunc ;==>received While 1 Sleep(100) WEnd The limit is your imagination? Well, no sure. We have this limit:
      You can't create more than one server with this UDF in the same script. However, you can pause and resume (read 'stop and start again') your server at any time in your script, without having to reset the server settings. And of course you can have many clients (or just one, it's your choice!) in the same server. Or run multiple instances. Functions list:
      _TCPServer_Start
      _TCPServer_Stop
      _TCPServer_Close
      _TCPServer_Send
      _TCPServer_Broadcast
      _TCPServer_SetParam
      _TCPServer_BindAppToSocket
      _TCPServer_SendToBound
      _TCPServer_UnBindAppToSocket
      _TCPServer_GetMaxClients
      _TCPServer_IsServerActive
      _TCPServer_ListClients
      _TCPServer_OnConnect
      _TCPServer_OnDisconnect
      _TCPServer_OnReceive
      _TCPServer_SetMaxClients
      _TCPServer_DebugMode
      _TCPServer_AutoTrim
      _TCPServer_SocketToIP
      _TCPServer_SocketToConnID
      _TCPServer_ConnIDToSocket
       
      Help file and more examples included!
      Latest version: 1.0.0.1
      Download: TCPServer UDF.rar
      Changelog
      1.0 - First release - 18/04/2015 1.0.0.1 - Bug fix __TCPServer_Accept internal function / help file recompiled - 26/04/2015 Perhaps you will need to uncompress the file first, so the help file will work.
      Fork this on Github: http://github.com/jesobreira/TCPServerUDF
      TCPServer UDF.rar