| 1 |
teddy |
1.1 |
#!/usr/bin/python |
| 2 |
|
|
# |
| 3 |
|
|
# lpr-client - communicate with lpr-agent |
| 4 |
|
|
# Copyright (C) 2000 Teddy Hogeborn |
| 5 |
|
|
# |
| 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 |
|
|
# Author: Teddy Hogeborn <teddy@fukt.hk-r.se> |
| 21 |
|
|
# Folkparksv. 22 |
| 22 |
|
|
# 372 38 Ronneby |
| 23 |
|
|
# SWEDEN |
| 24 |
|
|
|
| 25 |
|
|
import os, sys, getopt, errno, fileinput |
| 26 |
|
|
from socket import * |
| 27 |
|
|
|
| 28 |
|
|
# Parse the command line options. |
| 29 |
|
|
optlist, args = getopt.getopt(sys.argv[1:], 'P:', ['debug-server', \ |
| 30 |
|
|
'version', 'help', \ |
| 31 |
|
|
'print', 'write=', \ |
| 32 |
|
|
'password', 'eval', \ |
| 33 |
|
|
'command=', 'debug', \ |
| 34 |
|
|
'kill', \ |
| 35 |
|
|
'block-size',\ |
| 36 |
|
|
'smbclientformat=', \ |
| 37 |
|
|
'server=', \ |
| 38 |
|
|
'printer=' \ |
| 39 |
|
|
'service=', \ |
| 40 |
|
|
'serverip=', \ |
| 41 |
|
|
'smbclientoptions=', \ |
| 42 |
|
|
'smbclientcommand=']) |
| 43 |
|
|
|
| 44 |
|
|
def has_option(option, optlist): |
| 45 |
|
|
"Returns a non-false value if OPTION exists in OPTLIST." |
| 46 |
|
|
return filter(lambda el, opt=option: el[0]==opt, optlist) |
| 47 |
|
|
|
| 48 |
|
|
def option_arg(arg, optlist): |
| 49 |
|
|
"Returns the argument of the option ARG from OPTLIST." |
| 50 |
|
|
return has_option(arg, optlist)[0][1] |
| 51 |
|
|
|
| 52 |
|
|
if has_option('--version', optlist): |
| 53 |
|
|
print """lpr-client (lpr-agent) 1.0 |
| 54 |
|
|
Copyright (C) 2000 Teddy Hogeborn |
| 55 |
|
|
lpr-client comes with ABSOLUTELY NO WARRANTY. |
| 56 |
|
|
You may redistribute copies of lpr-client under the terms of the GNU |
| 57 |
|
|
General Public License. For more information about these matters, see |
| 58 |
|
|
the file named COPYING.""" |
| 59 |
|
|
sys.exit(0) |
| 60 |
|
|
|
| 61 |
|
|
if has_option('--help', optlist): |
| 62 |
|
|
print """lpr-client communicates with an lpr-agent and can send files to it for |
| 63 |
|
|
printing, and a password for it to use when authenticating with the |
| 64 |
|
|
SMB printer server. |
| 65 |
|
|
|
| 66 |
|
|
Usage: lpr-client [options] [filename ... ] |
| 67 |
|
|
|
| 68 |
|
|
Examples of common usage: |
| 69 |
|
|
Set a password (must be done first): |
| 70 |
|
|
ssh-askpass | lpr-client --password |
| 71 |
|
|
Print a file: |
| 72 |
|
|
lpr-client foobar.ps |
| 73 |
|
|
Print a file on an alternative printer: |
| 74 |
|
|
lpr-client --printer=rby_3 foobar.ps |
| 75 |
|
|
Print a file on an alternative server and printer: |
| 76 |
|
|
lpr-client --service="//obelix/rby_4" foobar.ps |
| 77 |
|
|
Kill the agent (when logging out or when not printing anymore): |
| 78 |
|
|
lpr-client --kill |
| 79 |
|
|
|
| 80 |
|
|
Normal options: |
| 81 |
|
|
--print print files or stdin (default) |
| 82 |
|
|
--password set password, read from stdin |
| 83 |
|
|
--kill kill the agent |
| 84 |
|
|
|
| 85 |
|
|
--version show version number |
| 86 |
|
|
--help show this help |
| 87 |
|
|
|
| 88 |
|
|
--server=SERVER use a non-default printer server, |
| 89 |
|
|
this can also be specified with |
| 90 |
|
|
the environment variable |
| 91 |
|
|
LPR_CLIENT_SERVER |
| 92 |
|
|
-P, --printer=PRINTER use a non-default printer, |
| 93 |
|
|
this can also be specified with |
| 94 |
|
|
the environment variable |
| 95 |
|
|
LPR_CLIENT_PRINTER |
| 96 |
|
|
--service=SERVICE specify both server and printer |
| 97 |
|
|
on the form "//SERVER/PRINTER" |
| 98 |
|
|
--serverip=IP explicitly specify server IP |
| 99 |
|
|
--smbclientoptions=OPTIONS any additional options to |
| 100 |
|
|
"smbclient" |
| 101 |
|
|
|
| 102 |
|
|
Obscure or debugging options: |
| 103 |
|
|
--debug show debugging information |
| 104 |
|
|
--block-size=SIZE use blocks of the specified size |
| 105 |
|
|
--smbclientformat=FORMAT change the form of how |
| 106 |
|
|
"smbclient" is invoked with the |
| 107 |
|
|
specified parameters |
| 108 |
|
|
--smbclientcommand=COMMANDS change the command send to |
| 109 |
|
|
"smbclient" |
| 110 |
|
|
--write=FILE make the agent write the input |
| 111 |
|
|
to the specified file name |
| 112 |
|
|
--eval make the agent `eval' the input |
| 113 |
|
|
as Python commands |
| 114 |
|
|
--debug-server send the "debug" command to the |
| 115 |
|
|
agent |
| 116 |
|
|
--command=COMMAND send a specific command to the |
| 117 |
|
|
agent |
| 118 |
|
|
|
| 119 |
|
|
Report bugs to <teddy@fukt.hk-r.se>. |
| 120 |
|
|
""" |
| 121 |
|
|
sys.exit(0) |
| 122 |
|
|
|
| 123 |
|
|
headers="" |
| 124 |
|
|
|
| 125 |
|
|
if has_option('--smbclientformat', optlist): |
| 126 |
|
|
headers = headers + "SmbClientFormat: " \ |
| 127 |
|
|
+ option_arg('--smbclientformat', optlist) + "\r\n" |
| 128 |
|
|
|
| 129 |
|
|
server = None |
| 130 |
|
|
|
| 131 |
|
|
if has_option('--server', optlist): |
| 132 |
|
|
server = option_arg('--server', optlist) |
| 133 |
|
|
elif os.environ.has_key('LPR_CLIENT_SERVER'): |
| 134 |
|
|
server=os.environ['LPR_CLIENT_SERVER'] |
| 135 |
|
|
|
| 136 |
|
|
if server: |
| 137 |
|
|
headers = headers + "SmbServer: " + server + "\r\n" |
| 138 |
|
|
|
| 139 |
|
|
printer = None |
| 140 |
|
|
|
| 141 |
|
|
if has_option('--printer', optlist): |
| 142 |
|
|
printer = option_arg('--printer', optlist) |
| 143 |
|
|
elif has_option('-P', optlist): |
| 144 |
|
|
printer = option_arg('-P', optlist) |
| 145 |
|
|
elif os.environ.has_key('LPR_CLIENT_PRINTER'): |
| 146 |
|
|
printer=os.environ['LPR_CLIENT_PRINTER'] |
| 147 |
|
|
|
| 148 |
|
|
if printer: |
| 149 |
|
|
headers = headers + "SmbPrinter: " + printer + "\r\n" |
| 150 |
|
|
|
| 151 |
|
|
if has_option('--service', optlist): |
| 152 |
|
|
headers = headers + "SmbService: " \ |
| 153 |
|
|
+ option_arg('--service', optlist) + "\r\n" |
| 154 |
|
|
|
| 155 |
|
|
if has_option('--serverip', optlist): |
| 156 |
|
|
headers = headers + "SmbServerIP: " \ |
| 157 |
|
|
+ option_arg('--serverip', optlist) + "\r\n" |
| 158 |
|
|
|
| 159 |
|
|
if has_option('--smbclientoptions', optlist): |
| 160 |
|
|
headers = headers + "SmbClientOptions: " \ |
| 161 |
|
|
+ option_arg('--smbclientoptions', optlist) + "\r\n" |
| 162 |
|
|
|
| 163 |
|
|
if has_option('--smbclientcommand', optlist): |
| 164 |
|
|
headers = headers + "SmbClientCommand: " \ |
| 165 |
|
|
+ option_arg('--smbclientcommand', optlist) + "\r\n" |
| 166 |
|
|
|
| 167 |
|
|
if has_option('--command', optlist): |
| 168 |
|
|
command = option_arg('--command', optlist) |
| 169 |
|
|
elif has_option('--debug-server', optlist): |
| 170 |
|
|
command = "debug" |
| 171 |
|
|
elif has_option('--write', optlist): |
| 172 |
|
|
command = "write" |
| 173 |
|
|
headers = headers + "Filename: " + option_arg('--write', optlist) \ |
| 174 |
|
|
+ "\r\n" |
| 175 |
|
|
elif has_option('--password', optlist): |
| 176 |
|
|
command = "password" |
| 177 |
|
|
elif has_option('--eval', optlist): |
| 178 |
|
|
command = "eval" |
| 179 |
|
|
elif has_option('--kill', optlist): |
| 180 |
|
|
command = "quit" |
| 181 |
|
|
else: # Default is print |
| 182 |
|
|
command = "print" |
| 183 |
|
|
|
| 184 |
|
|
debug_flag = has_option('--debug', optlist) |
| 185 |
|
|
|
| 186 |
|
|
if debug_flag: |
| 187 |
|
|
print "Command: " + command |
| 188 |
|
|
|
| 189 |
|
|
if os.environ.has_key('LPR_AUTH_SOCK'): |
| 190 |
|
|
socket_name=os.environ['LPR_AUTH_SOCK'] |
| 191 |
|
|
else: |
| 192 |
|
|
print "No agent is running" |
| 193 |
|
|
sys.exit(1) |
| 194 |
|
|
|
| 195 |
|
|
if debug_flag: |
| 196 |
|
|
print "Command: " + command |
| 197 |
|
|
|
| 198 |
|
|
if has_option('--block-size', optlist): |
| 199 |
|
|
BUFSIZE = option_arg('--block-size', optlist) |
| 200 |
|
|
else: |
| 201 |
|
|
BUFSIZE=8192 |
| 202 |
|
|
|
| 203 |
|
|
# If no file names are given, assume stdin. |
| 204 |
|
|
if args==[]: |
| 205 |
|
|
args=['-'] |
| 206 |
|
|
|
| 207 |
|
|
# For each supplied file name, do... |
| 208 |
|
|
for arg in args: |
| 209 |
|
|
# Create an unnamed socket. |
| 210 |
|
|
sock = socket(AF_UNIX, SOCK_STREAM) |
| 211 |
|
|
# Connect the socket to the file name. |
| 212 |
|
|
try: |
| 213 |
|
|
sock.connect(socket_name) |
| 214 |
|
|
except error, the_error: |
| 215 |
|
|
if the_error[0] != errno.ECONNREFUSED: |
| 216 |
|
|
raise error, the_error |
| 217 |
|
|
else: |
| 218 |
|
|
print "No agent is running" |
| 219 |
|
|
sys.exit(1) |
| 220 |
|
|
if debug_flag: |
| 221 |
|
|
print "Client connected to socket" |
| 222 |
|
|
serverfile=os.fdopen(sock.fileno(), "wb") |
| 223 |
|
|
serverfile.write(command + "\r\n" + headers + "\r\n") |
| 224 |
|
|
if debug_flag: |
| 225 |
|
|
print "Client wrote command to socket" |
| 226 |
|
|
print command + "\n" + headers + "\n" |
| 227 |
|
|
if command != "quit": |
| 228 |
|
|
if arg == '-': |
| 229 |
|
|
file = sys.stdin |
| 230 |
|
|
else: |
| 231 |
|
|
file = open(arg, "rb") |
| 232 |
|
|
while 1: |
| 233 |
|
|
data = file.read(BUFSIZE) |
| 234 |
|
|
if not data: break |
| 235 |
|
|
serverfile.write(data) |
| 236 |
|
|
if arg != '-': |
| 237 |
|
|
file.close() |
| 238 |
|
|
if debug_flag: |
| 239 |
|
|
print "Client wrote file to socket" |
| 240 |
|
|
serverfile.close() |
| 241 |
|
|
sock.close() |