Jump to content

is there a faster way to ping?


Recommended Posts

I'm writing a small LAN scanner to fit my purpose but I find the classical ping command is too slow.

As a work around, I'm planning to ask the user to limit the range of IPs to be scanned but I thinks it is always better if I can develop or fnd a faster way to do this.

#include <Constants.au3>

$vGateway = _IPConfig()
_ScanLAN($vGateway)

Func _IPConfig()
    Local $iPID, $sOutput = ''
    $iPID = Run(@ComSpec & ' /c ipconfig', @SystemDir, @SW_HIDE, $STDOUT_CHILD + $STDERR_CHILD)
    While 1
        $sOutput = StdoutRead($iPID)
        If @error Then ExitLoop
        if StringInStr($sOutput," Default Gateway . . . . . . . . . : ") Then
            $sOutput=StringMid($sOutput,StringInStr($sOutput," Default Gateway . . . . . . . . . : ") + StringLen(" Default Gateway . . . . . . . . . : "), 15)
            Return $sOutput
            ExitLoop
        EndIf
    WEnd
EndFunc

Func _ScanLAN($vGatewayIP)
    Local $iPID, $sOutput = ''
    local $array=StringSplit($vGatewayIP,".")
    $BaseIP = $array[1] & "." & $array[2] & "." & $array[3]  & "."

    $iPID = Run('"' & @ComSpec & '"', '', @SW_HIDE, $STDERR_MERGED + $STDIN_CHILD)

    for $i = 100 to 150
        $command = "ping -a -n 1 " & $BaseIP & $i
        ConsoleWrite($command & @CRLF)
        StdinWrite($iPID,$command & @CRLF)
        While 1
            $sOutput = StdoutRead($iPID)
            If @error Then ExitLoop
            if StringRight($sOutput,1) = ">" Then ExitLoop

            if StringInStr($sOutput,"Pinging ") Then
                If StringInStr($sOutput, "[") Then
                    $StartLocation = StringInStr($sOutput,"Pinging ") + StringLen("Pinging ")
                    $EndLocation = StringInStr($sOutput, "[")
                    $sOutput=StringMid($sOutput,$StartLocation, $EndLocation - $StartLocation)
                    ConsoleWrite ($sOutput & @CRLF)
                    ExitLoop
                EndIf
            EndIf
        WEnd
    Next
EndFunc

here the code I wrote:

Link to comment
Share on other sites

hamohd70, you _IPConfig() function only works with an english OS. :(

It might be interesting to not use an external program to get the gateway, no ? And careful, sometimes there are several gateways (when there is more than one NIC - with laptop for example)

Here is a small code, using registry to retrieve all gateways :

#Include <Array.au3>

Local $sKey = "HKLM\SYSTEM\CurrentControlSet\services\Tcpip\Parameters\Interfaces"
Local $i = 0, $n = 0
Local $aGateways[1]
Local $sGateway, $sInterfaces, $sDefaultGateway, $sDhcpDefaultGateway

While 1
    $sGateway = ""
    $sInterfaces = RegEnumKey($sKey , $i + 1)
    If @error Then ExitLoop


    $sDefaultGateway = RegRead($sKey & "\" & $sInterfaces, "DefaultGateway")
    $sDhcpDefaultGateway = RegRead($sKey & "\" & $sInterfaces, "DhcpDefaultGateway")

    If $sDefaultGateway <> "" Then
        $sGateway = $sDefaultGateway
    ElseIf $sDhcpDefaultGateway <> "" Then
        $sGateway = $sDhcpDefaultGateway
    EndIf

    If $sGateway <> "" Then
        $n += 1
        Redim $aGateways[$n]
        $aGateways[$n - 1] = $sGateway
    EndIf

    $i += 1
WEnd


_ArrayDisplay($aGateways)
Link to comment
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
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...