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

Revision 2028, 3.1 kB (checked in by jalet, 20 years ago)

Modified copyright years

  • 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, 2004, 2005 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.3  2005/01/17 08:44:24  jalet
37# Modified copyright years
38#
39# Revision 1.2  2004/01/08 14:10:32  jalet
40# Copyright year changed.
41#
42# Revision 1.1  2003/12/27 16:57:42  uid67467
43# Added Perl script which does PJL accounting, contributed by Ren�und Jensen
44#
45# Revision 1.1  2003/12/06 09:03:43  jalet
46# Added Perl script to retrieve printer's internal page counter via PJL,
47# contributed by Ren�und Jensen.
48#
49#
50#
51
52use Socket;
53use IO::Socket;
54
55if (@ARGV < 2){
56    print "usage: pagecount.pl servername port\n";
57}
58
59$printer = @ARGV[0];
60$port    = @ARGV[1];
61
62$ssh = osocket($printer, $port);
63if ($ssh){
64    $page = pagecount($ssh);
65    print $page."\n";
66    $ssh-close();
67    exit(0);
68}else {
69    exit(1);
70}
71
72sub pagecount {
73    my $sh = @_[0];    # Get sockethandle
74    # send pagequery to sockethandle
75    send($sh, "\033%-12345X\@PJL INFO PAGECOUNT\r\n",0);
76    # Read response from sockethandle
77    recv($sh,$RESPONSE,0xFFFFF,0);
78    (my $junk,$pc) = split (/\r\n/s,$RESPONSE); # Find the pagecount
79    $pc =~ s/(PAGECOUNT=)?([0-9]+)/$2/g;
80    return $pc;                                 # Return pagecount
81}
82
83
84sub osocket {
85
86 # Connecting to @_[0] = @arg[1] = $printer
87 # On port @_[1] = 9100 JetDirect port
88 # Using TCP protocol
89    my $sh= new IO::Socket::INET(PeerAddr => @_[0],
90                                 PeerPort => @_[1], 
91                                 Proto => 'tcp');
92    if (!defined($sh)) {        # Did we open the socket?
93        return undef;           # No! return undef
94    } else {                    # Yes!
95        $sh->sockopt(SO_KEEPALIVE,1);   # Set socket option SO_KEEPALIVE
96        return $sh;             # return sockethandle
97    }
98}
Note: See TracBrowser for help on using the browser.