Page 1 of 1 1
Topic Options
#199778 - 2010-09-06 06:48 AM KIX script based on IP network subnet
airofu Offline
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
#199779 - 2010-09-06 09:11 AM Re: KIX script based on IP network subnet [Re: airofu]
Mart Moderator Offline
KiX Supporter
*****

Registered: 2002-03-27
Posts: 4673
Loc: The Netherlands
You can check the IP address and do whatever is needed for that subnet. Be aware the every octet of the IP address always contains 3 characters. So if you if you have 10.10.1.1 it actually looks like " 10. 10. 1. 1" (without the quotes) when checking @IPADDRESS0. Every octet gets filled with spaces if it has less than 3 characters and the dots also count.
There are also several UDF that can do what you need.

A small example.
 Code:
If Left(@IPADDRESS0, 7) = "192.168"
	;Do stuff
EndIf
_________________________
Mart

- Chuck Norris once sold ebay to ebay on ebay.

Top
#199782 - 2010-09-06 02:31 PM Re: KIX script based on IP network subnet [Re: Mart]
Glenn Barnas Administrator Offline
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:
 Code:
$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! \:D

Top
#199784 - 2010-09-08 06:19 AM Re: KIX script based on IP network subnet [Re: airofu]
airofu Offline
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
#199786 - 2010-09-08 11:15 AM Re: KIX script based on IP network subnet [Re: airofu]
Richard H. Administrator Offline
Administrator
*****

Registered: 2000-01-24
Posts: 4946
Loc: Leatherhead, Surrey, UK
 Quote:
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.


Are all your networks /24 (255.255.255.0)?

The simplest code using built-ins that fulfils your request is something like this:
 Code:
$sMyIP=Replace(@IPADDRESS0," ","") ; Lose spaces from primary network adaptor address.
$sMySubnet=Join(Split($sMyIP,".",3),".") ; Drop trailing (host) address octet assuming /24 subnet

"   Raw IP address: '"+@IPADDRESS0+"'"+@CRLF
"Cooked IP address: '"+$sMyIP+"'"+@CRLF
"           Subnet: '"+$sMySubnet+"'"+@CRLF


Select
Case $sMySubnet="192.168.1"
	; Map drives and printers for ".1" subnet here
Case $sMySubnet="192.168.2"
	; Map drives and printers for ".2" subnet here
Case Default
	; Map drives and printers for everyone else here
EndSelect

Exit 0


This makes a number of assumptions about your environment, e.g. subnet masks are all /24 and @IPADDRESS0 is the adaptor that drives your subnet check.

Top
#199787 - 2010-09-08 02:08 PM Re: KIX script based on IP network subnet [Re: Richard H.]
ChristopheM Offline
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 :
 Code:
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 Offline
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
#199791 - 2010-09-09 09:08 AM Re: KIX script based on IP network subnet [Re: airofu]
Mart Moderator Offline
KiX Supporter
*****

Registered: 2002-03-27
Posts: 4673
Loc: The Netherlands
It all depends on what you are comfortable with.
I map printers based on AD groups but you can also use the IP, an INI file, usernames, departments, computer names, etc......
_________________________
Mart

- Chuck Norris once sold ebay to ebay on ebay.

Top
#199792 - 2010-09-09 09:38 AM Re: KIX script based on IP network subnet [Re: airofu]
Richard H. Administrator Offline
Administrator
*****

Registered: 2000-01-24
Posts: 4946
Loc: Leatherhead, Surrey, UK
 Originally Posted By: airofu
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 \:\)


KiXtart provides the framework, you provide the ideas ;\)

Probably the best idea if you are looking for inspiration is to browse the forums and see what other people are doing with it.

You can also look at the UDF forum where members have uploaded User Defined Functions which provide additional functionality not available with the built-ins. There is a simplified UDF listing available here: http://www.kixtart.org/udf/

Top
#199793 - 2010-09-09 02:31 PM Re: KIX script based on IP network subnet [Re: airofu]
Glenn Barnas Administrator Offline
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! \:D

Top
Page 1 of 1 1


Moderator:  Jochen, Allen, Radimus, Glenn Barnas, ShaneEP, Ruud van Velsen, Arend_, Mart 
Hop to:
Shout Box

Who's Online
0 registered and 320 anonymous users online.
Newest Members
ManuvdWielNL, Sir_Barrington, batdk82, StuTheCoder, M_Moore
17887 Registered Users

Generated in 0.109 seconds in which 0.072 seconds were spent on a total of 13 queries. Zlib compression enabled.

Search the board with:
superb Board Search
or try with google:
Google
Web kixtart.org