Sign in to follow this  
Followers 0
Ese

Match IP

3 posts in this topic

Hey

Is it possible to write a script that gets an arg as ip address. and then check if the first 3 octates match the first 3 in a file that looks like this:

58.147.128.0 - 58.147.159.255

more

more

more

i need to know if the ip matches that range. is it possible?

Thanks

Share this post


Link to post
Share on other sites



Hey

Is it possible to write a script that gets an arg as ip address. and then check if the first 3 octates match the first 3 in a file that looks like this:

58.147.128.0 - 58.147.159.255

more

more

more

i need to know if the ip matches that range. is it possible?

Thanks

Try this:

#include <File.au3>
#include <Array.au3>
$OPEN = FileOpenDialog("OPEN",@ScriptDir,"Text File (*.txt)",1)
$FILE = FileOpen($OPEN,0)
For $INDEX = 1 To _FileCountLines($OPEN)
    $LINE = FileReadLine($FILE,$INDEX)
    $SPLIT = StringSplit($LINE,"-")
    $IP1 = StringSplit($SPLIT[1],".")
    $IP2 = StringSplit($SPLIT[2],".")
    If StringStripWS($IP1[1],8) = StringStripWS($IP2[1],8) Then
        MsgBox(0,"MATCH","Same Range")
    EndIf
Next
FileClose($FILE)

When the words fail... music speaks

Share this post


Link to post
Share on other sites

Hey Thanks,

Thats what i needed in the end

#include <File.au3>
#include <Array.au3>

If $CmdLine[0] <> 2 Then
    MsgBox(0,"ERROR","Invalid Number Of Arguments, Please Enter 2 Arguments Correct SYNTAX: ScriptName.exe [ip] [textfile.txt]")
    Exit
EndIf   
$checkIpStr = $CmdLine[1]
$fileToCheck = $CmdLine[2]

$checkIpArray = StringSplit($checkIpStr,".")
if $checkIpArray[0] <> 4 Then
    MsgBox(0,"ERROR","Invalid First Argument, Must Be A Valid IP! SYNTAX: ScriptName.exe [ip] [textfile.txt]")
    Exit
EndIf


For $Octet = 1 To 4
    $ifnum = StringIsInt(StringStripWS($checkIpArray[$Octet],8))
    If $ifnum = 0 Then
        MsgBox(0,"ERROR","Invalid First Argument, Must Be A Valid IP! SYNTAX: ScriptName.exe [ip] [textfile.txt]")
        Exit
    EndIf
    if not (StringStripWS($checkIpArray[$Octet],8) >= 0 and StringStripWS($checkIpArray[$Octet],8) <= 255) then
        MsgBox(0,"ERROR","Invalid First Argument, Must Be A Valid IP! SYNTAX: ScriptName.exe [ip] [textfile.txt]")
        Exit
    EndIf
Next

$FILE = FileOpen($fileToCheck,0)
If $FILE = -1 Then
    MsgBox(0,"ERROR","Can't Open File: " & $fileToCheck)
    Exit
EndIf

$foundMatch = False

For $INDEX = 1 To _FileCountLines($fileToCheck)
    $LINE = FileReadLine($FILE,$INDEX)
    $SPLIT = StringSplit($LINE,"-")
    $IP1 = StringSplit($SPLIT[1],".")
    $IP2 = StringSplit($SPLIT[2],".")
    
    For $Octet = 1 To 4
        If StringStripWS($IP1[$Octet],8) <> StringStripWS($IP2[$Octet],8) Then
            If Not (StringStripWS($IP1[$Octet],8) <= StringStripWS($checkIpArray[$Octet],8) And StringStripWS($checkIpArray[$Octet],8) <= StringStripWS($IP2[$Octet],8)) Then
                ExitLoop
            EndIf
        ElseIf StringStripWS($IP1[$Octet],8) <> StringStripWS($checkIpArray[$Octet],8) Then
            ExitLoop
        EndIf
    
        If $Octet = 4 Then
        ;MsgBox(0,"MATCH","Found match in line #" & $INDEX & " , Line: " & $LINE )
            Run("Eventcreate /T ERROR /ID 155 /D Found")
            $foundMatch = True
            ExitLoop 2
        EndIf
    Next
Next

if $foundMatch = False Then
    MsgBox(0,"No MATCH","No match found")
EndIf

FileClose($FILE)

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