#199778 - 2010-09-06 06:48 AM
KIX script based on IP network subnet
|
airofu
Just in Town
Registered: 2010-09-06
Posts: 3
Loc: Australia
|
Reading through it seems there are a few ways to to the same thing in KIX Just wondering what is the most basic KIX script/command to acheive the following:
Map a network printer based on the user's current IP subnet. (Laptops move to different sites but need only the local subnet printer mapped). Thanks
|
|
Top
|
|
|
|
#199782 - 2010-09-06 02:31 PM
Re: KIX script based on IP network subnet
[Re: Mart]
|
Glenn Barnas
KiX Supporter
   
Registered: 2003-01-28
Posts: 4402
Loc: New Jersey
|
Sorry, but Mart's example of using string comparisons is an overly simplistic method of network subnet identification, and only works if you are on a classful boundary. This is fairly unlikely in mid to large size networks.
The first thing to do is to convert your IP address into a normalized dotted-decimal string: $MyIP = Join(Split(@IPADDRESS0, ' '), '') This removes the spaces from the @IPADDRESS0 macro. Note that use of this macro may be convenient, but if your system has multiple IP addresses, or is multihomed, it may not return the address that you are interested in. There are UDFs that can query the tcp/ip configuration and return all of the bound IP addresses from all adapters. Just something to be aware of when that one stubborn system doesn't work like all the rest. 
As for network comparisons, what happens when you have a CIDR subnet? Something other than a natural Class A, B, or C? (Realize that 10.1.2.3/24 is NOT a class C simply because the mask is 255.255.255.0!) What about 192.168.16.128 / 255.255.252.0? There are two UDFs that perform CIDR network calculations that make "In Subnet" type of procedures easy.
The first is fairly simple - InSubnet("address", "network/mask") - will return True if the "address" is in the defined "network/mask". This is all done with mathematical calculations.. the "address" is converted to a double-precision number. The Network is also converted to a number, and the mask is used to calculate the end address of the network. From there, it's a simple task to determine if the address is between the network's starting and ending addresses. There's no strange string manipulation, and the process works for any subnet/supernet. Of course, using the prebuilt UDFs hides all of the "magic" - give it an address and a network and it says "yea" or "nay"! The latest version of InSubnet can be downloaded from the KixLib resources page of my web site.
The second UDF is a bit more complex, but much more powerful. IsInSubnet will accept an address and network / mask similar to InSubnet(). Where this UDF excels is that you can pass it an array of network/mask parameters, and the UDF will return an array indicating which network or networks the address is part of. Think about this - you have a large network at one site. It has 4 subnets. Your array of networks could consist of 5 specific items - the entire network at the location and the 4 subnets. Thus, any computer at that site would be a member of 2 networks - the large site network and one of the 4 subnets. This allows mapping of resources by site and then by subnet at the site.
Our Universal Login Script uses InSubnet technology to authorize resources by network location.
Glenn
_________________________
Actually I am a Rocket Scientist!
|
|
Top
|
|
|
|
#199784 - 2010-09-08 06:19 AM
Re: KIX script based on IP network subnet
[Re: airofu]
|
airofu
Just in Town
Registered: 2010-09-06
Posts: 3
Loc: Australia
|
Thanks sorry still abit new to this - what would be the full script/code to do the following (keeping it as basic as possible):
Define sites based on IP
site a = 192.168.1.0 = certain printers/mapped drives site b = 192.168.2.0 = certain printers/mapped drives
To detect client IP (also code to get rid of IP spaces) and map a drive to the relevant site above, thanks.
|
|
Top
|
|
|
|
#199787 - 2010-09-08 02:08 PM
Re: KIX script based on IP network subnet
[Re: Richard H.]
|
ChristopheM
Hey THIS is FUN
   
Registered: 2002-05-13
Posts: 311
Loc: STRASBOURG, France
|
have a look at some functions i recently wrote for the same reason :function ConvertIpAddrToArray( $ipstr )
dim $i, $num
select
case vartype($ipstr)=8 ;-- chaine de caractères --
dim $i
$ipstr = replace( $ipstr, " ", "" )
$ipstr = split( $ipstr, "." )
if ubound($ipstr)<>3 exit 1 endif
case UBound($ipstr)=3 ;-- tableau de 4 éléments --
;-- le controle sera fait dans le bloc commun --
case 1
exit 1
endselect
;-- controle des 4 entrées du tableau --
for $i = 0 to UBound($ipstr)
$num = val($ipstr[$i])
if val($num) <> $ipstr[$i] exit 1 endif
if ($num<0) or ($num>255) exit 1 endif
$ipstr[$i] = $num
next
$ConvertIpAddrToArray = $ipstr
exit 0
endfunction
function ConvertIPMaskToArray( $mask )
$ConvertIPMaskToArray = ConvertIpAddrToArray($mask)
if @error
;-- controleur : valeur doit être entre 1 et 32 (inclus) --
$mask = val($mask)
if ($mask<=0) or ($mask>32) exit 1 endif
redim $ConvertIPMaskToArray[3]
dim $s, $i, $j
$s = ""
for $i = 1 to $mask
$s = $s + "1"
next
while len($s)<32
$s = $s + "0"
loop
$j = 1
for $i = 0 to 3
$ConvertIPMaskToArray[$i] = BinToDec( substr($s,$j,8) )
$j = $j + 8
next
endif
exit 0
endfunction
function CompareIPAddr( $ip1, $ip2 )
$CompareIPAddr = (0=1)
$ip1 = ConvertIpAddrToArray( $ip1 )
if @error exit 1 endif
$ip2 = ConvertIpAddrToArray( $ip2 )
if @error exit 1 endif
dim $i
for $i = 0 to 3
if $ip1[$i]<>$ip2[$i] exit endif
next
$CompareIPAddr = (1=1)
endfunction
function IsIPAddrInSubnet( $ip, $subnet, $subnetmask )
$IsIPAddrInSubnet = (0=1)
$ip = ConvertIpAddrToArray( $ip )
if @error exit 1 endif
$subnet = ConvertIpAddrToArray( $subnet )
if @error exit 2 endif
$subnetmask = ConvertIpMaskToArray( $subnetmask )
if @error exit 3 endif
dim $i
$IsIPAddrInSubnet = (1=1)
for $i = 0 to 3
if ($ip[$i] & $subnetmask[$i]) <> $subnet[$i]
$IsIPAddrInSubnet = (0=1)
exit 4
endif
next
endfunction
function ApplyIPMaskToIPAddr( $IPMask, $IPAddr )
$ipaddr = ConvertIpAddrToArray( $ipaddr )
if @error exit 1 endif
$ipmask = ConvertIpMaskToArray( $ipmask )
if @error exit 2 endif
redim $ApplyIPMaskToIPAddr[3]
dim $i
for $i = 0 to 3
$ApplyIPMaskToIPAddr[$i] = $IPAddr[$i] & $IPMask[$i]
next
endfunction
function ConvertIPArrayToString( $ip )
$ConvertIPArrayToString = ""
$ip = ConvertIpAddrToArray( $ip )
if @error exit @error endif
$ConvertIPArrayToString = join( $ip, "." )
endfunction
function BinToDec( $str )
dim $num, $i, $j
$num = 0
$j = 1
for $i = len($str) to 1 step -1
$num = $num + $j*val(substr($str,$i,1))
$j = $j * 2
next
$BinToDec = $num
endfunction
- function ConvertIpAddrToArray( $ipstr ) - function ConvertIPMaskToArray( $mask ) - function CompareIPAddr( $ip1, $ip2 ) - function IsIPAddrInSubnet( $ip, $subnet, $subnetmask ) - function ApplyIPMaskToIPAddr( $IPMask, $IPAddr )
the most interesting for you is IsIPAddrInSubnet. You give the IP to test, the IP of the subnet and the mask of the subnet (in IP format or in number of bits) and the function returns a boolean.
example : - IsIPAddrInSubnet( "192.168.1.127", "192.168.1.0", "24" ) returns 1 - IsIPAddrInSubnet( "192.168.1.127", "192.168.1.0", "25" ) returns 1 - IsIPAddrInSubnet( "192.168.1.127", "192.168.1.0", "26" ) returns 0
My library is not complete because i could calculate some other informations : - first ip addr of a subnet - last ip addr of the subnet - broadcast IP address - binary mask, - reverse mask for switch but today i have no need of theses.
i should document the header of functions and translate "french" comments in the code. Now, i have not enough time !!!
_________________________
Christophe
|
|
Top
|
|
|
|
#199790 - 2010-09-09 03:05 AM
Re: KIX script based on IP network subnet
[Re: airofu]
|
airofu
Just in Town
Registered: 2010-09-06
Posts: 3
Loc: Australia
|
Thankyou that worked 
Other then mapping printers/network drive by AD groups and IP, what other handy functions are available for KIX? (I know there are many options but what I might actually find useful?) Thanks
|
|
Top
|
|
|
|
#199793 - 2010-09-09 02:31 PM
Re: KIX script based on IP network subnet
[Re: airofu]
|
Glenn Barnas
KiX Supporter
   
Registered: 2003-01-28
Posts: 4402
Loc: New Jersey
|
A great way to become comfortable with UDFs is to download the KixDev package from our web site and use KGen. KGen does two things for you - it examines your script and identifies references to external UDF files, including them automatically in a finished script. It then runs Sanity, which checks your script for common errors such as doubly-defined or undefined variables, mismatched quotes and parens, and unbalanced paired functions (like If/EndIf).
KixDev includes several sample UDFs, and our entire development library is published every night to our web site, so the latest versions are always available online. KGen works with any UDF file, and as was mentioned, the entire KORG UDF library is indexed at http://www.kixtart.org/udf.
With KixDev and KGen, you create a folder to hold your UDFs, and define that path in the KIXLIBPATH environment variable. Create your script as a .TXT file - myscript.txt - and reference any UDF from your library when you write your script. You don't have to worry about INCLUDing or CALLing the UDF file. You can also break larger projects into smaller, managable UDF files within a single project folder.
Run "KGEN myscript" and it will identify all of the available UDFs in the library and the local project folder, examine your script, and resolve all UDF dependencies and generate a complete script. In addition to generating a finished script, KGen will create several log files that aid in debugging or documenting your script project.
Comparing your .TXT file with the resulting .GEN file will illustrate how UDFs are used within kix. (The .GEN file is a clear-text file with all comments - KGen can strip comments or tokenize the final script.)
We use KGen for nearly every one of our Kix scripting projects, and some of our projects exceed 15,000 lines of code.
Glenn
_________________________
Actually I am a Rocket Scientist!
|
|
Top
|
|
|
|
Moderator: Jochen, Allen, Radimus, Glenn Barnas, ShaneEP, Ruud van Velsen, Arend_, Mart
|
0 registered
and 320 anonymous users online.
|
|
|