Constantin

Is AutoIT suitable for this task?

6 posts in this topic

I am working on a standalone interface which should be used to telnet to some routers. I managed to do something basic using .hta files and JScript, but many tasks cannot be done this way. (Before I start learning it) Is AutoIT suitable for this?

In brief, this is what it should do:

- create/load some graphic elements (like a HTML or HTML map, or something similarly looking), so you can click on the router you want to connect to

- on click, check if the corresponding telnet window is already open. If it is, gives focus to it, otherwise launch telnet to an IP address (it would be even better if it could read the received answer from the remote router)

- changes the title of the telnet window to something more useful (like Router1)

Basically that's it. Can it be easily done in AutoIT?

Thanks

Share this post


Link to post
Share on other sites



Welcome to AutoIt and the forum!

I'm sure this can be done using AutoIt.

  1. I would use a GUI, much easier to create and process. That's what AutoIt (amongst other things) was made for
  2. WinList to check if the Window already exists, WinActivate to give focus to it, Run to start a telnet session
  3. WinSetTitle to modify a Windows title

If you have further questions we will be glad to assist.


My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2017-04-18 - Version 1.4.8.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (NEW 2017-02-27 - Version 1.3.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2015-04-01 - Version 0.4.0.0) - Download - General Help & Support - Example Scripts
Excel - Example Scripts - Wiki
Word - Wiki
PowerPoint (2015-06-06 - Version 0.0.5.0) - Download - General Help & Support

Tutorials:
ADO - Wiki

 

Share this post


Link to post
Share on other sites

Thanks for your (quick) answer and tips!

Well, it looks like my AutoIt adventure starts today :)

Share this post


Link to post
Share on other sites

I would split your script into several functions which then can be tested individually:

  • Creation of GUI and buttons
  • determine Routers to be processed (stored in an Ini file or enumerated dynamically?)
  • Check for Telnet Window, give focus or create new one and change title

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2017-04-18 - Version 1.4.8.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (NEW 2017-02-27 - Version 1.3.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2015-04-01 - Version 0.4.0.0) - Download - General Help & Support - Example Scripts
Excel - Example Scripts - Wiki
Word - Wiki
PowerPoint (2015-06-06 - Version 0.0.5.0) - Download - General Help & Support

Tutorials:
ADO - Wiki

 

Share this post


Link to post
Share on other sites

Hmmz, I recently did something along those lines as an experiment. Maybe this helps? It doesn't use telnet windows but direct TCP. all kinds of possibilities with it, like creating a different gui for each session so you can create as many as you want instead of being stuck to the 3 available in the hardcoded gui. It's actually nicer than I remembered :)

This is my quick&dirty NodeJS script that spins up three TCP servers:

Quote

var net = require('net');

var server1 = net.createServer(function (socket) {
    socket.write('Welcome to the Telnet server 1!\r\n');
    socket.on('data', function(data) { socket.write("Thanks for sending to 1: " + data.toString() + "\r\n"); })
}).listen(8887);

var server2 = net.createServer(function (socket) {
    socket.write('Welcome to the Telnet server 2!\r\n');
    socket.on('data', function(data) { socket.write("Thanks for sending to 2: " + data.toString() + "\r\n"); })
}).listen(8888);

var server3 = net.createServer(function (socket) {
    socket.write('Welcome to the Telnet server 3!\r\n');
    socket.on('data', function(data) { socket.write("Thanks for sending to 3: " + data.toString() + "\r\n"); })
}).listen(8889);

And now, welcome to S.T.T.T.T.T. :) 

#include <ButtonConstants.au3>
#include <EditConstants.au3>
#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>
#include <GUIConstantsEx.au3>

opt("TCPTimeout", 1)

OnAutoItExitRegister("OnAutoItExit")
TCPStartup()

Global $nrOfChannels = 3

$GUI = GUICreate("Sadbunny's Totally Terrific Three-Pronged Telnet Thingy", 1050, 732, 197, 131)

Global $hostInput[$nrOfChannels] = [GUICtrlCreateInput("127.0.0.1", 32, 58, 200, 21), GUICtrlCreateInput("127.0.0.1", 372, 58, 200, 21), GUICtrlCreateInput("127.0.0.1", 712, 58, 200, 21)]
Global $portInput[$nrOfChannels] = [GUICtrlCreateInput("8887", 248, 58, 90, 21), GUICtrlCreateInput("8888", 588, 58, 90, 21), GUICtrlCreateInput("8889", 928, 58, 90, 21)]
Global $sendInput[$nrOfChannels] = [GUICtrlCreateInput("hi", 32, 90, 256, 21), GUICtrlCreateInput("hello", 372, 90, 256, 21), GUICtrlCreateInput("yo", 712, 90, 256, 21)]
Global $goButton[$nrOfChannels] = [GUICtrlCreateButton("OPEN 1", 32, 11, 307, 35), GUICtrlCreateButton("OPEN 2", 372, 11, 307, 35), GUICtrlCreateButton("OPEN 3", 712, 11, 307, 35)]
Global $sendButton[$nrOfChannels] = [GUICtrlCreateButton("Send", 296, 89, 43, 23), GUICtrlCreateButton("Send", 636, 89, 43, 23), GUICtrlCreateButton("Send", 976, 89, 43, 23)]
Global $logEdit[$nrOfChannels] = [GUICtrlCreateEdit("", 32, 130, 305, 585, BitOR($GUI_SS_DEFAULT_EDIT, $ES_READONLY)), GUICtrlCreateEdit("", 372, 130, 305, 585, BitOR($GUI_SS_DEFAULT_EDIT, $ES_READONLY)), GUICtrlCreateEdit("", 712, 130, 305, 585, BitOR($GUI_SS_DEFAULT_EDIT, $ES_READONLY))]

Global $socketId[3] = [False, False, False]

For $i = 0 To $nrOfChannels - 1
    GUICtrlSetBkColor($goButton[$i], 0x88FF88)
    GUICtrlSetBkColor($logEdit[$i], 0xFF8888)
    GUICtrlSetState($sendButton[$i], $GUI_DISABLE)
Next

GUISetState(@SW_SHOW)

While 1
    $msg = GUIGetMsg()
    If $msg = $GUI_EVENT_CLOSE Then
        Exit
    EndIf

    For $i = 0 To $nrOfChannels - 1
        If $msg = $goButton[$i] Then openClose($i, GUICtrlRead($hostInput[$i]), GUICtrlRead($portInput[$i]))

        If $msg = $sendButton[$i] Then sendStuff($i, GUICtrlRead($sendInput[$i]))

        If $socketId[$i] Then
            $data = TCPRecv($socketId[$i], 65536)
            If @error Then
                MsgBox(16, "Read error", "Error receiving from open TCP socket " & $i & ": " & @error & ". Extended: " & @extended & ". Closing open channels, then exiting.")
                Exit
            Else
                If $data <> "" Then GUICtrlSetData($logEdit[$i], GUICtrlRead($logEdit[$i]) & $data)
            EndIf
        EndIf
    Next

    Sleep(10)
WEnd

Func closeOpenChannels()
    For $closeId = 0 To $nrOfChannels - 1
        If $socketId[$closeId] Then
            ConsoleWrite("Closing channel " & $closeId & ", socketId: " & $socketId[$closeId] & @CRLF)
            TCPCloseSocket($socketId)
        Else
            ConsoleWrite("Channel " & $closeId & " not open." & @CRLF)
        EndIf
    Next
EndFunc   ;==>closeOpenChannels

Func openClose($index, $host, $port)
    If Not $socketId[$index] Then
        doOpen($index, $host, $port)
    Else
        doClose($index, $host, $port)
    EndIf
EndFunc   ;==>openClose

Func doOpen($index, $host, $port)
    $socketId[$index] = TCPConnect($host, $port)
    If @error Then
        MsgBox(16, "error", "Failed to open TCP socket " & $index & " to " & $host & ":" & $port & ", error: " & @error)
        Exit
    Else
        ConsoleWrite("Opened channel " & $index & " to " & $host & ":" & $port & ", id: " & $socketId[$index] & @CRLF)
    EndIf
    GUICtrlSetState($sendButton[$index], $GUI_ENABLE)
    GUICtrlSetState($hostInput[$index], $GUI_DISABLE)
    GUICtrlSetState($portInput[$index], $GUI_DISABLE)
    GUICtrlSetBkColor($goButton[$i], 0xFF8888)
    GUICtrlSetBkColor($logEdit[$i], 0x88FF88)
    GUICtrlSetData($goButton[$index], StringReplace(GUICtrlRead($goButton[$index]), "OPEN", "CLOSE"))
EndFunc   ;==>doOpen

Func doClose($index, $host, $port)
    TCPCloseSocket($socketId[$index])
    ConsoleWrite("Closed channel " & $index & " to " & $host & ":" & $port & ", id: " & $socketId[$index] & @CRLF)
    GUICtrlSetState($sendButton[$index], $GUI_DISABLE)
    GUICtrlSetState($hostInput[$index], $GUI_ENABLE)
    GUICtrlSetState($portInput[$index], $GUI_ENABLE)
    $socketId[$index] = False
    GUICtrlSetBkColor($goButton[$i], 0x88FF88)
    GUICtrlSetBkColor($logEdit[$i], 0xFF8888)
    GUICtrlSetData($goButton[$index], StringReplace(GUICtrlRead($goButton[$index]), "CLOSE", "OPEN"))
EndFunc   ;==>doClose

Func sendStuff($index, $string)
    TCPSend($socketId[$index], $string)
EndFunc   ;==>sendStuff

Func OnAutoItExit()
    closeOpenChannels()
    TCPShutdown()
EndFunc   ;==>OnAutoItExit

 


Roses are FF0000, violets are 0000FF... All my base are belong to you.

Share this post


Link to post
Share on other sites

There is some very interesting stuff here, thank you, I'll see about the NodeJS stuff as soon as I have some time (I know nothing about NodeJS, although I often use Javascript with cgi scripts).

Thank you guys for being so ready to be helpful.

For further reference, let me post the sketch of the resulting script. I lost a lot of time trying to make it work using WinSetTitle, but for some reasons (bugs or Telnet.exe being a "special" kind of exe generating a special kind of window) it doesn't work. Fortunately there is the _WinAPI_SetWindowText alternative, which worked well. On the bright side, I learned to do some debugging in the process, so I won't complain. :)

#include <GUIConstantsEx.au3>
#include <MsgBoxConstants.au3>
#include <WinAPI.au3>

RunGUI()

Func RunGUI()
   Local $hGUI = GUICreate("Network", 300, 250)
   $r1=GUICtrlCreateIcon(@ScriptDir & '\router.ico', -1, 20, 40, 48, 48)
   $r2=GUICtrlCreateIcon(@ScriptDir & '\router.ico', -1, 100, 100, 48, 48)
   GUICtrlCreateLabel("PE router", 20, 25)
   GUICtrlCreateLabel("Customer router", 140, 140)
   GUISetState(@SW_SHOW)
   Do
         $idMsg = GUIGetMsg()
         Switch $idMsg
            Case $r1
               If WinExists("Router 1") Then
                  WinActivate("Router 1")
               Else
                  RunTelnet("1.2.3.4", "7002", "Router 1")
               EndIf
            Case $r2
               If WinExists("Router 2") Then
                  WinActivate("Router 2")
               Else
                  RunTelnet("1.2.3.4", "7002", "Router 2")
               EndIf
         EndSwitch
   Until $idMsg = $GUI_EVENT_CLOSE
   GUIDelete()
 EndFunc

 Func RunTelnet(ByRef $ip, ByRef $port, ByRef $caption)
   Local $iPID = Run("c:\\Windows\\Sysnative\\telnet.exe " & $ip & " " & $port, "", @SW_SHOWNORMAL)
   WinWaitActive("Telnet " & $ip)
   Local $hWnd = WinGetHandle("Telnet " & $ip)
   _WinAPI_SetWindowText ( $hWnd, $caption )
EndFunc

 

router.ico

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