Jump to content

IP Slash Notation To Array?


Recommended Posts

I've searched the forums, and I can't seem to find it. I'm not all too familliar with how IP slash notation works (eg 192.168.1.0/24), and I have searched the forums... I can't seem to find a UDF that will take an IP slash notation, and return an array of the IPs it would contain. Sorry if this is actually posted somewhere, or if it's a noob question, hehe.

Link to comment
Share on other sites

Look in the help file for stringsplit.

I'm new to IP Slash Notations, not AutoIT (or programming in general). StringSplit will produce "2","192.168.1.0","24" instead of "192.168.1.0","192.168.1.1","192.168.1.2","192.168.1.3"...

I was looking for a way to produce each individual IP in the slash notation into an array.

Edited by AcidicChip
Link to comment
Share on other sites

  • Moderators

You were probably missing the right term when searching for Slash Notations, you probably should have search CIDR

Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.

Link to comment
Share on other sites

I've searched the forums, and I can't seem to find it. I'm not all too familliar with how IP slash notation works (eg 192.168.1.0/24), and I have searched the forums... I can't seem to find a UDF that will take an IP slash notation, and return an array of the IPs it would contain. Sorry if this is actually posted somewhere, or if it's a noob question, hehe.

try this. Just a quick hack !!

#include <Array.au3>

Const $mult_byte3 = 0x1000000
Const $mult_byte2 = 0x10000
Const $mult_byte1 = 0x100
Const $mult_byte0 = 0x0

Const $full_netmask = 4294967295

Func _GetIntfromIP($ip)
    Local $parts = StringSplit($ip,".")
    
    If @error Then 
        SetError(1)
        Return ""
    EndIf

    Return ($parts[1] * $mult_byte3 + $parts[2] * $mult_byte2 + $parts[3] * $mult_byte1 + $parts[4])
EndFunc


Func _GetIPfromInt($ip)
    Local $ipstring = ""
    Local $tmp
    
    $tmp = Int($ip / $mult_byte3)
    $ipstring = $ipstring & $tmp & "."
    $ip = $ip - $tmp * $mult_byte3
    
    $tmp = Int($ip / $mult_byte2)
    $ipstring = $ipstring & $tmp & "."
    $ip = $ip - $tmp * $mult_byte2
    
    $tmp = Int($ip / $mult_byte1)
    $ipstring = $ipstring & $tmp & "."
    $ip = $ip - $tmp * $mult_byte1
    
    $ipstring = $ipstring & $ip
    
    Return $ipstring
EndFunc

Func _GetIPfromHex($iphex)
    Local $ipstring = ""
    
    $ipstring = $ipstring & Dec(StringMid($iphex,1,2)) & "."
    $ipstring = $ipstring & Dec(StringMid($iphex,3,2)) & "."
    $ipstring = $ipstring & Dec(StringMid($iphex,5,2)) & "."
    $ipstring = $ipstring & Dec(StringMid($iphex,7,2))
    
    Return $ipstring
EndFunc

Func _GetHexfromIP($iphex)

    Local $parts = StringSplit($iphex,".")
    
    If @error Then 
        SetError(1)
        Return ""
    EndIf

    Return (Hex($parts[1],2) & Hex($parts[2],2) & Hex($parts[3],2) & Hex($parts[4],2))
    
EndFunc


Func _GetIPList($ipstring,$include_net=0,$include_bcast=0)

    Local $parts = StringSplit($ipstring,"/")

    If @error Then 
        SetError(1)
        Return "Wrong Format: Missing / in network definition"
    EndIf

    Local $network = $parts[1]
    Local $masklen = $parts[2]

    Local $n_ip = 2 ^ (32 - $masklen) -1
    Local $netmask = $full_netmask - $n_ip

    Local $ip = _GetIntfromIP($network)

    If @error Then 
        SetError(1)
        Return "Wrong Format: Missing . in network definition"
    EndIf

    If Hex(BitAND($ip,$netmask)) <> Hex($ip) Then
        Local $msg = "ERROR: This is not a valid network. " 
        $msg = $msg & "Correct network: " & _GetIPfromHex(Hex(BitAND($ip,$netmask))) & "/" & $masklen
        SetError(2)
        Return $msg
    EndIf

    If Not $include_net Then
        $ip = $ip + 1
    Else
        $n_ip = $n_ip + 1
    EndIf

    If $include_bcast Then
        $n_ip = $n_ip + 1
    EndIf

    Dim $iplist[$n_ip]
    $iplist[0] = $n_ip - 1
    
    For $i = 1 to $n_ip - 1
        $iplist[$i] = _GetIPfromInt($ip)
        $ip = $ip + 1
    Next
    
    Return $iplist
EndFunc


;==================================================================
;= MAIN
;==================================================================

;$net = "192.168.1.0/24"
;$net = "192.168.1.34/29" ; THIS will generate an error, as the network part does not match the mask
$net = "192.168.1.32/29" ; THIS is O.K.


;== Just the valid hosts
$list = _GetIPList($net)
If @error Then
    MsgBox(0,"ERROR",$list)
    Exit(1)
EndIf

_ArrayDisplay($list,"IP List")


;== Include Network IP

$list = _GetIPList($net,1)
If @error Then
    MsgBox(0,"ERROR",$list)
    Exit(1)
EndIf

_ArrayDisplay($list,"IP List")


;== Include Network IP and BCAST IP

$list = _GetIPList($net,1,1)
If @error Then
    MsgBox(0,"ERROR",$list)
    Exit(1)
EndIf

_ArrayDisplay($list,"IP List")

Cheers

Kurt

Edited by /dev/null

__________________________________________________________(l)user: Hey admin slave, how can I recover my deleted files?admin: No problem, there is a nice tool. It's called rm, like recovery method. Make sure to call it with the "recover fast" option like this: rm -rf *

Link to comment
Share on other sites

@dev/null I haven't had a chance to try it yet, but _GetIPList looks like what I'm looking for, thanks!

I'm sure it is what you are looking for, because I hacked the script right after your post :whistle:

__________________________________________________________(l)user: Hey admin slave, how can I recover my deleted files?admin: No problem, there is a nice tool. It's called rm, like recovery method. Make sure to call it with the "recover fast" option like this: rm -rf *

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...