Ping4 - Question

Get help with using AutoHotkey and its commands and hotkeys
zotune
Posts: 83
Joined: 17 Nov 2014, 17:57

Ping4 - Question

09 Feb 2017, 08:39

Can this script be used to check if a TCP port is open?
User avatar
jNizM
Posts: 2665
Joined: 30 Sep 2013, 01:33
GitHub: jNizM
Contact:

Re: Ping4 - IPv4 ping function (2015-07-16)

09 Feb 2017, 09:15

No, but I just created a function for you -> GetTcpPortTable.ahk (GitHub)

Ref:
- GetTcpTable function (msdn)
- MIB_TCPTABLE structure (msdn)
- MIB_TCPROW structure (msdn)
[AHK] 1.1.32.00 x64 Unicode | [WIN] 10 Pro (Version 2004) x64 | [GitHub] Profile
Donations are appreciated if I could help you
zotune
Posts: 83
Joined: 17 Nov 2014, 17:57

Re: Ping4 - IPv4 ping function (2015-07-16)

09 Feb 2017, 09:43

jNizM: That's great. Thank you. But, how can I pass a remote IP/Host:Port to this and verify if it is open?
User avatar
jNizM
Posts: 2665
Joined: 30 Sep 2013, 01:33
GitHub: jNizM
Contact:

Re: Ping4 - IPv4 ping function (2015-07-16)

09 Feb 2017, 09:49

Will modify the function to get LocalIP, LocalPort, RemoteIP, RemotePort & Status
[AHK] 1.1.32.00 x64 Unicode | [WIN] 10 Pro (Version 2004) x64 | [GitHub] Profile
Donations are appreciated if I could help you
zotune
Posts: 83
Joined: 17 Nov 2014, 17:57

Re: Ping4 - IPv4 ping function (2015-07-16)

09 Feb 2017, 09:51

Thank you. That would be very useful
User avatar
jNizM
Posts: 2665
Joined: 30 Sep 2013, 01:33
GitHub: jNizM
Contact:

Re: Ping4 - IPv4 ping function (2015-07-16)

09 Feb 2017, 10:10

Here you go -> GetTcpTable.ahk (GitHub)
[AHK] 1.1.32.00 x64 Unicode | [WIN] 10 Pro (Version 2004) x64 | [GitHub] Profile
Donations are appreciated if I could help you
zotune
Posts: 83
Joined: 17 Nov 2014, 17:57

Re: Ping4 - IPv4 ping function (2015-07-16)

09 Feb 2017, 10:21

jNizM: I just want to check if a remote TCP port is open. Here's my current (crappy) script:

Code: Select all

CheckPort(Host,Port)
{
	; DllCall("AllocConsole")
	; WinHide % "ahk_id " DllCall("GetConsoleWindow", "ptr")
	Loop, Parse, % ComObjCreate("WScript.Shell").Exec("Addons\Software\SysinternalsSuite\psping.exe -i 0 -n 0 " Host ":" Port).StdOut.ReadAll(),"`n"
	{
		if (A_Index = 3)
		{
			if InStr(A_LoopField,"ms")
				Return 1
			else
				Return 0
		}
	}
}
User avatar
jNizM
Posts: 2665
Joined: 30 Sep 2013, 01:33
GitHub: jNizM
Contact:

Re: Ping4 - IPv4 ping function (2015-07-16)

09 Feb 2017, 10:27

Code: Select all

RemoteIP   := "192.168.0.1"
RemotePort := "445"

for i, v in GetTcpTable()
    if (RemoteIP = v.RemoteIP) && (RemotePort = v.RemotePort)
        MsgBox % v.State
[AHK] 1.1.32.00 x64 Unicode | [WIN] 10 Pro (Version 2004) x64 | [GitHub] Profile
Donations are appreciated if I could help you
zotune
Posts: 83
Joined: 17 Nov 2014, 17:57

Re: Ping4 - IPv4 ping function (2015-07-16)

09 Feb 2017, 10:45

Thanks, but this port is open and does not return anything

Code: Select all

RemoteIP   := "10.211.114.9"
RemotePort := 6100

for i, v in GetTcpTable()
{
    if (RemoteIP = v.RemoteIP) && (RemotePort = v.RemotePort)
        MsgBox % v.State
}
zotune
Posts: 83
Joined: 17 Nov 2014, 17:57

Re: Ping4 - IPv4 ping function (2015-07-16)

10 Feb 2017, 06:03

Not sure if there was a misunderstanding here
User avatar
jNizM
Posts: 2665
Joined: 30 Sep 2013, 01:33
GitHub: jNizM
Contact:

Re: Ping4 - Question

10 Feb 2017, 07:01

Can you look into this? It retrieves the IPv4 TCP connection table.

Code: Select all

Gui, Margin, 5, 5
Gui, Add, ListView, xm ym w600 h600, % "LocalIP|LocalPort|RemoteIP|RemotePort|State"
for i, v in GetTcpTable()
    LV_Add("", v.LocalIP, v.LocalPort, v.RemoteIP, v.RemotePort, v.State)
LV_ModifyCol(1, 110), LV_ModifyCol(2, 75), LV_ModifyCol(3, 110), LV_ModifyCol(4, 75), LV_ModifyCol(5, 100)
Gui, Show, AutoSize
return

GuiEscape:
GuiClose:
ExitApp
I can add IPv6 too if needed...
[AHK] 1.1.32.00 x64 Unicode | [WIN] 10 Pro (Version 2004) x64 | [GitHub] Profile
Donations are appreciated if I could help you
zotune
Posts: 83
Joined: 17 Nov 2014, 17:57

Re: Ping4 - Question

10 Feb 2017, 07:42

jNizM: I'm not trying to list local ports that are open. I am trying to 'ping' a remote IP with Port to verify if it is open. Kind of like 'telnet IP PORT' in cmd. This way I can see if a remote machine is running a certain program.
User avatar
jNizM
Posts: 2665
Joined: 30 Sep 2013, 01:33
GitHub: jNizM
Contact:

Re: Ping4 - Question

10 Feb 2017, 08:22

You cannot ping a port.
What you can do is try to open a socket on the remote port. If the connection works, the port is open.
Socket Class
[AHK] 1.1.32.00 x64 Unicode | [WIN] 10 Pro (Version 2004) x64 | [GitHub] Profile
Donations are appreciated if I could help you

Return to “Ask For Help”

Who is online

Users browsing this forum: Bing [Bot], punchin, scriptor2016, sobuj53, TAC109, Xeo786 and 33 guests