Description

Print filter to redirect output to Email
This script will be invoked by lpd (from a printcap entry) with the parameters [-c] -w# -l# -i# -n logonid -h host acct-file

Source Code

#!/bin/bash
# print filter to redirect output to Email
# This script will be invoked by lpd (from a printcap entry)
# with the parameters
#   [-c] -w# -l# -i# -n logonid -h host acct-file
#

# first, drop the -c if it was supplied
if [ "$1" = "-c" ]
then
  shift
fi

optlogonid=$5
opthost=$7

# assign a name to the temporary spool file
tmpfile=/tmp/email.filter.$$

# Strip off leading flash page
/usr/bin/tail +57 $tmpfile >$tmpfile.bc

# Strip of trailing flash page
RptFlash=`/usr/bin/wc -l <$tmpfile.bc`
RptOnly=`/usr/bin/expr $RptFlash - 56`
/bin/head -$RptOnly $tmpfile.bc >$tmpfile.rpt

# Replace old spool file with stripped file
/bin/rm -f $tmpfile $tmpfile.bc
/bin/mv $tmpfile.rpt $tmpfile
#------------------------------------------------------------------------------------
/usr/bin/cat /var/spool/lpd/tools/jesprt.notice.text >>$tmpfile

# gather information about print job, to be used in email later
jobid=`/usr/bin/awk '/JOBID:/ { print $3 }' $tmpfile.flash`
jobname=`/usr/bin/awk '/JOBNAME:/ { print $3 }' $tmpfile.flash`
system=`/usr/bin/awk '/SYSTEM:/ { print $3 }' $tmpfile.flash`
email=`/usr/bin/awk -f /var/spool/lpd/tools/jesprt.mailto.awk $tmpfile.flash`
subject=`/usr/bin/awk -f /var/spool/lpd/tools/jesprt.subject.awk $tmpfile.flash`

if [ "$email" = "#UNKNOWN#" ]
then
  # Log an error, and dont respool the print
  /usr/bin/logger -t jesprt.filter "Print job ${jobname-UNKNOWN} from $optlogonid/$opthost discarded"
else
  # Spool the output to email and log the event
  /usr/bin/fastmail -s "$subject($jobname / $jobid from $system)" $tmpfile $email
  /usr/bin/logger -t jesprt.filter "Print job $jobname ($jobid) from $system to email for $email"
fi

# clean up the files, then log the activity to the syslog
/bin/rm $tmpfile $tmpfile.flash

This script was developed for a Unix ver. 7 system but can be adapted to any modern Linux or Unix system. You set it up by adding an entry in the printcap (or comparable file for print system). Instead of pointing to a printer driver/file you point it to this script.