-
Notifications
You must be signed in to change notification settings - Fork 613
/
capabilities.py
208 lines (193 loc) · 9.18 KB
/
capabilities.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# python standard library
import re
import os
import sys # , urllib.error, urllib.parse
import requests
import urllib3
# local pret classes
from helper import output, item
# third party modules
try:
from pysnmp.entity.rfc3413.oneliner import cmdgen
except ImportError:
pass
class capabilities():
urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)
# set defaults
support = False
# be quick and dirty
timeout = 1.5
# set pret.py directory
rundir = os.path.dirname(os.path.realpath(__file__)) + os.path.sep
'''
┌──────────────────────────────────────────────────────────┐
│ how to get printer's capabilities? │
├──────┬───────────────────────┬───────────────────────────┤
│ │ model (for db lookup) │ lang (ps/pjl/pcl support) │
├──────┼───────────────────────┼───────────────────────────┤
│ IPP │ printer-description │ printer-description │
│ SNMP │ hrDeviceDescr │ prtInterpreterDescription │
│ HTTP │ html-title │ - │
| HTTPS| html-title | - |
└──────┴───────────────────────┴───────────────────────────┘
'''
def __init__(self, args):
# skip this in unsafe mode
if not args.safe:
return
# set printer language
if args.mode == 'ps':
lang = ["PS", "PostScript", "BR-Script", "KPDL"]
if args.mode == 'pjl':
lang = ["PJL"]
if args.mode == 'pcl':
lang = ["PCL"]
# get list of PostScript/PJL/PCL capable printers
self.models = self.get_models(args.mode + ".dat")
# try to get printer capabilities via IPP/SNMP/HTTP
if not self.support:
self.ipp(args.target, lang)
if not self.support:
self.http(args.target)
if not self.support:
self.https(args.target)
if not self.support:
self.snmp(args.target, lang)
# feedback on PostScript/PJL/PCL support
self.feedback(self.support, lang[0])
# in safe mode, exit if unsupported
if args.safe and not self.support:
print((os.linesep + "Quitting as we are in safe mode."))
sys.exit()
print("")
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
# get capabilities via IPP
def ipp(self, host, lang):
try:
# poor man's way to get printer info via IPP
sys.stdout.write("Checking for IPP support: ")
body = ("\x01\x01\x00\x0b\x00\x01\xab\x10\x01G\x00\x12attributes-charset\x00\x05utf-8H"
+ "\x00\x1battributes-natural-language\x00\x02enE\x00\x0bprinter-uri\x00\x14ipp:"
+ "//localhost/ipp/D\x00\x14requested-attributes\x00\x13printer-description\x03").encode()
request = requests.get("http://" + host + ":631/",
data=body, headers={'Content-type': 'application/ipp'})
response = request.content.decode()
# get name of device
# e.g. MDL:hp LaserJet 4250
model = item(re.findall("MDL:(.+?);", response))
# get language support
# e.g. CMD:PCL,PJL,POSTSCRIPT
langs = item(re.findall("CMD:(.+?);", response))
self.support = [_f for _f in [re.findall(
re.escape(pdl), langs, re.I) for pdl in lang] if _f]
self.set_support(model)
output().green("found")
except Exception as e:
output().errmsg("not found", e)
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
# get capabilities via HTTP
def http(self, host):
try:
# poor man's way get http title
sys.stdout.write("Checking for HTTP support: ")
html = requests.get("http://" + host).text
# cause we are to parsimonious to import BeautifulSoup ;)
title = re.findall("<title.*?>\n?(.+?)\n?</title>",
html, re.I | re.M | re.S)
# get name of device
model = item(title)
# get language support
self.set_support(model)
output().green("found")
except Exception as e:
output().errmsg("not found", e)
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
# get capabilities via HTTPS
def https(self, host):
try:
# poor man's way get https title
sys.stdout.write("Checking for HTTPS support: ")
html = requests.get("https://" + host, verify=False).text
# cause we are to parsimonious to import BeautifulSoup ;)
title = re.findall("<title.*?>\n?(.+?)\n?</title>",
html, re.I | re.M | re.S)
# get name of device
model = item(title)
# get language support
self.set_support(model)
output().green("found")
except Exception as e:
output().errmsg("not found", e)
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
# get capabilities via SNMP
def snmp(self, host, lang):
try:
sys.stdout.write("Checking for SNMP support: ")
# query device description and supported languages
# HOST-RESOURCES-MIB → hrDeviceDescr
desc, desc_oid = [], '1.3.6.1.2.1.25.3.2.1.3'
# Printer-MIB → prtInterpreterDescription
pdls, pdls_oid = [], '1.3.6.1.2.1.43.15.1.1.5.1'
error, error_status, idx, binds = cmdgen.CommandGenerator().nextCmd(
cmdgen.CommunityData('public', mpModel=0), cmdgen.UdpTransportTarget(
(host, 161), timeout=self.timeout, retries=0), desc_oid, pdls_oid)
# exit on error
if error:
raise Exception(error)
if error_status:
raise Exception(error_status.prettyPrint())
# parse response
for row in binds:
for key, val in row:
if desc_oid in str(key):
desc.append(str(val))
if pdls_oid in str(key):
pdls.append(str(val))
# get name of device
model = item(desc)
# get language support
langs = ','.join(pdls)
self.support = [_f for _f in [re.findall(
re.escape(pdl), langs, re.I) for pdl in lang] if _f]
# self.set_support(model)
output().green("found")
except NameError:
output().errmsg("not found", "pysnmp module not installed")
except Exception as e:
output().errmsg("not found", e)
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
# feedback on language support
def feedback(self, support, lang):
sys.stdout.write("Checking for %-21s" % (lang + " support: "))
if support:
output().green("found")
else:
output().warning("not found")
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
# set language support
def set_support(self, model):
# model_stripped = re.sub(r'(\d|\s|-)[a-zA-Z]+$', '', model)
'''
┌───────────────────────────────────────────────────────┐
│ Experimental -- This might introduce false positives! │
├───────────────────────────────────────────────────────┤
│ The stripped down version of the model string removes │
│ endings like '-series', ' printer' (maybe localized), │
│ 'd' (duplex), 't' (tray), 'c' (color), 'n' (network). │
└───────────────────────────────────────────────────────┘
'''
self.support = [_f for _f in [re.findall(
re.escape(m), model, re.I) for m in self.models] if _f]
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
# open database of supported devices
def get_models(self, file):
try:
with open(self.rundir + "db" + os.path.sep + file, 'r') as f:
models = [_f for _f in (line.strip() for line in f) if _f]
f.close()
return models
except IOError as e:
output().errmsg("Cannot open file", e)
return []