Page 1 of 1 1
Topic Options
#206632 - 2013-02-04 09:20 PM EnumIPInfo Randomly Stops Working
Destinova Offline
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:

 Code:
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:

 Code:
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
#206633 - 2013-02-04 09:46 PM Re: EnumIPInfo Randomly Stops Working [Re: Destinova]
Mart Moderator Offline
KiX Supporter
*****

Registered: 2002-03-27
Posts: 4673
Loc: The Netherlands
A small variation on your script.
Does this work better?

I added a Case 1. This case is always executed if no other case evaluates to true.

 Code:
For $NIC = 0 to 5
	;Split the IP into seperate octets.
	$ip = Split(EnumIPInfo($NIC, 0), ".")
	;Remove any leading spaces. Just in case there are any.
	For $i = 0 to UBound($ip)
		$ip[$i] = Trim($ip[$i])
	Next
	Select
		;Do stuff if 3rd octet is 32, 33, 34 or 35.
		Case $ip[2] = "32" Or $ip[2] = "33" Or $ip[2] = "34" Or $ip[2] = "35"
			$loc = "sch"
			Call "sch.kix"
		;Do stuff if 3rd octet is 40, 41, 42 or 43.
		Case $ip[2] = "40" Or $ip[2] = "41" Or $ip[2] = "42" Or $ip[2] = "43"
			$loc = "nam"
			Call "nam.kix"
		;Do stuff if 3rd octet is 44, 45, 46 or 47.
		Case $ip[2] = "44" Or $ip[2] = "45" Or $ip[2] = "46" Or $ip[2] = "47"
			$loc = "gut"
			Call "gut.kix"
		;Do stuff if no case is true.
		Case 1
			? "Unable to find matching third octet. Please contact IT."
			? "IP: " + EnumIPInfo($NIC, 0)
			? "Third octet: " + $ip[2]
	EndSelect
Next
:End
Exit
_________________________
Mart

- Chuck Norris once sold ebay to ebay on ebay.

Top
#206634 - 2013-02-04 10:34 PM Re: EnumIPInfo Randomly Stops Working [Re: Destinova]
Glenn Barnas Administrator Offline
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.
 Code:
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! \:D

Top
#206635 - 2013-02-04 11:04 PM Re: EnumIPInfo Randomly Stops Working [Re: Glenn Barnas]
Destinova Offline
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
Page 1 of 1 1


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

Who's Online
0 registered and 323 anonymous users online.
Newest Members
Audio, Hoschi, Comet, rrosell, PatrickPinto
17880 Registered Users

Generated in 0.115 seconds in which 0.09 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