From owner-freebsd-questions@FreeBSD.ORG Thu Sep 25 15:39:28 2008 Return-Path: Delivered-To: freebsd-questions@freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2001:4f8:fff6::34]) by hub.freebsd.org (Postfix) with ESMTP id 1B56D1065691 for ; Thu, 25 Sep 2008 15:39:28 +0000 (UTC) (envelope-from tedm@toybox.placo.com) Received: from mail.freebsd-corp-net-guide.com (mail.freebsd-corp-net-guide.com [65.75.192.90]) by mx1.freebsd.org (Postfix) with ESMTP id C83788FC16 for ; Thu, 25 Sep 2008 15:39:27 +0000 (UTC) (envelope-from tedm@toybox.placo.com) Received: from TEDSDSK (nat-rtr.freebsd-corp-net-guide.com [65.75.197.130]) by mail.freebsd-corp-net-guide.com (8.13.8/8.13.8) with SMTP id m8PFdHLE048548; Thu, 25 Sep 2008 08:39:25 -0700 (PDT) (envelope-from tedm@toybox.placo.com) From: "Ted Mittelstaedt" To: "Jonathan McKeown" , Date: Thu, 25 Sep 2008 08:40:26 -0700 Message-ID: MIME-Version: 1.0 Content-Type: text/plain; charset="iso-8859-1" Content-Transfer-Encoding: 7bit X-Priority: 3 (Normal) X-MSMail-Priority: Normal X-Mailer: Microsoft Outlook IMO, Build 9.0.6604 (9.0.2911.0) In-Reply-To: <200809250940.54775.jonathan+freebsd-questions@hst.org.za> X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2800.1933 Importance: Normal X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-3.0 (mail.freebsd-corp-net-guide.com [65.75.192.90]); Thu, 25 Sep 2008 08:39:26 -0700 (PDT) Cc: Subject: RE: Netprint perl script from Handbook doesn't work X-BeenThere: freebsd-questions@freebsd.org X-Mailman-Version: 2.1.5 Precedence: list List-Id: User questions List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Thu, 25 Sep 2008 15:39:28 -0000 > -----Original Message----- > From: owner-freebsd-questions@freebsd.org > [mailto:owner-freebsd-questions@freebsd.org]On Behalf Of Jonathan > McKeown > Sent: Thursday, September 25, 2008 12:41 AM > To: freebsd-questions@freebsd.org > Subject: Re: Netprint perl script from Handbook doesn't work > > > On Wednesday 24 September 2008 17:12:36 Dan Nelson wrote: > > In the last episode (Sep 24), Andy Kosela said: > > > The netprint perl script provided in the Handbook (9.4.3.2) is not > > > working.. or am I missing something: > > > > > > plotinus:~> cat new.txt | lp.sh > > > Can't contact 10.10.21.12: Address family not supported by protocol > > > family at /usr/local/libexec/netprint line 21. > > > > Can you telnet to that ip address ("telnet 10.10.21.12 9100", or > > whatever port you're using)? > > > > > plotinus:> cat /usr/local/libexec/netprint > > > #!/usr/bin/perl > > > # > > > # netprint - Text filter for printer attached to network > > > # Installed in /usr/local/libexec/netprint > > > # > > > $#ARGV eq 1 || die "Usage: $0 "; > > > > > > $printer_host = $ARGV[0]; > > > $printer_port = $ARGV[1]; > > > > > > require 'sys/socket.ph'; > > > > > > ($ignore, $ignore, $protocol) = getprotobyname('tcp'); > > > ($ignore, $ignore, $ignore, $ignore, $address) > > > = gethostbyname($printer_host); > > > > > > $sockaddr = pack('S n a4 x8', &AF_INET, $printer_port, $address); > > > > > > socket(PRINTER, &PF_INET, &SOCK_STREAM, $protocol) > > > > > > || die "Can't create TCP/IP stream socket: $!"; > > > > > > connect(PRINTER, $sockaddr) || die "Can't contact $printer_host: $!"; > > > while () { print PRINTER; } > > > exit 0; > > > > Wow. That's a really complicated way to say > > > > #! /bin/sh > > nc $1 $2 > > It's also ugly (and very old-fashioned) Perl. Starting at (and > replacing) the > require 'sys/socket.ph' line (which is Perl 4, I think), it > should look more > like this (with appropriate error-checking added): > > use Socket; > my $proto = getprotobyname('tcp'); > socket(my $socket, PF_INET, SOCK_STREAM, $proto); > my $sock_in = sockaddr_in($printer_port, inet_aton($printer_host)); > connect($socket, $sock_in); > > Although this rewrite removes the need, if you want in general to > ignore some > of the return values of a function returning a list, the usual way is to > assign to undef: > > (undef, undef, undef, undef, $address) = gethostbyname($printer_host); > > Although when you're throwing away that many, it makes more sense > to index the > returned list in the same way you would index an array: > > $address = (gethostbyname($printer_host))[4] # returns 5th element > > I really should submit a doc patch for this (incorporating Dan's sterling > suggestion of nc $1 $2). > Jonathan, Submit a patch but rewrite the script as well as include use of the nc utility. It is important that when possible the handbook contain solutions that are portable to other UNIX variants. Everything in the handbook is indexed in search engines and we want people looking for solutions to be able to use the Handbook, this can help them get interested in FreeBSD. Ted