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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, 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 | # |
---|
36 | |
---|
37 | use Socket; |
---|
38 | use IO::Socket; |
---|
39 | |
---|
40 | if (@ARGV < 2){ |
---|
41 | print "usage: pagecount.pl servername port\n"; |
---|
42 | } |
---|
43 | |
---|
44 | $printer = @ARGV[0]; |
---|
45 | $port = @ARGV[1]; |
---|
46 | |
---|
47 | $ssh = osocket($printer, $port); |
---|
48 | if ($ssh){ |
---|
49 | $page = pagecount($ssh); |
---|
50 | print $page."\n"; |
---|
51 | $ssh-close(); |
---|
52 | exit(0); |
---|
53 | }else { |
---|
54 | exit(1); |
---|
55 | } |
---|
56 | |
---|
57 | sub pagecount { |
---|
58 | my $sh = @_[0]; # Get sockethandle |
---|
59 | # send pagequery to sockethandle |
---|
60 | send($sh, "\033%-12345X\@PJL INFO PAGECOUNT\r\n",0); |
---|
61 | # Read response from sockethandle |
---|
62 | recv($sh,$RESPONSE,0xFFFFF,0); |
---|
63 | (my $junk,$pc) = split (/\r\n/s,$RESPONSE); # Find the pagecount |
---|
64 | $pc =~ s/(PAGECOUNT=)?([0-9]+)/$2/g; |
---|
65 | return $pc; # Return pagecount |
---|
66 | } |
---|
67 | |
---|
68 | |
---|
69 | sub osocket { |
---|
70 | |
---|
71 | # Connecting to @_[0] = @arg[1] = $printer |
---|
72 | # On port @_[1] = 9100 JetDirect port |
---|
73 | # Using TCP protocol |
---|
74 | my $sh= new IO::Socket::INET(PeerAddr => @_[0], |
---|
75 | PeerPort => @_[1], |
---|
76 | Proto => 'tcp'); |
---|
77 | if (!defined($sh)) { # Did we open the socket? |
---|
78 | return undef; # No! return undef |
---|
79 | } else { # Yes! |
---|
80 | $sh->sockopt(SO_KEEPALIVE,1); # Set socket option SO_KEEPALIVE |
---|
81 | return $sh; # return sockethandle |
---|
82 | } |
---|
83 | } |
---|