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
; Run port check
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)
Func CheckPort ()
TCPStartup() ; Start the TCP service.
; Register OnAutoItExit to be called when the script is closed.
; 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)
MsgBox($MB_SYSTEMMODAL, “”, “Connection successful”)
; Close the socket.
TCPShutdown() ; Close the TCP service.