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

Revision 1257, 3.0 kB (checked in by jalet, 21 years ago)

Copyright year changed.

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