Simple Port Checking Tool – AutoIT

Below you will find some code to make a simple port checking tool in AutoIT. The tools simply asks the user for what IP address or Hostname they want to check, and what port. It will then confirm your entry and show you the result.

 

; Script Function:
; Check if a port is open on a machine and report back to the user

#include <FileConstants.au3>
#include <MsgBoxConstants.au3>
#include <WinAPIFiles.au3>

; Run port check

UserPort ()
CheckPort()

Func UserPort ()

; Ask user for what Port and server they want
Global $Server = InputBox(“Server Hostname / IP”, “What is the Hostname or the IP of the server?”, “127.0.0.1”, “”, – 1, -1, 0, 0)
Global $Port = InputBox(“Port”, “What port would you like to check?”, “25”, “”, – 1, -1, 0, 0)

; Display their answer
MsgBox($MB_SYSTEMMODAL, “Responses:”, “I will check ” & $Server & “:” & $Port)

EndFunc

Func CheckPort ()

TCPStartup() ; Start the TCP service.

; Register OnAutoItExit to be called when the script is closed.
OnAutoItExitRegister(“OnAutoItExit”)

; Assign a Local variable the socket and connect to a Listening socket with the IP Address and Port specified.
Local $iSocket = TCPConnect($Server, $Port)

; If an error occurred display the error code and return False.
If @error Then
; The server is probably offline/port is not opened on the server.
Local $iError = @error
MsgBox(BitOR($MB_SYSTEMMODAL, $MB_ICONHAND), “”, “Could not connect, Error code: ” & $iError)
Return False
Else
MsgBox($MB_SYSTEMMODAL, “”, “Connection successful”)
EndIf

; Close the socket.
TCPCloseSocket($iSocket)

EndFunc
Func OnAutoItExit()

TCPShutdown() ; Close the TCP service.

EndFunc
Exit

Leave a Reply

Your email address will not be published. Required fields are marked *