Break On
; Uses an INI file for subnet/printer mapping
; The INI file in this example is called NetPrint.ini, and uses one section
; called MAPPINGS. Each value consists of a comma-delimited pair of data
; representing the network/mask and printer share UNC in the format
; sitename=w.x.y.z/mask,\\server\printer
$Mapped = 0 ; value showing that printer was not mapped
$MyIP = Join(Split(@IPADDRESS0, ' '), '') ; Get the local IP address, removing spaces
; Load an array from the INI file
$Mappings = EnumIni('.\netprint.ini', 'MAPPINGS')
For Each $SiteName in $Mappings
; create a 2-element array by splitting the data at the comma
; element 0 contains the network/mask, and element 1 contains the printer UNC
$NetPrint = Split(ReadProfileString('.\netprint.ini', 'MAPPINGS', $SiteName), ',')
; see if the local IP is inside the network
If InSubnet($MyIP, $NetPrint[0])
; these print statements are for diagnostic/testing
'My ip address (' $MyIP ') exists in the ' $SiteName ' site (' $NetPrint[0] ')' ?
'Mapping printer ' $NetPrint[1] ?
; make the printer connection
AddPrinterConnection($NetPrint[1])
; show that at least 1 printer mapped successfully
If Not @ERROR
$Mapped = 1
EndIF
EndIF
Next
If Not $Mapped
'No printers were mapped!' ?
EndIf
; do not modify anything after this line!!!
;;
;;======================================================================
;;
;;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
; 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
;;
;;======================================================================
;;
;;FUNCTION EnumIni()
;;
;;ACTION Enumerates sections or keys of an INI file
;;
;;AUTHOR Glenn Barnas
;;
;;VERSION 2.0
;;
;;DATE CREATED 2003/11/17
;;
;;DATE MODIFIED 2004/10/16
;;
;;SYNTAX EnumIni(File [, Section])
;;
;;PARAMETERS File - REQUIRED, path/name of INI file to examine
;;
;; Section - OPTIONAL, Section name to parse
;;
;;REMARKS Returns an array containing the sections in an INI file, or
;; an array of key names in a specified section. Errors are returned
;; for non-existant files or INI file reads. If the specified file
;; contains no sections, or the specified section contains no keys,
;; the UDF exits with error 13 and returns a null array. Thus, a For-Each loop
;; will properly perform no iterations if no data is returned.
;;
;; CAUTION - Error 13 is returned for empty files, or nonexistant sections.
;; This is not necessarily a "failure".
;;
;;RETURNS Array of sections or keys in a section
;;
;;DEPENDENCIES none
;;
;;TESTED WITH Kix 4.2+, NT4, W2K, WXP, W2K3
;;
;;EXAMPLES $Sections = EnumIni('.\config.ini')
;; $Keys = EnumIni('.\config.ini', 'Common')
;
Function EnumIni($_fSrcFile, OPTIONAL $_fSectName)
Dim $_fSectList
; die if the file doesn't exist
If Exist($_fSrcFile) = 0
Exit 2
EndIf
; Get the list of sections or keys
$_fSectList = ReadProfileString($_fSrcFile, $_fSectName, '')
; Return if error occurred
If @ERROR
Exit @ERROR
EndIf
; If len is >0, return an array of sections
; If len is 0, either no sections or keys exist, or an invalid section name was specified. Return nothing.
If Len($_fSectList) > 0
$EnumIni = Split(Left($_fSectList,len($_fSectList)-1), Chr(10))
Exit 0
EndIF
; return an error here for value not found (no sections or no keys in section)
Exit 13
EndFunction