Page 1 of 2 12>
Topic Options
#74048 - 2003-03-17 04:28 AM Mapping Printers by IP Subnet
Adam Crichton Offline
Fresh Scripter

Registered: 2003-03-17
Posts: 15
Loc: Canada
Pardon what may be a very bad newbie question. I've never written a script before but I need to map printers based on subnet. After reading the boards and the FAQ I came up with this:

code:
$ipaddress=@IPADDRESS0
$iparray[0]='192.168.2.0/24'
if isiniprange($ipaddress,$iparray)
$S=AddPrinterConnection ("\\diction\Woodstock Front Desk02")
$S=AddPrinterConnection ("\\woodstock02\Woodstock Label Printer")
$S=AddPrinterConnection ("\\diction\OHIP")
$S=SetDefaultPrinter ("\\diction\Woodstock Front Desk02")
ENDIF
$ipaddress=@IPADDRESS0
$iparray[0]='192.168.3.0/24'
if isiniprange($ipaddress,$iparray)
$S=AddPrinterConnection ("\\diction\Stratford Front Desk02")
$S=AddPrinterConnection ("\\stratford01\Stratford Label Printer")
$S=AddPrinterConnection ("\\diction\OHIP")
$S=SetDefaultPrinter ("\\diction\Stratford Front Desk02")
ENDIF
$ipaddress=@IPADDRESS0
$iparray[0]='192.168.4.0/24'
if isiniprange($ipaddress,$iparray)
$S=AddPrinterConnection ("\\goderichfrndsk\Goderich Front Desk01")
$S=AddPrinterConnection ("\\goderichfrndsk\Goderich Label Printer")
$S=AddPrinterConnection ("\\diction\OHIP")
$S=SetDefaultPrinter ("\\goderichfrndsk\Goderich Front Desk01")
ENDIF

Naturally this doesn't work (first try after all). Any ideas why? Would appreciate a pointer if anyone has time.

Thanks a lot.

[ 17. March 2003, 04:52: Message edited by: Adam Crichton ]

Top
#74049 - 2003-03-17 04:47 AM Re: Mapping Printers by IP Subnet
Howard Bullock Offline
KiX Supporter
*****

Registered: 2000-09-15
Posts: 5809
Loc: Harrisburg, PA USA
What part doesn't work? Some of that detail would be useful. You are missing an "endif" at the end of your published script.
_________________________
Home page: http://www.kixhelp.com/hb/

Top
#74050 - 2003-03-17 04:50 AM Re: Mapping Printers by IP Subnet
Adam Crichton Offline
Fresh Scripter

Registered: 2003-03-17
Posts: 15
Loc: Canada
Oh I'm sorry. The ENDIF is actually in the script, I made an error when I pasted it into the code.

The script does not map any printers from any of the IP ranges described. I apologize for being vague I thought I would be making a simple mistake someone would notice right away.

Thanks though.

Top
#74051 - 2003-03-17 04:53 AM Re: Mapping Printers by IP Subnet
Howard Bullock Offline
KiX Supporter
*****

Registered: 2000-09-15
Posts: 5809
Loc: Harrisburg, PA USA
Does your script include the code for the IsInIPrange() UDF?

Have you placed a print statement in each IF..EndIf to see if the IF condition worked? If so, then what are the return codes for the ADDPrinterConnection().

What OS is the client?

[ 17. March 2003, 04:54: Message edited by: Howard Bullock ]
_________________________
Home page: http://www.kixhelp.com/hb/

Top
#74052 - 2003-03-17 04:59 AM Re: Mapping Printers by IP Subnet
Adam Crichton Offline
Fresh Scripter

Registered: 2003-03-17
Posts: 15
Loc: Canada
Ah ha. My script does not include the code for the IsInIPrange() UDF? What should that be? I'm sure that's what I'm missing.

The clients are all XP (I notice the addprinterconnection doesn't work in 9x).

The error is:
Script error: expected expression!
if isiniprage($ipaddress,$iparray)

Thanks for your help.

Top
#74053 - 2003-03-17 05:02 AM Re: Mapping Printers by IP Subnet
Howard Bullock Offline
KiX Supporter
*****

Registered: 2000-09-15
Posts: 5809
Loc: Harrisburg, PA USA
Go get the code for "isiniprage". You can find it by looking in the UDF Library or better yet follow the UDF Collection link in my signature line.
_________________________
Home page: http://www.kixhelp.com/hb/

Top
#74054 - 2003-03-17 05:09 AM Re: Mapping Printers by IP Subnet
Adam Crichton Offline
Fresh Scripter

Registered: 2003-03-17
Posts: 15
Loc: Canada
OK - progress - pasted that into my script - different error now:

Script Error: error in expression!
$iparray[0]='192.168.3.0/24'

The FAQ seemed to indicate I could use the bit subnet identifier. Could that be the problem? Or am I missing something still.

Really appreciate the help.

Top
#74055 - 2003-03-17 05:12 AM Re: Mapping Printers by IP Subnet
Howard Bullock Offline
KiX Supporter
*****

Registered: 2000-09-15
Posts: 5809
Loc: Harrisburg, PA USA
Try adding "Dim $iparray[0]" at the top of your script.

Also you do not need to get the IP address multiple times.

[ 17. March 2003, 05:13: Message edited by: Howard Bullock ]
_________________________
Home page: http://www.kixhelp.com/hb/

Top
#74056 - 2003-03-17 05:18 AM Re: Mapping Printers by IP Subnet
Adam Crichton Offline
Fresh Scripter

Registered: 2003-03-17
Posts: 15
Loc: Canada
More progress (again thanks for the help), still no luck, new error:

Script Error: expected expression!
if isinsubnet($ipaddresss,$networkid,$subnetmask)

So close [Confused]

Top
#74057 - 2003-03-17 05:20 AM Re: Mapping Printers by IP Subnet
Howard Bullock Offline
KiX Supporter
*****

Registered: 2000-09-15
Posts: 5809
Loc: Harrisburg, PA USA
Now that you have had some fun trouble shooting...

This is how I would format it:

Dim $iparray[2]
$ipaddress=@IPADDRESS0
$iparray[0]='192.168.2.0/24'
$iparray[1]='192.168.3.0/24'
$iparray[2]='192.168.4.0/24'

call "isiniprange.udf"

Select
   case isiniprange($ipaddress,$iparray[0])
      $S=AddPrinterConnection ("\\diction\Woodstock Front Desk02")
      $S=AddPrinterConnection ("\\woodstock02\Woodstock Label Printer")
      $S=AddPrinterConnection ("\\diction\OHIP")
      $S=SetDefaultPrinter ("\\diction\Woodstock Front Desk02")

   case isiniprange($ipaddress,$iparray[1])
      $S=AddPrinterConnection ("\\diction\Stratford Front Desk02")
      $S=AddPrinterConnection ("\\stratford01\Stratford Label Printer")
      $S=AddPrinterConnection ("\\diction\OHIP")
      $S=SetDefaultPrinter ("\\diction\Stratford Front Desk02")

   case isiniprange($ipaddress,$iparray[2])
      $S=AddPrinterConnection ("\\goderichfrndsk\Goderich Front Desk01")
      $S=AddPrinterConnection ("\\goderichfrndsk\Goderich Label Printer")
      $S=AddPrinterConnection ("\\diction\OHIP")
      $S=SetDefaultPrinter ("\\goderichfrndsk\Goderich Front Desk01")
EndSelect



{edit} used PostPrep so that one could copy/paste directly to your text editor.

Also check the dependencies of IsInIPrange().

[ 17. March 2003, 05:49: Message edited by: Howard Bullock ]
_________________________
Home page: http://www.kixhelp.com/hb/

Top
#74058 - 2003-03-17 05:22 AM Re: Mapping Printers by IP Subnet
Howard Bullock Offline
KiX Supporter
*****

Registered: 2000-09-15
Posts: 5809
Loc: Harrisburg, PA USA
When and why did you change from "isiniprange" to "isinsubnet"? Did you place that code in your program?
_________________________
Home page: http://www.kixhelp.com/hb/

Top
#74059 - 2003-03-17 05:25 AM Re: Mapping Printers by IP Subnet
Howard Bullock Offline
KiX Supporter
*****

Registered: 2000-09-15
Posts: 5809
Loc: Harrisburg, PA USA
Did you read the dependencies of IsInIPrange()? It requires additional code.

code:
;DEPENDENCIES  BINARYIP() 
; ISINSUBNET()



[ 17. March 2003, 05:32: Message edited by: Howard Bullock ]
_________________________
Home page: http://www.kixhelp.com/hb/

Top
#74060 - 2003-03-17 05:29 AM Re: Mapping Printers by IP Subnet
Howard Bullock Offline
KiX Supporter
*****

Registered: 2000-09-15
Posts: 5809
Loc: Harrisburg, PA USA
You could also look into:

$LogicalSubnet = CalcLogicalSubnet (EnumIpInfo(0), EnumIpInfo(1))

[ 17. March 2003, 05:30: Message edited by: Howard Bullock ]
_________________________
Home page: http://www.kixhelp.com/hb/

Top
#74061 - 2003-03-17 05:36 AM Re: Mapping Printers by IP Subnet
Adam Crichton Offline
Fresh Scripter

Registered: 2003-03-17
Posts: 15
Loc: Canada
OK first I like your method better for selecting the IP's it's much cleaner (my way is what happens when people with Arts degrees try to write code). And I didn't see the dependencies so those are now in.

So now we have what I think might be our last error:

Script Error: array reference out of bounds!
$iparray[1]='192.168.3.0/24'

Top
#74062 - 2003-03-17 05:40 AM Re: Mapping Printers by IP Subnet
Howard Bullock Offline
KiX Supporter
*****

Registered: 2000-09-15
Posts: 5809
Loc: Harrisburg, PA USA
Do you have the "DIM $iparray[2]" statement in your code?

Did you copy the code from the post, paste it into WORD.exe, and then copy/paste it to you text editor?

[ 17. March 2003, 05:40: Message edited by: Howard Bullock ]
_________________________
Home page: http://www.kixhelp.com/hb/

Top
#74063 - 2003-03-17 05:44 AM Re: Mapping Printers by IP Subnet
Adam Crichton Offline
Fresh Scripter

Registered: 2003-03-17
Posts: 15
Loc: Canada
Yes, I noticed that I needed to add that DIM statment (actually added all three), so that's fine now, and I also worked out about pasting into word first so it formats correctly before I paste it into the text editor.

Anyway, still the same error message. Sigh.

Thanks.

Top
#74064 - 2003-03-17 05:46 AM Re: Mapping Printers by IP Subnet
Howard Bullock Offline
KiX Supporter
*****

Registered: 2000-09-15
Posts: 5809
Loc: Harrisburg, PA USA
Please post your code. What do you mean
quote:
I noticed that I needed to add that DIM statment (actually added all three)
_________________________
Home page: http://www.kixhelp.com/hb/

Top
#74065 - 2003-03-17 05:50 AM Re: Mapping Printers by IP Subnet
Howard Bullock Offline
KiX Supporter
*****

Registered: 2000-09-15
Posts: 5809
Loc: Harrisburg, PA USA
Go back and recopy the code. I missed a QUOTE. You can now copy it directly into your text editor.
_________________________
Home page: http://www.kixhelp.com/hb/

Top
#74066 - 2003-03-17 05:51 AM Re: Mapping Printers by IP Subnet
Adam Crichton Offline
Fresh Scripter

Registered: 2003-03-17
Posts: 15
Loc: Canada
OK here's where it's at now; the paste is botching it a little bit (in the printer add area) and I'm sorry but I really have been working on this all day and it's driving me crazy. I included the UDF's to make it easier.

Thanks so much for your help:

code:
function ipmask($ipaddress1, $ipaddress2)
Dim $bit if instr($ipaddress1,'.') or instr($ipaddress2,'.') or len($ipaddress1)<>32 or len($ipaddress2)<>32
exit 87
endif $ipmask=''
for $bit=1 to 32
$ipmask=$ipmask+(val(substr($ipaddress1,$bit,1)) & val(substr($ipaddress2,$bit,1)))
next
endfunction


function BinaryIP($IP)
DIM $item, $bitsize, $octet $bitsize = 128
$ip = split($IP,".") select
case UBOUND($ip) <> 3
$BinaryIP = 0
exit(87)
case 1
for each $octet in $ip
if VAL($octet) <0 OR VAL($octet) > 255
$BinaryIP = 0
exit(87)
endif
while $bitsize > 0
if val($octet) & $bitsize
$Binary = $Binary + "1"
else
$Binary = $Binary + "0"
endif
$bitsize = $bitsize / 2
loop
$binaryIP = $binaryIP + "$binary"
$bitsize = 128
$binary = ""
next
endselect
exit(0)
endfunction

function isinsubnet($ipaddress,$networkid,$subnetmask)
Dim $maskedip, $bit

if instr($ipaddress,'.')
$ipaddress=binaryip($ipaddress)
endif
if instr($networkid,'.')
$networkid=binaryip($networkid)
endif
if instr($subnetmask,'.')
$subnetmask=binaryip($subnetmask)
endif

$maskedip=ipmask($ipaddress,$subnetmask)
if $maskedip=$networkid
$isinsubnet = 1
else
$isinsubnet = 0
endif
endfunction

function isiniprange($ipaddress,$iprangearray)
Dim $iprange, $networkid, $subnetmask, $bit

if $ipaddress=''
exit 87
endif

$ipaddress=binaryip($ipaddress)

for each $iprange in $iprangearray

if instr($iprange,'/')
$iprange=split($iprange,'/')
$networkid=binaryip($iprange[0])

if instr($iprange[1],'.')
$subnetmask=binaryip($iprange[1])
else
$subnetmask=''
for $bit=1 to val($iprange[1])
$subnetmask=$subnetmask+'1'
next
for $bit=(val($iprange[1])+1) to 32
$subnetmask=$subnetmask+'0'
next
endif

if isinsubnet($ipaddress,$networkid,$subnetmask)
$isiniprange=1
return
else
$isiniprange=0
endif
endif
next

endfunction

DIM $iparray[0]
DIM $iparray[1]
DIM $iparray[2]
$ipaddress=@IPADDRESS0
$iparray[0]='192.168.2.0/24'
$iparray[1]='192.168.3.0/24'
$iparray[2]='192.168.4.0/24
call "isiniprange.udf"
Select
case isiniprange($ipaddress,$iparray[0])
$S=AddPrinterConnection ("\\diction\Woodstock Front Desk02") $S=AddPrinterConnection ("\\woodstock02\Woodstock Label Printer") $S=AddPrinterConnection ("\\diction\OHIP")
$S=SetDefaultPrinter ("\\diction\Woodstock Front Desk02")

case isiniprange($ipaddress,$iparray[1])
$S=AddPrinterConnection ("\\diction\Stratford Front Desk02")
$S=AddPrinterConnection ("\\stratford01\Stratford Label Printer") $S=AddPrinterConnection ("\\diction\OHIP")
$S=SetDefaultPrinter ("\\diction\Stratford Front Desk02")

case isiniprange($ipaddress,$iparray[2])
$S=AddPrinterConnection ("\\goderichfrndsk\Goderich Front Desk01") $S=AddPrinterConnection ("\\goderichfrndsk\Goderich Label Printer") $S=AddPrinterConnection ("\\diction\OHIP")
$S=SetDefaultPrinter ("\\goderichfrndsk\Goderich Front Desk01")
EndSelect
:END


Top
#74067 - 2003-03-17 05:54 AM Re: Mapping Printers by IP Subnet
Howard Bullock Offline
KiX Supporter
*****

Registered: 2000-09-15
Posts: 5809
Loc: Harrisburg, PA USA
Remove the following lines:

DIM $iparray[0]
DIM $iparray[1]
call "isiniprange.udf"

Add single-quote to end of:
$iparray[2]='192.168.4.0/24
_________________________
Home page: http://www.kixhelp.com/hb/

Top
Page 1 of 2 12>


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

Who's Online
0 registered and 310 anonymous users online.
Newest Members
Praveer, Ollero, rayyo, Foxlicht, DonChild
17742 Registered Users

Generated in 0.043 seconds in which 0.011 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