root / pykota / trunk / contributed / pagecount.pl @ 1243

Revision 1243, 2.9 kB (checked in by uid67467, 20 years ago)

Added Perl script which does PJL accounting, contributed by Ren� Lund Jensen

  • Property svn:eol-style set to native
  • Property svn:executable set to *
  • Property svn:keywords set to Author Date Id Revision
Line 
1#!/usr/bin/perl -U
2#
3# PyKota : Print Quotas for CUPS and LPRng
4#
5# (c) 2003 Jerome Alet <alet@librelogiciel.com>
6# This program is free software; you can redistribute it and/or modify
7# it under the terms of the GNU General Public License as published by
8# the Free Software Foundation; either version 2 of the License, or
9# (at your option) any later version.
10#
11# This program is distributed in the hope that it will be useful,
12# but WITHOUT ANY WARRANTY; without even the implied warranty of
13# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14# GNU General Public License for more details.
15#
16# You should have received a copy of the GNU General Public License
17# along with this program; if not, write to the Free Software
18# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
19#
20#
21############################################################
22#                                                          #
23# This script is 100% copyright (c) 2003 Ren�und Jensen  #
24#                                                          #
25# He contributed it to the PyKota project on Dec. 4th 2003 #
26# and licensed it under the terms of the GNU GPL.          #
27#                                                          #
28# MANY THANKS TO HIM                                       #
29#                                                          #
30############################################################
31#
32#
33# $Id$
34#
35# $Log$
36# Revision 1.1  2003/12/27 16:57:42  uid67467
37# Added Perl script which does PJL accounting, contributed by Ren�und Jensen
38#
39# Revision 1.1  2003/12/06 09:03:43  jalet
40# Added Perl script to retrieve printer's internal page counter via PJL,
41# contributed by Ren�und Jensen.
42#
43#
44#
45
46use Socket;
47use IO::Socket;
48
49if (@ARGV < 2){
50    print "usage: pagecount.pl servername port\n";
51}
52
53$printer = @ARGV[0];
54$port    = @ARGV[1];
55
56$ssh = osocket($printer, $port);
57if ($ssh){
58    $page = pagecount($ssh);
59    print $page."\n";
60    $ssh-close();
61    exit(0);
62}else {
63    exit(1);
64}
65
66sub pagecount {
67    my $sh = @_[0];    # Get sockethandle
68    # send pagequery to sockethandle
69    send($sh, "\033%-12345X\@PJL INFO PAGECOUNT\r\n",0);
70    # Read response from sockethandle
71    recv($sh,$RESPONSE,0xFFFFF,0);
72    (my $junk,$pc) = split (/\r\n/s,$RESPONSE); # Find the pagecount
73    $pc =~ s/(PAGECOUNT=)?([0-9]+)/$2/g;
74    return $pc;                                 # Return pagecount
75}
76
77
78sub osocket {
79
80 # Connecting to @_[0] = @arg[1] = $printer
81 # On port @_[1] = 9100 JetDirect port
82 # Using TCP protocol
83    my $sh= new IO::Socket::INET(PeerAddr => @_[0],
84                                 PeerPort => @_[1], 
85                                 Proto => 'tcp');
86    if (!defined($sh)) {        # Did we open the socket?
87        return undef;           # No! return undef
88    } else {                    # Yes!
89        $sh->sockopt(SO_KEEPALIVE,1);   # Set socket option SO_KEEPALIVE
90        return $sh;             # return sockethandle
91    }
92}
Note: See TracBrowser for help on using the browser.