Page 1 of 1 1
Topic Options
#197497 - 2010-01-22 07:41 PM Running script only if User has a certan IP range
belezeebub Offline
Just in Town

Registered: 2010-01-22
Posts: 2
Loc: Saint Helens, OR.
Okay I am teaching myself Kix I have a pretty basic script (see below)

I want the script to run only of the users are in the 192.168.2.xxx Ip range and I would like to include a run command that will run only once a week to force a Program to un in this case a MSI update package any suggestions?

I can get the run command to work but it runs everytime I can't get the syntax on run once every 7 days to work




;=============================================
;Map Network Drives
;=============================================


If InGroup("AZ-HR")
Use H: "\\Pizza\HR"
EndIf

If InGroup("AZ-Users")
Use O: "\\Pizza\Officedocument"
EndIf

If InGroup("Domain Admins")
Use S: "\\Pizza\support"
EndIf

If InGroup("AZ-BSWDEPT")
Use U: "\\Pizza\Working"
EndIf

If InGroup("AZ-JobMaster")
Use V: "\\Pizza\Downloads"
EndIf

If InGroup("AZ-Developer")
Use W: "\\Pizza\AZ-BSWDept"
EndIf

If InGroup("AZ-Developer")
Use X: "\\Pizza\Applications"
EndIf

If InGroup("AZ-JobMaster")
Use Y: "\\Pizza\JobDocs"
EndIf

If InGroup("AZ-JobMaster")
Use Z: "\\Pizza\DataBase"
EndIf

Top
#197498 - 2010-01-22 07:53 PM Re: Running script only if User has a certan IP range [Re: belezeebub]
Glenn Barnas Administrator Offline
KiX Supporter
*****

Registered: 2003-01-28
Posts: 4307
Loc: New Jersey
Welcome to KORG!

The easiest way to check for subnet membership is to use one of the pre-written functions called UDFs (User Defined Functions). If you want to run this during the login script (not necessarily a good idea, IMO), then you will need to store a date of last run somewhere, such as the registry. Another UDF - TimeDiff - can calculate the difference between 2 dates. You'll need to search for and include the TimeDiff UDF for the example to work. Just paste it at the end of the script below.
 Code:
$MyIP = Join(Split(@IPADDRESS0, ' '), '')  ; "normalize" the IP address
If InSubnet($MyIP, '192.168.2.0/24')
  'is in the subnet!' ?
  ; time to do stuff?
  $LastDate = ReadValue('HKLM\Software\MyCompany', 'LastInstallCheckDate')
  If TimeDiff($LastDate, 'today', 'D') > 6
    ; it's been 7 days - run the install!

    ; update the registry
    $LastDate = WriteValue('HKLM\Software\MyCompany', 'LastInstallCheckDate', @DATE, 'REG_SZ')
  EndIf
EndIf

; InSubnet function follows - do not modify!
;;
;;======================================================================
;;
;;FUNCTION       InSubnet()
;;
;;ACTION         Determines if a specific IP address is in a subnet
;;
;;AUTHOR         Glenn Barnas
;;
;;VERSION        1.0 / 2008/01/20
;;
;;SYNTAX         InSubnet(Address, Network)
;;
;;PARAMETERS     Address - REQUIRED, IP address in dotted-decimal (w.x.y.z)
;;
;;		 Network - REQUIRED, Network in CIDR format (w.x.y.z/mask)
;;
;;REMARKS        
;;
;;RETURNS        1 if ip address is in the defined subnet, 0 otherwise
;;
;;DEPENDENCIES   none
;;
;;TESTED WITH    W2K, WXP, W2K3, Vista, X64
;;
;;EXAMPLES       If InSubnet('192.168.12.243', '192.168.8.0/21')
;;		   'Is in subnet!' ?
;;		 EndIf
;
Function InSubnet($_IPAddr, $_Network)

  Dim $_aAdd				; array of address values
  Dim $_Mask				; Mask value
  Dim $_Hosts				; hosts in subnet
  Dim $_I				; Index counter

  ; Convert the supplied IP address to a double value representing the decimal address
  $_aAdd = Split($_IPAddr, '.')
  $_IPAddr = (CDbl($_aAdd[0]) * 16777216.0) + (CDbl($_aAdd[1]) * 65536.0) + (CDbl($_aAdd[2]) * 256.0) + (CDbl($_aAdd[3]) * 1.0)

  ; Convert the network from a w.x.y.z/mask format to a decimal value
  ; this is the starting network address value
  $_Network = Split($_Network, '/')
  $_Mask = Val($_Network[1])
  $_aAdd = Split($_Network[0], '.')
  $_Network = (CDbl($_aAdd[0]) * 16777216.0) + (CDbl($_aAdd[1]) * 65536.0) + (CDbl($_aAdd[2]) * 256.0) + (CDbl($_aAdd[3]) * 1.0)

  ; Set the number of hosts in the defined network
  $_Hosts = 1.0
  For $_I = 31 to $_Mask Step -1
    $_Hosts = ($_Hosts * 2.0)
  Next

  ; return the value 
  $InSubnet = 0
  If $_IPAddr >= $_Network And $_IPAddr < ($_Network + $_Hosts)
    $InSubnet = 1
  EndIf

  Exit 0

EndFunction
Hope this gets you moving in the right direction.

Glenn



_________________________
Actually I am a Rocket Scientist! \:D

Top
#197500 - 2010-01-22 08:20 PM Re: Running script only if User has a certan IP range [Re: Glenn Barnas]
belezeebub Offline
Just in Town

Registered: 2010-01-22
Posts: 2
Loc: Saint Helens, OR.
Thank you that points me in the right place for one part but I think I am confused if I am reading yoour code correctly it would run the install only if they are in the correct IP range I don't want the script to run anything even map a drive unless they are in tghe correct range here is why we are a growing business in the last 6 mths we have went from workgroup to AD and citrix our main office is MN is a 192.168.1.xxx Office in Phoenix is 2.xxx office in Prescott is 3.xxx sierra vista 4.xxx etc... our people travel to all offices and if they are trying to map and print to MN resources from AZ it lags the WAN, so I want the users to run only the local scripts I figured I could do it if I could see some sample code for how to run only if you are in ip range XXX



Different offices also have different software packages to be installed.

(confused yet?)

All Phoenix People have forefront (no mom server yet yes I know but its hard to squeese money out of a small company mindset) phoenix gets the updates for forefront which I download and put on the server once a week.

HR maps local drives which I sync nightly


Let me try it like this my wife said I ramble when I type and talk

Logon

If Ipaddress is 192.168.1.xxx Then run MNlogon
If Ipaddress is 192.168.2.xxx then run AZlogon



Edited by belezeebub (2010-01-22 08:25 PM)

Top
#197501 - 2010-01-22 08:39 PM Re: Running script only if User has a certan IP range [Re: belezeebub]
Glenn Barnas Administrator Offline
KiX Supporter
*****

Registered: 2003-01-28
Posts: 4307
Loc: New Jersey
If you want to tackle this on your own, you can rearrange the logic to suit your needs. There's another UDF that Jens Meyer wrote that compares your IP address to a table of network addresses and will tell you WHICH network you are in.

Honestly, what you're looking to do can be fairly complex, especially for a new coder (or someone learning a new language). Go take a look at the manual for the login script on my web site. It handles mapping resources by network subnet with no coding. You can freely download the script and even get two free support incidents. I'm sure it will do exactly what you need, with about an hour or two of configuration and testing (after reading the manual).

I have it running at one client with 160 locations from a central AD server in the HQ location. 1 drive maps from HQ, other drives and printers map from the local server(s). Even with the login script loading from a DC in NJ and processing 40+ disk resources, it takes less than 15 seconds to complete. WAN was a 256K frame-relay connection when that was tested - they have since upgrated to 1 or 2Mbps MPLS, and the login script runs in under 10 seconds.

What's different is that it is ONE script that adapts based on the location. Adding new locations is easy, and you don't need to maintain multiple login scripts, or even multiple login config files.

Regards,

Glenn
_________________________
Actually I am a Rocket Scientist! \:D

Top
Page 1 of 1 1


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

Who's Online
0 registered and 160 anonymous users online.
Newest Members
GBMicros, Jodey, Hiker, GTRoberts, KatieQuinn
17685 Registered Users

Generated in 0.029 seconds in which 0.012 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