Printing from Linux to a Windows 95/NT shared printer.

or "one less reason to have to use windoze on your desktop"

My setup :
Slackware Distribution 3.2
Kernel 2.0.33
Samba 1.9.17p4
LPRng 3.2.3

I have setup my system so that it can print in both text and postscript mode to an HP LaserJet 4.

Step 1. Edit your /etc/printcap file

My printcap file contains the following two printer definitions

#start of printcap

hp4:\
        :lp=/dev/hp4:\
        :sd=/var/spool/lpd/hp:\
        :af=/var/spool/lpd/hp/acct:\
        :mx#0:\
  
     :if=/usr/local/print/texthp4:

ps4:\
        :lp=/dev/ps4:\
        :sd=/var/spool/lpd/ps:\
        :af=/var/spool/lpd/ps/acct:\
     
  :mx#0:\
        :if=/usr/local/print/pshp4:

#end of printcap


Step 2. Create your devices

To prevent possible 'exclusive' open conflicts I have specified different devices in the :lp statement. Create these devices now.

	bash# touch /dev/hp4
	bash# touch /dev/ps4
	bash# chmod 777 /dev/hp4
	bash# chmod 777 /dev/ps4


Step 3. Prepare LPD

A common cause of printer problems is not to do with the setup of the files, but the setup of the directories used to spool and control the print devices.

bash# mkdir /var/spool/lpd/hp
bash# mkdir /var/spool/lpd/ps
bash# checkpc -f


After running checkpc, create anything that it has trouble finding, usually it is log and lock files.
When setting up a new system I always forget to create the /var/spool/lpd/lpd.lock.??? file.

Step 4. Create those print scripts

The printcap file specified an input filter for each device setup. These can be made quite complex (as in the example I used to get going) but I have created *very* simple ones that just do the print filtering. I have also created a /usr/local/print directory to keep them in, so put them where you like, just make sure that printcap specifies their location.

The plain text filter :

#!/bin/sh -x
#
# /usr/local/print/texthp4
#
# Plain text printing to an HP laserjet 4
#
logfile=/tmp/smb-print.log
(
        echo translate
        echo "print -"
        cat
        echo -e "\f"
) | /usr/local/samba/bin/smbclient "\\\\OBSLON2\\mktghp4" password -U username -N -P >>$logfile
#end of texthp4


The postscript filter :

#!/bin/sh -x
#
# /usr/local/print/pshp4
#
# Postscript printing to an HP Laserjet 4
#
logfile=/tmp/smb-print.log
(
        echo "print -"
        /usr/bin/gs -dSAFER -dNOPAUSE -sPAPERSIZE=a4 -q -sDEVICE=ljet4 -sOutputFile=- -
        printf "\014"
) | /usr/local/samba/bin/smbclient "\\\\OBSLON2\\mktghp4" password -U username -N -P >>$logfile
#end of pshp4


Note: these filters expect to find the samba binary smbclient in the /usr/local/samba/bin directory. If you have installed samba elsewhere, change the smbclient line accordingly.
In these examples I am communicating with an NT server called OBSLON2 which has a print service mktghp4 . (the many many '\' characters are required).

Step 5. TCP/IP bits and bobs

Naturally you require TCP/IP running on your system, and you need to inform samba of the IP address of the NT server. You should also ensure that the router can route stuff to the printer.

Ensure that you have an entry for the NT server in your /etc/hosts file. Mine looks like this :

#
# hosts         This file describes a number of hostname-to-address
#               mappings for the TCP/IP subsystem.  It is mostly
#               used at boot time, when no name servers are running.
#               On small systems, this file can be used instead of a
#               "named" name server.  Just add the names, addresses
#      
        and any aliases to this file...
#
# By the way, Arnt Gulbrandsen <agulbra@nvg.unit.no> says that 127.0.0.1
# should NEVER be named with the name of the machine.  It causes problems
# for some (stupid) programs, irc and reputedly talk. :^)
#

#
For loopbacking.
127.0.0.1       localhost
1.0.12.17       lynx.obs lynx (my linux box)
1.0.21.125      OBSLON2
# End of hosts.


Once this has been done, ensure that you have a route to the NT server by adding a route command to your /etc/rc.d/rc.inet1 file.

/sbin/route add -net 1.0.21.0 netmask 255.255.255.0 eth0


Test the connection to the NT server using the nmblookup command :

bash#  /usr/local/samba/bin/nmblookup -B 1.0.21.125 OBSLON2


Which should return
1.0.21.125 OBSLON2

if successful.

Step 6. Start LPD

Start or restart LPD. If LPD is running then kill it and restart it (yes, okay, so you can probably also use lpc to do it nicely).

bash# lpd

If it is already running then,

bash# lpd
lpd: Fatal error - Another print spooler is using TCP printer port, possibly lpd process '4788'
bash# kill -9 4788
bash# lpd


Step 7. Give it a go

To test out the plain text print :

bash# lpr -Php4 /usr/local/print/texthp4

And wait to see what (if anything) appears

To test out the postscript mode, well use something that generates postscript (netscape for example, or ez-andrew) and print to queue ps4.

If you do not have some output generated, well, either your printer is offline, your printer daemon has a problem, or I was *very* lucky to get mine working in the first place.


Mark W J Redding, 2nd March 1998

Return to the main Linux page

本文转自中文Linux论坛

Return to the Index page