Jump to content

IP range


Recommended Posts

Hi I am creating an inventory script that uses WMI over the network

I have this

$iprange_from = _GUICtrlIpAddressCreate ($mainwindow, 150, 20, 125, 20)

$iprange_to = _GUICtrlIpAddressCreate ($mainwindow, 280, 20, 125, 20)

i want that my script starts at $iprange_from

and ends at $iprange_to

i can do it with a D range that meens 192.168.0.1 till 192.168.0.255

but i want to scan for example from 192.168.0.1 till 192.168.1.255

I hope you understand my question, my english isn't that perfect :)

greats MikeY

Link to comment
Share on other sites

I Have found the solution

Function: Converts a string containing an IP4 IP into a proper address

Func ip2long ($ip)
    $split = StringSplit($ip,'.')
    $long = ($split[1] * 16777216) + ($split[2] * 65536) + ($split[3] * 256) + ($split[4] * 1)
    return $long
EndFunc
oÝ÷ Ù±nËb¢{¢{Þ®Û¦º)z¶v·¬²Úø öv·¬³û¿m«­¢+Ø)Õ¹±½¹É¥À ÀÌØí±½¹¤(%¥´ÀÌØí¥ÁlÑt($ÀÌØí¥ÁlÁtô±½½È ÀÌØí±½¹¼ÄØÜÜÜÈÄؤ($ÀÌØí½ÙÈô ÀÌØí±½¹´ ÄØÜÜÜÈÄبÀÌØí¥ÁlÁt¤¤($ÀÌØí¥ÁlÅtô±½½È ÀÌØí½ÙȼØÔÔÌؤ($ÀÌØí½ÙÈô ÀÌØí½ÙÈ´ ØÔÔÌبÀÌØí¥ÁlÅt¤¤($ÀÌØí¥ÁlÉtô±½½È ÀÌØí½ÙȼÈÔؤ($ÀÌØí½ÙÈô ÀÌØí½ÙÈ´ ÈÔبÀÌØí¥ÁlÉt¤¤($ÀÌØí¥ÁlÍtô±½½È ÀÌØí½ÙȤ(%ÉÑÕɸÀÌØí¥ÁlÁtµÀìÌäì¸ÌäìµÀìÀÌØí¥ÁlÅtµÀìÌäì¸ÌäìµÀìÀÌØí¥ÁlÉtµÀìÌäì¸ÌäìµÀìÀÌØí¥ÁlÍt)¹Õ¹(oÝ÷ Øíá®*Þj׶ öv·¬²×±µº1zȧr[y¸§r[z["×kzË»x-ý¶®¶­s`¢b33c¶&ævUög&öÒÒôuT7G&ÄFG&W747&VFRb33c¶ÖçvæF÷rÂSÂ#Â#RÂ#¢b33c¶&ævU÷FòÒôuT7G&ÄFG&W747&VFRb33c¶ÖçvæF÷rÂ#Â#Â#RÂ# oÝ÷ ØÚ0Áæ«z
"hr·µëaz¶§æ®¶­s` ¦f÷"b33c¶Òb33c¶&ævUög&öÕöÆöærFòb33c¶&ævUög&öÕ÷Fð¢eTä5DôâÆöæs&b33c¶¦æW@
Link to comment
Share on other sites

  • 2 weeks later...

I Have found the solution

Function: Converts a string containing an IP4 IP into a proper address

Func ip2long ($ip)
    $split = StringSplit($ip,'.')
    $long = ($split[1] * 16777216) + ($split[2] * 65536) + ($split[3] * 256) + ($split[4] * 1)
    return $long
EndFuncoÝ÷ Ù±nËb¢{¢{Þ®Û¦º)z¶v·¬²Úø öv·¬³û¿m«­¢+ÙÕ¹±½¹É¥À ÀÌØí±½¹¤(¥´ÀÌØí¥ÁlÑt(ÀÌØí¥ÁlÁtô±½½È ÀÌØí±½¹¼ÄØÜÜÜÈÄؤ(ÀÌØí½ÙÈô ÀÌØí±½¹´ ÄØÜÜÜÈÄبÀÌØí¥ÁlÁt¤¤(ÀÌØí¥ÁlÅtô±½½È ÀÌØí½ÙȼØÔÔÌؤ(ÀÌØí½ÙÈô ÀÌØí½ÙÈ´ ØÔÔÌبÀÌØí¥ÁlÅt¤¤(ÀÌØí¥ÁlÉtô±½½È ÀÌØí½ÙȼÈÔؤ(ÀÌØí½ÙÈô ÀÌØí½ÙÈ´ ÈÔبÀÌØí¥ÁlÉt¤¤(ÀÌØí¥ÁlÍtô±½½È ÀÌØí½ÙȤ(ÉÑÕɸÀÌØí¥ÁlÁtµÀìÌäì¸ÌäìµÀìÀÌØí¥ÁlÅtµÀìÌäì¸ÌäìµÀìÀÌØí¥ÁlÉtµÀìÌäì¸ÌäìµÀìÀÌØí¥ÁlÍt)¹Õ¹
Comment: The first funtion contains ($split[4] * 1) which is a waste of resources (but an excellent inclusion to make the algorithm clear). I see the second has already been pruned. The speed savings may not be important, but who knows?
Link to comment
Share on other sites

  • 1 month later...

I've just needed these functions for little script, but i think it would be better to use bit manipulation functions :

; Converts a string containing an (IPv4) Internet Protocol dotted address into a proper address
Func ip2long($ip_str)
    Local $ip = StringSplit($ip_str, '.')
    Return BitOR(BitShift($ip[1], -24), BitShift($ip[2], -16), BitShift($ip[3], -8), $ip[4])
EndFunc  ;==>ip2long


; Converts an (IPv4) Internet network address into a string in Internet standard dotted format
Func long2ip($ip)
    Return  BitAND(BitShift($ip, 24), 255) & "." & _
            BitAND(BitShift($ip, 16), 255) & "." & _
            BitAND(BitShift($ip, 8), 255) & "." & _
            BitAND($ip, 255)
EndFunc  ;==>long2ip
Edited by psadac
Link to comment
Share on other sites

@all

This is a working example based on the code snippets

#include <GuiConstants.au3>
#include <GuiIPAddress.au3> 

opt("GUIOnEventMode", 1)

$mainwindow = GuiCreate("IP Addresses", 475, 271,(@DesktopWidth-475)/2, (@DesktopHeight-271)/2 , _ 
$WS_OVERLAPPEDWINDOW + $WS_VISIBLE + $WS_CLIPSIBLINGS)
GUISetOnEvent($GUI_EVENT_CLOSE, "GUIevent") ; Close GuiEvent

;Button Control
$cButton1=GUICtrlCreateButton ("Get IP",10,20,100,20)
GUICtrlSetOnEvent(-1,"GetIP")

$iprange_from = _GUICtrlIpAddressCreate ($mainwindow, 150, 20, 125, 20)
$iprange_to = _GUICtrlIpAddressCreate ($mainwindow, 280, 20, 125, 20)
 
GuiSetState()

While 1
    Sleep(100)
WEnd

Func GetIP()
; Now use the function to create a proper address from the ip addresses
$iprange_from_long = ip2long(_GUICtrlIpAddressGet($iprange_from))
$iprange_to_long = ip2long(_GUICtrlIpAddressGet($iprange_to))

; Now we are going to create the range 
for $i = $iprange_from_long to $iprange_to_long
    ConsoleWrite(long2ip($i)&@CRLF)
next
EndFunc

; Function: Converts a string containing an IP4 IP into a proper address
Func ip2long ($ip)
    $split = StringSplit($ip,'.')
    $long = ($split[1] * 16777216) + ($split[2] * 65536) + ($split[3] * 256) + ($split[4] * 1)
    return $long
EndFunc
 
;Function: Converts a proper address to an IP4 IP address
func long2ip ($long)
    dim $ip[4]
    $ip[0] = Floor( $long / 16777216 )
    $over = ( $long - (16777216 * $ip[0]))
    $ip[1] = Floor( $over / 65536 )
    $over = ($over - (65536 * $ip[1]))
    $ip[2] = Floor( $over / 256 )
    $over = ($over - (256 * $ip[2]))
    $ip[3] = Floor($over)
    return $ip[0] & '.' & $ip[1] & '.' & $ip[2]  & '.' & $ip[3]
EndFunc

;----------------------------- Gui Events ------------------------------------
Func GUIevent()
    Exit
EndFunc

regards,

ptrex

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