APLawrence.com -  Resources for Unix and Linux Systems, Bloggers and the self-employed

We no longer offer ftp downloads. If there is a file you need referenced here, please contact me by email and I will get it to you.

Setting up Netcat Printers

Email: [email protected]

See Where do I get netcat and how do I use it?

Boy, I like netcat. Just finished converting one client's SCO 5.0.5 system from Berkeley LPD to netcat and recommend the change to anyone now using LPD to print to dedicated print servers.

I cobbled the following script to parse the /etc/printers file and automatically setup one or all printers. I also include my version of HPLaserJet.net supporting duplex enable printers, 66 line portrait and 66 line landscape as well as 88 line 16.6cpi portrait.


>/usr/local/bin/printeradmin: option=$1 tprinter=$2 [ -z "$option"
] || opt="opt=$1" [ -z "$tprinter" ] || printer="ptr="$2 awk  '
BEGIN { FS = ":" } { #       print ARGC, ARGV[0], ARGV[1], ARGV[2]
, ARGV[3], ARGV[4]

        if ( ARGC < 3 ) {
                printf "\n\n Usage: printeradmin add    <-
                create all printers in /etc/printers\n
                printeradmin delete <- delete all printers
                in /etc/printers\n\n" printf "\n\n Usage:
                printeradmin add    name <- create named printer
                in /etc/printers\n        printeradmin delete name
                <- delete named printer in /etc/printers\n\n"
        exit } if ( ARGC == 3 ) { if ( NF == 4 ) {
                if ( opt == "add" ) {
                        printf "/usr/lib/lpadmin -p %s -v /dev/null
                        -m HPLaserJet.net -D \042%s\042\n", $1, $4
                        printf "/usr/lib/accept  %s\n", $1 printf
                        "enable  %s\n", $1
                } if ( opt == "delete" ) {
                        printf "/usr/lib/lpadmin -x %s \n", $1
                }
        } } if ( ARGC == 4 ) { if ( NF == 4 ) {
                if ( opt == "add" && ptr == $1 ) {
                        printf "/usr/lib/lpadmin -p %s -v /dev/null
                        -m HPLaserJet.net -D \042%s\042\n", $1, $4
                        printf "/usr/lib/accept  %s\n", $1 printf
                        "enable  %s\n", $1
                } if ( opt == "delete"  && ptr == $1 ) {
                        printf "/usr/lib/lpadmin -x %s \n", $1
                }
        } }
} ' $opt $printer /etc/printers

printeradmin writes its output to standardout and does not execuite the commands. I just run printeradmin > /tmp/doit and then "sh /tmp/doit" after I page /tmp/doit to see that it's correct.

Once I am comfortable with the output of printeradmin, I just run printeradmin options | sh to skip the step of outputting to a temporary file and then using sh to execute the file.


>HPLaserJet.net:
:
#
printer=`basename $0`
request=$1
name=$2
title=$3
copies=$4
options=$5
shift; shift; shift; shift; shift
spool=/usr/spool/lp
FILTER="xtod | ${spool}/bin/ffstrip"

# Look for HOST and Port address
PRTSETUP=`grep "^$printer:" /etc/printers`
if [ $? = 0 ]
then
PRTHOST=`echo $PRTSETUP|awk -F: '{ print $2 }'`
PRTPORT=`echo $PRTSETUP|awk -F: '{ print $3 }'`
else
 exit 1
fi

# border around the banner
x="xyzxyzxyzxyzxyzxyzxyzxyzxyzxyzxyzxyzxyzxyzxyzxyzxyzxyzxyzxyzxyzxyzxyzxyzxyz"

Graphics=no
Landscape=no
Wide=no
banner=no  # I don't use banners, have not tested banner=yes
sixlines=no
tencpi=no
Duplex=no
for i in $options
do
        case $i in
        raw|g|G|pro)    Graphics=yes
                        ;;
                l|L)    Landscape=yes
                        ;;
                d|D)    Duplex=yes
                        ;;
                w|W)   Wide=yes
                        ;;
         nobanner|b)    banner=no
                        ;;
                6lpi)   sixlines=yes
                        ;;
                10cpi)  tencpi=yes
                        ;;
        esac
done
#
# Set up FILTER environment variable.
#
#
: ${SPOOLDIR:=/usr/spool/lp}
: ${LOCALPATH:=${SPOOLDIR}/bin}
if [ ! -x "${LPCAT:=${LOCALPATH}/lp.cat}" ]
then
        LPCAT="cat"
fi

if [ "$Graphics" = "yes" ]
then
        FILTER="${LPCAT} 0"
fi


(

if [ "$Landscape" = yes ]
then
        echo "\033E\033&l1o5.45c66F\033(s16.66H\c"
        [ "$tencpi" = yes ] &&  echo "\033(s10H\c"
elif [ "$Wide" = yes ]
then
        echo "\033E\033&l0o5.47c88F\033(s16.66H\c"
else
        echo "\033E\033&a0L\033&l0o7.27c66F\c"  

fi

if [ "$Duplex" = "yes" ]
then
        echo "\033&l1S\c"
else
        echo "\033&l0S\c"
fi

if [ "$sixlines" = yes ]
then
        echo "\033&l6D\c"
fi

[ "$banner" = yes ] && {
        # get the local system id
        if test -r /etc/systemid; then
                sysid=`sed 1q /etc/systemid`
        else
                sysid=`uname -n`
        fi

        # user = fifth field of /etc/passwd
        user=`sed -n "s/^$name:.*:.*:.*:\(.*\):.*:.*$/\1/p" /etc/passwd`

        # nhead gets the value of BANNERS or 1 by default
        nhead=`sed -n 's/^BANNERS=//p' /etc/default/lpd`
        [ "$nhead" -ge 0 -a "$nhead" -le 5 ] || nhead=1

        # print the banner $nhead times
        while   [ "$nhead" -gt 0 ]
        do
                echo "$x\n"
                banner "$name"
                echo "$x\n"
                [ "$user" ] && echo "User: $user\n"
                echo "Request id: $request\n"
                echo "Printer: $printer\n"
                echo "Options: $options\n"
                date
                echo "\nMachine: $sysid\n"
                [ "$title" ] && banner $title
                echo "\f\c"
                nhead=`expr $nhead - 1`
        done
}

# send the file(s) to the standard out $copies times
while   [ "$copies" -gt 0 ]

do
        for file
        do
                0<${file} eval ${FILTER} 

        done
        copies=`expr $copies - 1`
done ) | /usr/local/bin/netcat -h $PRTHOST -p $PRTPORT
sleep 1 
exit 0 




> Modified /etc/printers:
printer:printserver host:port
laser1:hpnet1:9100:HP 4050D in accounting
boblaser:hpnet2:9100:Lexmark T522N 192.168.10.20
 

The comment field was added and these comments appear as printer comments with the scoadmin printer screen.

Feb 2007: Steve added the following:

The following are changes to my /etc/printers and printeradmin scripts to reflect their current evolution.

Printeradmin and /etc/printers have been modified to add a field to specify lp model name in the /etc/printers file.

Please update the code on your website to reflect the current versions..

The files printers, HPLaserJet.net, Netcat.dotmatrix, /etc/printers printeradmin, ffstrip and ffstrip.c are contained in the ftp://aplawrence.com/pub/fabac_netcat_files.tgz

/etc/printers:

----Start----
# /etc/printers
# "/usr/local/bin/printeradmin add | sh" will add all printers below
# "/usr/local/bin/printeradmin add printer_name | sh" will add the named printer
# "/usr/local/bin/printeradmin"  will print help message
#
# NOTE: the example record format below has semicolon in place of colon (:)
#       replace the ";" with ":" in active records below
#
# printer_name;host_name;port_number;comment;model
#
# printer_name: UNIX printer name
#    host_name: Host name in /etc/hosts for IP address
#         port: Printserver dependent 9101 for HPJet direct
#      comment: Comment that appears in mkdev lp screen
#        model: /usr/spool/lp/model/model_name
#
#  Stock netcat models: HPLaserJet.net Netcat.dotmatrix
#
# Example of Entry for ActiveFax HP laserJet emulation
# Must embed ActiveFax fields in print stream prior to sending to lp.
#fax;faxserver;9150;Active Fax server on Windows host;HPLaserJet.net
#milan2;milan2;2000;Laser in Accounting 192.168.10.240;HPLaserJet.net
#axis1;axis1;9101;Laser in Engineering 192.168.10.230;HPLaserJet.net
#hpig;hp3;9101;HP 4050N Laser in shipping 192.168.10.231;HPLaserJet.net
#lexmark;lexlaser1;9100;Lexmark T522N in shipping;HPLaserJet.net
fax:win981:9150:ActivFax server on Win98 192.168.111.10:HPLaserJet.net
homeptr:localhost:SSH_PRINTER:remote printer for ssh USER:homeptr
report:report:report:Print to /tmp/report.out:Netcat.dotmatrix
----End-----

/usr/local/bin/printeradmin:

----Start----
option=$1
tprinter=$2
[ -z "$option" ] || opt="opt=$1"
[ -z "$tprinter" ] || printer="ptr="$2
awk  ' BEGIN { FS = ":" }
{
#       print ARGC, ARGV[0], ARGV[1], ARGV[2] , ARGV[3], ARGV[4]

       if ( ARGC < 3 ) {
               printf "\n\n Usage: printeradmin add    <- create all printers in /etc/printers\n"
               printf "        printeradmin delete <- delete all printers in /etc/printers\n\n"
               printf "\n\n Usage: printeradmin add    name <- create named printer in /etc/printers\n"
               printf "        printeradmin delete name <- delete named printer in /etc/printers\n\n"
       exit
       }
       if ( ARGC == 3 ) {
       if ( NF == 5 ) {
               if ( opt == "add" ) {
                       printf "/usr/lib/lpadmin -p %s -v /dev/null -m %s -D \042%s\042\n", $1, $5, $4
                       printf "/usr/lib/accept  %s\n", $1
                       printf "enable  %s\n", $1
               }
               if ( opt == "delete" ) {
                       printf "/usr/lib/lpadmin -x %s \n", $1
               }
       }
       }
       if ( ARGC == 4 ) {
       if ( NF == 5 ) {
               if ( opt == "add" && ptr == $1 ) {
                       printf "/usr/lib/lpadmin -p %s -v /dev/null -m %s -D \042%s\042\n", $1, $5, $4
                       printf "/usr/lib/accept  %s\n", $1
                       printf "enable  %s\n", $1
               }
               if ( opt == "delete"  && ptr == $1 ) {
                       printf "/usr/lib/lpadmin -x %s \n", $1
               }
       }
       }
} ' $opt $printer /etc/printers
----End------
 


Got something to add? Send me email.





(OLDER)    <- More Stuff -> (NEWER)    (NEWEST)   

Printer Friendly Version

-> -> Setting up Netcat Printers




Increase ad revenue 50-250% with Ezoic


More Articles by © Steve Fabac



Kerio Samepage


Have you tried Searching this site?

Unix/Linux/Mac OS X support by phone, email or on-site: Support Rates

This is a Unix/Linux resource website. It contains technical articles about Unix, Linux and general computing related subjects, opinion, news, help files, how-to's, tutorials and more.

Contact us





An editor is a person who knows precisely what he wants, but isn't quite sure. (Walter Davenport)

FORTRAN—the "infantile disorder"—, by now nearly 20 years old, is hopelessly inadequate for whatever computer application you have in mind today: it is now too clumsy, too risky, and too expensive to use. (Edsger W. Dijkstra)












This post tagged: