#206632 - 2013-02-04 09:20 PM
EnumIPInfo Randomly Stops Working
|
Destinova
Just in Town
Registered: 2008-05-15
Posts: 4
|
We have a fairly basic script in place that checks the IP address of the computer to determine it's location and then calls a sub-script based on that location to map drives and printers.
It's been working for about 4-5 years, and now suddenly (at random) stops working for some machines. We have a mix of Windows XP and Windows 7 machines.
I am absolutely stumped as to why, and it is seemingly random. I have a test machine that runs both XP and 7 and the scripts run fine on it, yet daily I am getting a call about drives not mapping and testing those machines the script does not run.
Since most machines have multiple NICs it goes through them all looking for a valid IP:
For $NIC = 0 to 5
Select
Case EnumIPInfo ($NIC,0) >= "172.16.32.0" And EnumIPInfo ($NIC,0) <= "172.16.35.255"
$loc = "sch"
Call "sch.kix"
Case EnumIPInfo ($NIC,0) >= "172.16.40.0" And EnumIPInfo ($NIC,0) <= "172.16.43.255"
$loc = "nam"
Call "nam.kix"
Case EnumIPInfo ($NIC,0) >= "172.16.44.0" And EnumIPInfo ($NIC,0) <= "172.16.47.255"
$loc = "gut"
Call "gut.kix"
EndSelect
Next
:End
Exit
Going through this, no Case is ever selected and the script completes normally but never calls a sub-script.
I added a quick snippet to display the enumeration of the NIC addresses at the beginning:
For $addr = 0 to 5
$card=EnumIPInfo($addr,0)
$addr
$card
?"Press any key..." GET$
Next
This works and displays all of the IP addresses for the computer, yet again the sub-script is never called.
Any help is appreciated.
Edited by Destinova (2013-02-04 09:23 PM)
|
Top
|
|
|
|
#206634 - 2013-02-04 10:34 PM
Re: EnumIPInfo Randomly Stops Working
[Re: Destinova]
|
Glenn Barnas
KiX Supporter
   
Registered: 2003-01-28
Posts: 4400
Loc: New Jersey
|
The comparisons you are making are based on strings, which is less than ideal for IP Address comparisons. There are several UDFs that will convert the IP address and subnet start/end values to a number to perform a direct numeric comparison.
The code below uses one such UDF, and I confirm that it works using my subnet ranges. This is also a simpler configuration as you just use "If InSubnet", passing the local IP address and the network address/mask value. Also, the updated code below only performs the test (Select/Case) if the IPAddr variable contains data. Further, using the IPAddr variable achieves two goals - the EnumIpInfo function is only run once per loop instead of 6 times, and you can then display the variable being evaluated in debugging.For $NIC = 0 to 5
$IPAddr = EnumIPInfo($NIC,0, 1)
If $IPAddr
'Checking ' $IPAddr ?
Select
Case InSubnet($IPAddr, '172.16.32.0/22')
$loc = "sch"
;Call "sch.kix"
Case InSubnet($IPAddr, '172.16.40.0/22')
$loc = "nam"
;Call "nam.kix"
Case InSubnet($IPAddr, '172.16.44.0/22')
$loc = "gut"
;Call "gut.kix"
Case InSubnet($IPAddr, '172.16.12.0/22')
$loc = "ITCG"
EndSelect
EndIf
Next
'loc: ' $Loc ?
Exit
;;
;;======================================================================
;;
;;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, '.')
If UBound($_aAdd) <> 3
Exit 87 ; invalid data passed
EndIf
$_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 Glenn
_________________________
Actually I am a Rocket Scientist!
|
Top
|
|
|
|
#206635 - 2013-02-04 11:04 PM
Re: EnumIPInfo Randomly Stops Working
[Re: Glenn Barnas]
|
Destinova
Just in Town
Registered: 2008-05-15
Posts: 4
|
@Mart - we do have a Case 1 at the end I had neglected to add it in the example code.
@Glenn - I'll give that a try and see if it works any better.
Thanks!
|
Top
|
|
|
|
Moderator: Glenn Barnas, NTDOC, Arend_, Jochen, Radimus, Allen, ShaneEP, Ruud van Velsen, Mart
|
0 registered
and 323 anonymous users online.
|
|
|