Looking for a way to check if a particular port is currently listening Topic is solved

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
Chiefkes
Posts: 30
Joined: 04 May 2020, 20:01

Looking for a way to check if a particular port is currently listening

Post by Chiefkes » 04 Dec 2020, 17:12

I've been able to do this by using SKAN's RunCMD function and running "netstat -an" and searching for the port, but I'm wondering if there is a simpler way using DllCalls. I don't need to connect to the port - just check whether it is currently listening or established.

Cheers

M4verick
Posts: 193
Joined: 03 Nov 2020, 12:00

Re: Looking for a way to check if a particular port is currently listening

Post by M4verick » 04 Dec 2020, 17:31

Try:

Code: Select all

SetTitleMatchMode, 2
Run, %A_ComSpec%,,, PID 
WinWait, ahk_pid %PID% 
ControlSend,, netstat -an{Enter}, cmd.exe

Chiefkes
Posts: 30
Joined: 04 May 2020, 20:01

Re: Looking for a way to check if a particular port is currently listening  Topic is solved

Post by Chiefkes » 04 Dec 2020, 18:03

Thanks! I've actually just managed to cobble together after discovering the GetTcpTable WinAPI function

I found this old gem and butchered together a function that only checks for a particular port and returns its state if it exists.

Gem:
https://github.com/Ixiko/AHK-libs-and-classes-collection/blob/master/lib-a_to_h/GetTcpTable.ahk

Modified function:

Code: Select all

TCP_PortExist(port) {
	static hIPHLPAPI := DllCall("LoadLibrary", "str", "iphlpapi.dll", "ptr"), table := []
	VarSetCapacity(TBL, 4 + (s := (20 * 32)), 0)
	while (DllCall("iphlpapi\GetTcpTable", "ptr", &TBL, "uint*", s, "uint", 1) = 122)
		VarSetCapacity(TBL, 4 + s, 0)

	loop % NumGet(TBL, 0, "uint") {
		o := 4 + ((A_index - 1) * 20)
		, temp_port := (((ROW := NumGet(TBL, o+8,  "uint"))&0xff00)>>8) | ((ROW&0xff)<<8)
		, state := NumGet(TBL, o, "uint")
		if (temp_port = port)
			return state, DllCall("FreeLibrary", "ptr", hIPHLPAPI)
	}
	return 0, DllCall("FreeLibrary", "ptr", hIPHLPAPI)
}

/* ===============================================================================================================================
References:
- https://msdn.microsoft.com/en-us/library/aa366026(v=vs.85).aspx    GetTcpTable function
- https://msdn.microsoft.com/en-us/library/aa366917(v=vs.85).aspx    MIB_TCPTABLE structure
- https://msdn.microsoft.com/en-us/library/aa366909(v=vs.85).aspx    MIB_TCPROW structure
State-Codes:
- CLOSED         1
- LISTEN         2
- SYN_SENT       3
- SYN_RCVD       4
- ESTAB          5
- FIN_WAIT1      6
- FIN_WAIT2      7
- CLOSE_WAIT     8
- CLOSING        9
- LAST_ACK      10
- TIME_WAIT     11
- DELETE_TCB    12
*/

Mr MG
Posts: 14
Joined: 07 Dec 2021, 04:14

Re: Looking for a way to check if a particular port is currently listening

Post by Mr MG » 25 Aug 2022, 05:41

May any one please edit this function to return remote port instead of local port. Thanks

Post Reply

Return to “Ask for Help (v1)”