Gentoo Forums
Gentoo Forums
Gentoo Forums
Quick Search: in
Perl Script to admin LPR queue printing.
View unanswered posts
View posts from last 24 hours

 
Reply to topic    Gentoo Forums Forum Index Documentation, Tips & Tricks
View previous topic :: View next topic  
Author Message
exklusve
Apprentice
Apprentice


Joined: 23 Aug 2002
Posts: 270
Location: currently ~/

PostPosted: Thu Mar 17, 2005 7:15 pm    Post subject: Perl Script to admin LPR queue printing. Reply with quote

I wrote this script to help out some of the non-*nix guys here where i work.
You can add print que's, kill jobs, user jobs, down and up que's, etc.
This is one of my first perl scripts so i'm sure its not written very well, but it works good for what I use it for. :)
Under the subroutine 'info' we collect information about the remote printer. Here we have quite a few Toshiba Estudio printers. Their lpr print que name is 'Port1' instead of the normal 'raw' for most HP printers. I made this an available option when adding a new printer. Also I have two options set for normal or condensed printing. This is where the two filters come into play. I have posted the code for the two filters that use PCL codes to modify the printing from normal text to condensed text.
Hope someone can use this! :)
Code:
#! /usr/bin/perl -w
#use strict;
##################################################################
##################################################################
# Lprcontrol.pl                                         
# 01/09/05                                             
# eXklusve - eXklusve@hotmail.com                                   
#                                                       
#                                                       
# Change log:                                           
# 03/05/05 - Added the list of printers in menu 3 4   
# 03/08/05 - Added info on the printers in above menu
#
#
##################################################################
##################################################################


#Ok lets get some sort of ID please.  License and registration too.
chomp ($whoami = `memberof printq`);

if (!($whoami eq "Yes")) {
  die("$0: You do not have the needed permissions to admin print queues.\n");
}

#Start off with the main menu
&main;

##################################################################
#This is the top menu which will be at the top of all the menu's and sub routines.
#


sub menu {
  system "clear";
  print "\n";
  print "             PRINTER ADMINISTRATION MENU\n\n";
#  print "                 PRINT SPOOLER MENU\n\n";   
 
  $me = `whoami`;
  print "                   Welcome $me\n";


}
###################################################################
#Main sub routine for the main menu
#


sub main {
  &menu;
  print "                1. Print file.\n";
  print "                2. Print Queue Administration.\n";
  print "                3. Print Job Administration.\n";
  print "                4. Printer Administration.\n\n";


  print "       Enter choice or <ENTER> to exit menu:";

chomp ($choice = <STDIN>);
if ($choice eq 1) {
  &print;
  } if ($choice eq 2) {
  &queue;
  } if ($choice eq 3) {
  &job;
  } if ($choice eq 4) {
  &admin;
}

####################################################################
#Sub routine to print a single file from command line
#


sub print {
  &menu;
  print "Enter file name with full path:";
  chomp ($file = <STDIN>);
  print "Enter printer queue to use:";
  chomp ($printer = <STDIN>);
  system "lp -o nobanner -d $printer $file";
  &main;
}

####################################################################
#Sub routine for queue administration menu.
#


sub queue {
  &menu;
  print "                1. Restart Print Queue.\n";
  print "                2. Stop Print Queue.\n";
  print "                3. Start Print Queue.\n\n";
  print "                4. Back to Previous Menu.\n\n";

  print "       Enter choice or <ENTER> to exit menu:";
 
  chomp ($choice = <STDIN>);
  if ($choice eq 1) {
  print "Enter printer queue to restart:";
  chomp ($printer = <STDIN>);
  system "lpc -P$printer down && lpc -P$printer up";
  &queue;
  }
  if ($choice eq 2) {
  print "Enter printer queue to Stop:";
  chomp ($printer = <STDIN>);
  system "lpc -P$printer down";
  &queue;
  }
  if ($choice eq 3) {
  print "Enter printer queue to Start:";
  chomp ($printer = <STDIN>);
  system "lpc -P$printer up";
  &queue;
  } 
  if ($choice eq 4) {
  &main;
  }
}

####################################################################
#Sub routine for job administration menu.
#

sub job {
  &menu;
  print "                1. Remove ALL Jobs From Queue (Use with caution).\n";
  print "                2. Remove All User Jobs from Queue.\n";
  print "                3. Queue Status.\n";
  print "                4. Back to Previous Menu.\n\n";

  print "       Enter choice or <ENTER> to exit menu:";

  chomp ($choice = <STDIN>);
  if ($choice eq 1) {
  print "Enter printer queue to flush:";
  chomp ($printer = <STDIN>);
  system "lprm -P$printer all";
  print " Press [enter] to continue: ";my $null = <>;
  &job;
  }
  if ($choice eq 2) {
  print "Enter user name of jobs to be killed:";
  chomp ($user = <STDIN>);
  print "Enter printer queue:";
  chomp ($printer = <STDIN>);
  system "lprm -P$printer $user";
  print " Press [enter] to continue: ";my $null = <>;
  &job;
  }
  if ($choice eq 3) {
  print "Enter printer queue:";
  chomp ($printer = <STDIN>);
  system "/usr/sbin/lpc -P$printer status";
  print " Press [enter] to continue: ";my $null = <>;
  &job;
  }
  if ($choice eq 4) {
  &main;
  }
}
system "clear";


#####################################################################
#Sub routine for printer administration
#

sub admin {
  &menu;
  print "\n";
  print "             1. Add Printer\n";
  print "             2. Show pending changes\n";
  print "             3. Commit pending changes\n";
  print "             4. Clear pending changes\n\n";
  print "       Enter choice or <ENTER> to exit menu:";
 chomp ($choice = <STDIN>);
 if ($choice eq 1) {
 &info;
 }
 if ($choice eq 2) {
 &review;
 }
 if ($choice eq 3) {
 &commit;
 }
 if ($choice eq 4) {
 &clear;
 }
&main;
#
#Lets get the info needed in &add
#
sub info {
 &menu;
 print "              Please enter printer que name to add:";
 chomp ($printer = <STDIN>);
 print "\n";
 print "              Please enter the IP address of the Printer:";
 chomp ($ip = <STDIN>);
 print "\n";
 print "              Is this printer a copier? (Y or N)Case Sensitive:";
 chomp ($copier = <STDIN>);
 print "\n";
 print "              Normal or condensed printing? (N or C)Case Sensitive:";
 chomp ($condensed = <STDIN>);
 print "\n";

#
#Opening a file to output this stuff too.
#

 open(FD, ">/tmp/print.txt")
  or die "Cannot create tmp file: $!";
 
 print FD "\n";
 print FD "$printer:\\\n";
 print FD ":lp=:\\\n";
 print FD ":rm=$ip:\\\n";

 if ($copier eq Y){
 print FD ":rp=port1:\\\n";
 } else {
 print FD ":rp=raw:\\\n";
 }

 print FD ":lf=$printerpath$printer.log:\\\n";

 if ($condensed eq N){
 print FD ":if=/var/spool/lpd/filters/filter:\\\n";
 } else {
 print FD ":if=/var/spool/lpd/filters/filter2:\\\n";
 }

 print FD ":sd=$printerpath$printer:\n";


 print "\n";
 close(FD)

}

#
#Sub routine to view the changes we are about to make
sub review {
 system "more /tmp/print.txt"; my $null = <>;
}
&admin;


#
#Sub routine to append the changes to /etc/printcap
sub commit {
 system "cat /tmp/print.txt >> /etc/printcap";
 print "File Appended\n";
 print "Removing Unused File\n"; my $null = <>;
 system "echo > /tmp/print.txt";
&admin;
}


#
#Subroutine to clear the /tmp/print.txt file
sub clear {
 system "echo > /tmp/print.txt";
 print "Cleared tmp file\n"; my $null = <>;

&admin;
}
}
}




filter - normal printing
Code:
#! /usr/bin/perl
# The above line should really have the whole path to perl

# This script must be executable: chmod 755 filter
while(<STDIN>){chomp $_; print "$_\r\n";};
# You might also want to end with a form feed: print "\f";



filter2 -condensed printing
Code:
#!/bin/sh

# Filter for reports for the Line Printer font in 132 columns, in size 10 font.

echo -ne \\033\E  #reset
echo -ne \\033\&k2G  #do a CR+LF when it sees a CR
echo -ne \\033\&l10O
echo -ne \\033\(s0p16.67h8.5v0s0bT
echo -ne \\033\&l8D #vertial spacing 8lpi
cat
#echo -ne \\033\014

_________________
eXklusve

You do know that no one gives a shit about your system spec's right?
Back to top
View user's profile Send private message
exklusve
Apprentice
Apprentice


Joined: 23 Aug 2002
Posts: 270
Location: currently ~/

PostPosted: Fri Oct 21, 2005 9:45 pm    Post subject: Reply with quote

bumping this up so more folks can see it. :)
_________________
eXklusve

You do know that no one gives a shit about your system spec's right?
Back to top
View user's profile Send private message
Display posts from previous:   
Reply to topic    Gentoo Forums Forum Index Documentation, Tips & Tricks All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum