Pseudo-Multi-threaded IP scanner

Post your working scripts, libraries and tools for AHK v1.1 and older
Jeramy
Posts: 20
Joined: 18 Feb 2015, 05:42

Pseudo-Multi-threaded IP scanner

03 Mar 2015, 23:30

I know this will seem like amateur hour for some of you, but I'm just putting this here (mostly because it was something I didn't see existing when searching).

This is a simple pair of scripts which will have "threaded" IP scanning. The first script calls the second script in a loop. The second script will return the IP, average latency, and hostname (if resolved). It even has basic subnet functionality.

Code: Select all

#SingleInstance Force
SetWorkingDir %A_ScriptDir%
TestFile = IPScanLog.csv
filedelete %TempFile%

Gui, Add, Text,,Please enter starting IP address
Gui, Add, Edit, w40 number limit3 vOct1
Gui, Add, Text,x+,*
Gui, Add, Edit, w40 x+ number limit3 vOct2
Gui, Add, Text,x+,*
Gui, Add, Edit, w40 x+ number limit3 vOct3
Gui, Add, Text,x+,*
Gui, Add, Edit, w40 x+ number limit3 vOct4
Gui, Add, Text,y+10 x9,Scan how many IPs?`n(`/subnet)
Gui, Add, Edit, w40 x+ vHowMany
Gui, Add, Text, y+10 x9,Ping#
Gui, Add, Edit, W20 x+ number limit2 vPingNumber,4
Gui, Add, Text, x+,Timeout
Gui, Add, Edit, W40 x+ number limit4 vTimeout,1000
Gui, Add, Button, x+5 r1,Scan range
Gui, Color, Gray
Gui, Add, Statusbar
SB_SetParts(85,60)
SB_SetText("`t"A_IPAddress1,1)
SB_SetText("`t"A_ComputerName,2)
SB_SetText("`t"A_UserName,3)
Gui, Color, Gray
Gui Show,,IP Scanner
Return

buttonScanRange:
gui, submit,nohide
StartIP = %Oct1%.%Oct2%.%Oct3%.%Oct4%
if instr(HowMany,"/")
     {
     Howmany := SubStr(HowMany,2)
     HowMany := 32 - HowMany
     HowMany := 2**howmany
     }
FinalRange := Oct4 + HowMany
if ( ( PingNumber = "")  || ( PingNumber = "0" ) )
     {
     PingNumber = 1
     }
if ( Timeout = "" )
     {
     Timeout = 50
     }
if ( Timeout <= 50 )
     {
     Timeout = 50
     }
loop %HowMany%
{
IPAddress = %Oct1%.%Oct2%.%Oct3%.%Oct4%
run Threaded.AHK %IPAddress% %PingNumber% %TimeOut%
     Oct4++
     If Oct4 > 255
          {
          Oct4 = 0
          Oct3++
          }
     If Oct3 > 255
          {
          Oct3 = 0
          Oct2++
          }
     If Oct2 > 255
          {
          Oct2 = 0
          Oct1++
          }
sleep 250
}
msgbox Done
Return

guiclose:
exitapp

Escape::ExitApp

Code: Select all

SetWorkingDir %A_ScriptDir%
#NoTrayIcon
IP = %1%
PingNumber = %2%
TimeOut = %3%

TempFile = %IP%.temp
Temp2 = %IP%.temp2
TestFile = IPScanLog.csv

Count = -n %PingNumber%

runwait %comspec% /c ping %IP% %Count% -w %TimeOut% | find "Average" > %Temp2%,,hide
If !Errorlevel
     {
     runwait %comspec% /c nslookup %ip%| find "Name" > %tempfile%,,hide
          loop, read, %tempfile%
               if instr(a_loopreadline,"Name")
                    {
                    StringReplace,Name,A_LoopReadLine,Name:
                    Name := trim(Name)
                    }
     if Name =
          Name = No Matching Name Found
          Loop, read, %Temp2%
               if instr(a_loopreadline,"Average")
               {
               Average := trim(A_loopReadLine)
               AvgPos := InStr(Average,"Average")
               AvgPos := AvgPos + 9
               StringTrimLeft,Average,Average,%AvgPos%
               }
     FileAppend,%IP%`,%average%`,%Name%`n,%TestFile%
               }
FileDelete,%TempFile%
FileDelete,%Temp2%
exitapp
If I could have the output from cmd piped directly into a variable (without using other external tools), the whole process would be sped up tremendously. In the main loop, you can add a sleep if you find that the system is bogging down too much. Right now, it takes less than a minute to scan an entire /24 subnet.

Edit: Added some more functionality. The ability to pick number of pings, timeouts, add a statusbar with useful details (in some situations, could probably loop A_IPAddress%A_Index% to find which ones don't return blank and show that in the status bar. Maybe make the UI a bit cleaner).
Last edited by Jeramy on 04 Mar 2015, 07:55, edited 2 times in total.
User avatar
Soft
Posts: 174
Joined: 07 Jan 2015, 13:18
Location: Seoul
Contact:

Re: Pseudo-Multi-threaded IP scanner

04 Mar 2015, 02:41

seems useful!
AutoHotkey & AutoHotkey_H v1.1.22.07
geek
Posts: 1055
Joined: 02 Oct 2013, 22:13
Location: GeekDude
Contact:

Re: Pseudo-Multi-threaded IP scanner

04 Mar 2015, 17:33

I thought the term was "Multiprocessing", but wikipedia has this to say about that
Wlklpedla wrote:At the operating system level, multiprocessing is sometimes used to refer to the execution of multiple concurrent processes in a system as opposed to a single process at any one instant.[7][8] When used with this definition, multiprocessing is sometimes contrasted with multitasking, which may use just a single processor but switch it in time slices between tasks (i.e. a time-sharing system). Multiprocessing however means true parallel execution of multiple processes using more than one processor.[8] Multiprocessing doesn't necessarily mean that a single process or task uses more than one processor simultaneously; the term parallel processing is generally used to denote that scenario.[7] Other authors prefer to refer to the operating system techniques as multiprogramming and reserve the term multiprocessing for the hardware aspect of having more than one processor.[9][2] The remainder of this article discusses multiprocessing only in this hardware sense.
In this light, I'd say that multiprogramming would be the term to use. However, that very well could be more confusing than other terms.

Return to “Scripts and Functions (v1)”

Who is online

Users browsing this forum: No registered users and 252 guests