From owner-freebsd-questions@FreeBSD.ORG Thu Sep 25 07:37:00 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 8CBF01065686 for ; Thu, 25 Sep 2008 07:37:00 +0000 (UTC) (envelope-from jonathan+freebsd-questions@hst.org.za) Received: from hermes.hst.org.za (onix.hst.org.za [209.203.2.133]) by mx1.freebsd.org (Postfix) with ESMTP id 9ECB78FC14 for ; Thu, 25 Sep 2008 07:36:58 +0000 (UTC) (envelope-from jonathan+freebsd-questions@hst.org.za) Received: from sysadmin.hst.org.za (sysadmin.int.dbn.hst.org.za [10.1.1.20]) (authenticated bits=0) by hermes.hst.org.za (8.13.8/8.13.8) with ESMTP id m8P7TLTn059684 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-SHA bits=256 verify=NO) for ; Thu, 25 Sep 2008 09:29:21 +0200 (SAST) (envelope-from jonathan+freebsd-questions@hst.org.za) From: Jonathan McKeown Organization: Health Systems Trust To: freebsd-questions@freebsd.org Date: Thu, 25 Sep 2008 09:40:54 +0200 User-Agent: KMail/1.9.7 References: <3cc535c80809240333s1036386fnaa597267f0cf12aa@mail.gmail.com> <20080924151235.GA3284@dan.emsphone.com> In-Reply-To: <20080924151235.GA3284@dan.emsphone.com> X-Face: $@VrUx^RHy/}yu]jKf/<4T%/d|F+$j-Ol2"2J$q+%OK1]&/G_S9(=?utf-8?q?HkaQ*=60!=3FYOK=3FY!=27M=60C=0A=09aP=5C9nVPF8Q=7DCilHH8l=3B=7E!4?= =?utf-8?q?2HK6=273lg4J=7Daz?=@1Dqqh:J]M^"YPn*2IWrZON$1+G?oX3@ =?utf-8?q?k=230=0A=0954XDRg=3DYn=5FF-etwot4U=24b?=dTS{i X-Spam-Score: -4.378 () ALL_TRUSTED,AWL,BAYES_00 X-Scanned-By: MIMEDefang 2.61 on 209.203.2.133 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 07:37:00 -0000 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