ScanSubnet() - Fast [WMI] IP Scanner, Ping() [WMI]

Post your working scripts, libraries and tools
CyL0N
Posts: 210
Joined: 27 Sep 2018, 09:58

ScanSubnet() - Fast [WMI] IP Scanner, Ping() [WMI]

27 Dec 2018, 08:08

I needed a script to constantly check for other computers on network for automated throttling of my torrent seedbox, and so...

ScanSubnet() - Fast [WMI] IP Scanner

Code: Select all

;ScanSubnet() Basically Does what all those IP scanners do,with WMI...Pretty Darn Fast Too,almost as fast as Nmap.

Msgbox % ScanSubnet()	;scan all active network adapters for active IP's... if no ip's were specified...
;Msgbox % ScanSubnet("192.168.2.1 192.168.43.1 192.168.56.1")	;find all ip's in the specified address subnets...ex. 192.168.2.(1-255)


ScanSubnet(addresses:="") {	;pings IP ranges & returns active IP's
	BL := A_BatchLines
	SetBatchLines, -1
	If !addresses{	;scan all active network adapters/connections for active IP's... if no ip's were specified...
		colItems := ComObjGet( "winmgmts:" ).ExecQuery("Select * from Win32_NetworkAdapterConfiguration WHERE IPEnabled = True")._NewEnum
		while colItems[objItem]
			addresses .= addresses ? " " objItem.IPAddress[0] : objItem.IPAddress[0]
	}
	
	Loop, Parse, addresses, % A_Space
	{
		If ( A_LoopField && addrArr := StrSplit(A_LoopField,".") )
			Loop 256
				addr .= addr ? A_Space "or Address = '" addrArr.1 "." addrArr.2 "." addrArr.3 "." A_Index-1 "'" : "Address = '" addrArr.1 "." addrArr.2 "." addrArr.3 "." A_Index-1 "'"
		colPings := ComObjGet( "winmgmts:" ).ExecQuery("Select * From Win32_PingStatus where " addr "")._NewEnum
		While colPings[objStatus]
			rVal .= (oS:=(objStatus.StatusCode="" or objStatus.StatusCode<>0)) ? "" : (rVal ? "`n" objStatus.Address : objStatus.Address)
		addr := ""	;the quota on Win32_PingStatus prevents more than roughtly two ip ranges being scanned simultaneously...so each range is scanned individually.
	}
	SetBatchLines, % BL
	Return rVal
}

Ping(), Ping0() - WMI Ping

Code: Select all

Msgbox % Ping0("www.google.com")Ping0("www.youtube.com")Ping0("www.idontexist.org")

For k, v in Ping("www.google.com www.yahoo.com www.autohotkey.com 192.168.2.1 www.idontexist.org")
	MsgBox % v.1 "`n" v.2

Msgbox % Ping("www.google.com")[1].1
Msgbox % Ping("127.0.0.1")[1].1 	; replace with your host ip

Ping(addresses) {
	rVal := []
	Loop, Parse, addresses, % A_Space
		addr .= addr ? A_Space "or Address = '" A_LoopField "'" : "Address = '" A_LoopField "'"
	colPings := ComObjGet( "winmgmts:" ).ExecQuery("Select * From Win32_PingStatus where " addr "")._NewEnum
	While colPings[objStatus]
		rVal.Push( [((oS:=(objStatus.StatusCode="" or objStatus.StatusCode<>0)) ? "0" : "1" ), objStatus.Address] )
	Return rVal
}

Ping0(addr) {	;ping only one addresses, alternative for a simplified return value,but only one address supported
	colPings := ComObjGet( "winmgmts:" ).ExecQuery("Select * From Win32_PingStatus where Address = '" addr "'")._NewEnum
	While colPings[objStatus]
		Return ((oS:=(objStatus.StatusCode="" or objStatus.StatusCode<>0)) ? "0" : "1")
}

Cheers & Happy Holidays.
Last edited by CyL0N on 29 Dec 2018, 10:15, edited 1 time in total.
live ? long & prosper : regards
User avatar
niczoom
Posts: 76
Joined: 09 Mar 2016, 22:17

Re: ScanSubnet() - Fast [WMI] IP Scanner, Ping() [WMI]

19 May 2019, 23:07

Thanks for the script, the true/false response works well. I think it needs a timeout period. Seems to timeout at just under 4 seconds if the result times out. just me has a ping function here https://www.autohotkey.com/boards/viewtopic.php?t=552 which works a little faster and has a timeout feature. But its much more code.
[AHK] 1.1.23.05 x32 Unicode
[WIN] 10 Pro x64 Version 5111 (Build 10586.218)

Return to “Scripts and Functions”

Who is online

Users browsing this forum: Bill, JoeWinograd and 21 guests