Sign in to follow this  
Followers 0
Alek

Lan scanner

1 post in this topic

Was diging trough some old scrips and I found my old multi-process lan scanner and I thought it would be a nice script for the example forum :(

How it works:

the main script runs another script with a few parameters (IP and main scripts form handle)

and the other script executes the ping command and uses SendMessage to send the result back to the main script.

Orignaly the main script just re-ran it self with the same parameters but it was to much of a cpu and memory hog so I had to create a seperate script for the pinging.

so here it is

main script.

#include <iNet.au3>
#Include <GuiListView.au3>
#include <GUIConstantsEx.au3>
#include <StaticConstants.au3>
#include <WindowsConstants.au3>
#Include <GuiStatusBar.au3>

Global $ProcLimit = RegRead("HKEY_CURRENT_USER\Software\LanScanner\Settings","ProcessLimit")
Global $UseExternalPinger = False

If $ProcLimit = 0 Then
    $ProcLimit = 20
EndIf

#Region RealPing.au3
#Include <SendMessage.au3>
#NoTrayIcon

Global $PingTimeout = RegRead("HKEY_CURRENT_USER\Software\LanScanner\Settings","PingTimeout")

If $PingTimeout = 0 Then
    $PingTimeout = 1000
EndIf

If $Cmdline[0] = 2 Then
    $ping = Ping($Cmdline[1], $PingTimeout)
    If $ping = 0 Then
        _SendMessage($Cmdline[2], 0x0F01)
    Else
        _SendMessage($Cmdline[2], 0x0F00, _IpToHex($Cmdline[1]), String($ping))
    EndIf

    Exit
EndIf


Func _IpToHex($Ip)
    Local $array = StringSplit($Ip, ".")
    Return "0x" & Hex($array[1], 2) & Hex($array[2], 2) & Hex($array[3], 2) & Hex($array[4], 2)
EndFunc
#EndRegion

TCPStartup()

Global $RunTimer = 0

$Form = GUICreate("Lan scanner", 470, 175)

$Menu_File = GUICtrlCreateMenu("&File")
$Menu_Exit = GUICtrlCreateMenuItem("&Exit", $Menu_File)

$Menu_Tools = GUICtrlCreateMenu("&Tools")
$Menu_Settings = GUICtrlCreateMenuItem("&Settings", $Menu_Tools)

$ListView =_GUICtrlListView_Create($Form, "IP|Name|Ping", 10, 10, 290, 115)
_GUICtrlListView_SetExtendedListViewStyle($ListView, BitOR($LVS_EX_FULLROWSELECT,$LVS_EX_DOUBLEBUFFER, $LVS_EX_GRIDLINES))

GUICtrlCreateLabel("Start IP:", 310, 13)
$Input1 = GUICtrlCreateInput(@IPAddress1, 360, 10, 100)
GUICtrlCreateLabel("End IP:", 310, 43)
$Input2 = GUICtrlCreateInput(@IPAddress1, 360, 40, 100)

$Button1 = GUICtrlCreateButton("Start", 310, 70, 150, 25)
$Progress1 = GUICtrlCreateProgress(4, 0, -1, -1)

_GUICtrlListView_SetColumnWidth($Listview, 0, 100)
_GUICtrlListView_SetColumnWidth($Listview, 1, 150)
_GUICtrlListView_SetColumnWidth($Listview, 2, 40)

$StatusBar = _GUICtrlStatusBar_Create($Form)
Dim $StatusBar_Parts[4] = [150, 250, 370, -1]
_GUICtrlStatusBar_SetParts ($StatusBar, $StatusBar_Parts)
_GUICtrlStatusBar_SetText($StatusBar, "Running Process: 0")
_GUICtrlStatusBar_SetText($StatusBar, "IP: ", 1)
_GUICtrlStatusBar_EmbedControl($StatusBar, 3, GUICtrlGetHandle($Progress1))
GUIRegisterMsg(0x0F00, "_PingSucess")
GUIRegisterMsg(0x0F01, "_PingDone")

GUIRegisterMsg($WM_NOTIFY, "WM_NOTIFY")


GUISetState()

$Form2 = GUICreate("Settings", 400, 190)
$Label1 = GUICtrlCreateLabel("Number of active processes at one time: " & $ProcLimit, 10, 10,380, 20)
$Slider1 = GUICtrlCreateSlider(10, 30, 380, 40)
GUICtrlSetLimit($Slider1, 40, 0)
GUICtrlSetData($Slider1, $ProcLimit / 5)

$Label2 = GUICtrlCreateLabel("Ping Timeout in milliseconds: 4000", 10, 80,380, 20)
$Slider2 = GUICtrlCreateSlider(10, 100, 380, 40)
GUICtrlSetLimit($Slider2, 100, 1)
GUICtrlSetData($Slider2, $PingTimeout / 50)

$Button2 = GUICtrlCreateButton("Apply", 280, 155, 50, 25)
$Button3 = GUICtrlCreateButton("Close", 340, 155, 50, 25)
GUISetState(@SW_HIDE)

Global $ip = 0
Dim $ProcCount = 0
Dim $Run = False
Dim $SliderData1 = $ProcLimit
Dim $SliderData2 = $PingTimeout

While True
    $Msg = GUIGetMsg(1)
    Switch $Msg[1]
        Case $Form
            Switch $Msg[0]
                Case $GUI_EVENT_CLOSE, $Menu_Exit
                    Exit
                Case $Button1
                    If $ip = 0 Then
                        _GUICtrlListView_DeleteAllItems($Listview)
                        $ip = GUICtrlRead($Input1)
                        GUICtrlSetState($Input1, $GUI_DISABLE)
                        GUICtrlSetState($Input2, $GUI_DISABLE)
                        GUICtrlSetData($Button1, "Stop")
                        $Run = True
                        $RunTimer = TimerInit()
                    Else
                        $ip = 0
                        GUICtrlSetState($Input1, $GUI_ENABLE)
                        GUICtrlSetState($Input2, $GUI_ENABLE)
                        GUICtrlSetData($Button1, "Start")
                        GUICtrlSetData($Progress1, 0)
                        $Run = False
                    EndIf
                Case $Menu_Settings
                    GUISetState(@SW_SHOW, $Form2)
                Case Else
                    If $Msg[0] > 0 Then ConsoleWrite($Msg[0] & @CRLF)
            EndSwitch

        Case $Form2
            Switch $Msg[0]
                Case $GUI_EVENT_CLOSE, $Button3
                    GUISetState(@SW_HIDE, $Form2)
                Case $Button2
                    If GUICtrlRead($Slider1) = 0 Then
                        RegWrite("HKEY_CURRENT_USER\Software\LanScanner\Settings","ProcessLimit", "REG_DWORD", 1)
                        $ProcLimit = 1
                    Else
                        RegWrite("HKEY_CURRENT_USER\Software\LanScanner\Settings","ProcessLimit", "REG_DWORD", GUICtrlRead($Slider1) * 5)
                        $ProcLimit = GUICtrlRead($Slider1) * 5
                    EndIf

                    RegWrite("HKEY_CURRENT_USER\Software\LanScanner\Settings","PingTimeout", "REG_DWORD", GUICtrlRead($Slider2) * 50)

                    GUISetState(@SW_HIDE, $Form2)
                Case $Slider1
                    If GUICtrlRead($Slider1) = 0 Then
                        GUICtrlSetData($Label1, "Number of active processes at one time: 1")
                    Else
                        GUICtrlSetData($Label1, "Number of active processes at one time: " & GUICtrlRead($Slider1) * 5)
                    EndIf

            EndSwitch

    EndSwitch

    If $SliderData1 <> GUICtrlRead($Slider1) Then
        $SliderData1 = GUICtrlRead($Slider1)
        If GUICtrlRead($Slider1) = 0 Then
            GUICtrlSetData($Label1, "Number of active processes at one time: 1")
        Else
            GUICtrlSetData($Label1, "Number of active processes at one time: " & GUICtrlRead($Slider1) * 5)
        EndIf

    EndIf

    If $SliderData2 <> GUICtrlRead($Slider2) Then
        $SliderData2 = GUICtrlRead($Slider2)
        GUICtrlSetData($Label2,"Ping Timeout in milliseconds: " & GUICtrlRead($Slider2) * 50)
    EndIf

    If Not $Run Or $ProcCount >= $ProcLimit Then ContinueLoop

    If $UseExternalPinger Then
        ;We get signicent effincy by running a external script rather then re-running this 1.
        If @Compiled Then
            $Pid = Run("RealPing.exe" & " " & $ip & " " & $Form, @WorkingDir)
        Else
            $Pid = Run(@AutoItExe & ' "' & @ScriptDir & "\RealPing.au3" & '" ' & $ip & " " & $Form, @WorkingDir)
        EndIf
    Else
        If @Compiled Then
            $Pid = Run(@AutoItExe & " " & $ip & " " & $Form, @WorkingDir)
        Else
            $Pid = Run(@AutoItExe & ' "' & @ScriptFullPath & '" ' & $ip & " " & $Form, @WorkingDir)
        EndIf
    EndIf
    If $Pid <> 0 Then
        ;ProcessSetPriority($Pid, 0)
    EndIf

    _GUICtrlStatusBar_SetText($StatusBar, "IP: " & $ip, 1)
    _GUICtrlStatusBar_SetText($StatusBar, _MsToTime(TimerDiff($RunTimer)), 2)
    $ProcCount += 1
    GUICtrlSetData($Progress1, _IPGetPercentDiff($ip, GUICtrlRead($Input2)))
    If _IpGetNext($ip, GUICtrlRead($Input1), GUICtrlRead($Input2)) = 1 Then
        $ip = 0

        GUICtrlSetState($Input1, $GUI_ENABLE)
        GUICtrlSetState($Input2, $GUI_ENABLE)
        GUICtrlSetData($Button1, "Start")

        $Run = False
    EndIf

    _GUICtrlStatusBar_SetText($StatusBar, "Running Process: " & $ProcCount & " / " & $ProcLimit)
WEnd

Func _PingSucess($hWnd, $Msg, $wParam, $lParam)
    Local $index = _GUICtrlListView_AddItem($Listview, _HexToIp($wParam), 0)
    _GUICtrlListView_AddSubItem($Listview, $index, _TCPIpToName(_HexToIp($wParam)), 1)
    _GUICtrlListView_AddSubItem($Listview, $index, Int("0x" & Hex($lParam)), 2)
    $ProcCount -= 1

    _GUICtrlStatusBar_SetText($StatusBar, "Running Process: " & $ProcCount & " / " & $ProcLimit)
EndFunc

Func _PingDone($hWnd, $Msg, $wParam, $lParam)
    $ProcCount -= 1

    _GUICtrlStatusBar_SetText($StatusBar, "Running Process: " & $ProcCount & " / " & $ProcLimit)
EndFunc

Func WM_NOTIFY($hWnd, $iMsg, $iwParam, $ilParam)

    Local $hWndFrom, $iCode, $tNMHDR, $hWndListView
    $hWndListView = $ListView
    If Not IsHWnd($ListView) Then $WndListView = GUICtrlGetHandle($ListView)

    $tNMHDR = DllStructCreate($tagNMHDR, $ilParam)
    $hWndFrom = HWnd(DllStructGetData($tNMHDR, "hWndFrom"))
    $iCode = DllStructGetData($tNMHDR, "Code")

    Switch $hWndFrom
    Case $hWndListView
    Switch $iCode

    Case $NM_DBLCLK ; Sent by a list-view control when the user double-clicks an item with the left mouse button
    Local $tInfo = DllStructCreate($tagNMITEMACTIVATE, $ilParam)

    $Index = DllStructGetData($tInfo, "Index")

    $subitemNR = DllStructGetData($tInfo, "SubItem")


    ; make sure user clicks on the listview & only the activate
    If $Index <> -1 Then

    ; col1 ITem index
    $item = StringSplit(_GUICtrlListView_GetItemTextString($ListView, $Index),'|')
    $item = $item[1]
                        $ping = Ping($item, 1000)
                        If $ping = 0 Then
                            Switch @error
                                Case 1
                                    _GUICtrlListView_SetItemText($Listview, $Index, "Off", 2)
                                Case 2
                                    _GUICtrlListView_SetItemText($Listview, $Index, "Non", 2)
                                Case 3
                                    _GUICtrlListView_SetItemText($Listview, $Index, "Bad", 2)
                                Case 4
                                    _GUICtrlListView_SetItemText($Listview, $Index, "Err", 2)
                            EndSwitch
                        Else
                            _GUICtrlListView_SetItemText($Listview, $Index, $ping, 2)
                        EndIf


    EndIf

    EndSwitch
    EndSwitch
    Return $GUI_RUNDEFMSG
EndFunc ;==>WM_NOTIFY

Func _HexToIp($Hex)
    Local $array = StringSplit($Hex, "")
    If StringLeft($Hex, 2) = "0x" Then
        Return Int("0x" & $array[3] & $array[4]) & "." & Int("0x" & $array[5] & $array[6]) & "." & Int("0x" & $array[7] & $array[8]) & "." & Int("0x" & $array[9] & $array[10])
    Else
        Return Int("0x" & $array[1] & $array[2]) & "." & Int("0x" & $array[3] & $array[4]) & "." & Int("0x" & $array[5] & $array[6]) & "." & Int("0x" & $array[7] & $array[8])
    EndIf
EndFunc

Func _IpGetDiff($ip1, $ip2)
    Local $ret[4]

    If _IpGetValue($ip1) > _IpGetValue($ip2) Then
        Return $ret
    EndIf

    Local $array1 = StringSplit($ip1, ".")
    Local $array2 = StringSplit($ip2, ".")

    $ret[0] = $array2[1] - $array1[1]
    $ret[1] = $array2[2] - $array1[2]
    $ret[2] = $array2[3] - $array1[3]
    $ret[3] = $array2[4] - $array1[4]

    Return $ret
EndFunc

Func _IpGetNext(ByRef $ip, $ip1, $ip2)
    Local $array = _IpGetDiff($ip, $ip2)
    Local $iparray = _IpGetDiff("0.0.0.0", $ip)
    Local $iparray1 = _IpGetDiff("0.0.0.0", $ip1)

    If $array[3] = 0 Then
        If $array[2] = 0 Then
            If $array[1] = 0 Then
                If $array[0] = 0 Then
                    Return 1
                Else
                    $iparray[0] += 1
                    $iparray[1] = $iparray1[1]
                    $iparray[2] = $iparray1[2]
                    $iparray[3] = $iparray1[3]
                EndIf
            Else
                $iparray[1] += 1
                $iparray[2] = $iparray1[2]
                $iparray[3] = $iparray1[3]
            EndIf
        Else
            $iparray[2] += 1
            $iparray[3] = $iparray1[3]
        EndIf
    Else
        $iparray[3] += 1
    EndIf
    $ip = $iparray[0] & "." & $iparray[1] & "." & $iparray[2] & "." & $iparray[3]
    Return 0
EndFunc

Func _IpGetValue($ip)
    Local $array = StringSplit($Ip, ".")
    Return $array[1] + $array[2] + $array[3] + $array[4]
EndFunc

Func _IPGetPercentDiff($ip, $ip2)
    Local $array = StringSplit($ip, ".")
    Local $array2 = StringSplit($ip2, ".")

    $array[4] = ($array[4] + 1) / ($array2[4] + 1)
    $array[3] = ($array[3] + 1) / ($array2[3] + 1)
    $array[2] = ($array[2] + 1) / ($array2[2] + 1)
    $array[1] = ($array[1] + 1) / ($array2[1] + 1)

    Return $array[4] * $array[3] * $array[2] * $array[1] * 100
EndFunc

Func _Swap(ByRef $1, ByRef $2)
    Local $tmp = $2
    $2 = $1
    $1 = $tmp
EndFunc

Func _HwndToInt($hWnd)
    Return Int("0x" & Hex($hWnd))
EndFunc

Func _MsToTime($ms)

    Local $sec = Floor($ms / 1000)
    Local $hour = Floor($sec / 3600)
    $sec -= $hour * 3600
    Local $min = Floor($sec / 60)

    $sec-= $min * 60

    Return StringFormat("%02d:%02d:%02d", $hour, $min, $sec)
EndFunc

Real Pinger.au3

#Include <SendMessage.au3>
#NoTrayIcon
Global $PingTimeout = RegRead("HKEY_CURRENT_USER\Software\LanScanner\Settings","PingTimeout")

If $PingTimeout = 0 Then
    $PingTimeout = 1000
EndIf

If $Cmdline[0] = 2 Then
    $ping = Ping($Cmdline[1], $PingTimeout)
    If $ping = 0 Then
        _SendMessage($Cmdline[2], 0x0F01)
    Else
        _SendMessage($Cmdline[2], 0x0F00, _IpToHex($Cmdline[1]), String($ping))
    EndIf

    Exit
EndIf

Func _IpToHex($Ip)
    Local $array = StringSplit($Ip, ".")
    Return "0x" & Hex($array[1], 2) & Hex($array[2], 2) & Hex($array[3], 2) & Hex($array[4], 2)
EndFunc

[font="Impact"]Never fear, I is here.[/font]

Share this post


Link to post
Share on other sites



Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0