forked from CauldronDevelopmentLLC/oh-brother
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathoh-brother.py
executable file
·283 lines (216 loc) · 7.49 KB
/
oh-brother.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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
#!/usr/bin/env python
#
# Oh Brother, Brother printer firmware update program
# Copyright (C) 2015 Cauldron Development LLC
# Author Joseph Coffland
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
# TODO:
# - should definitely attempt to have this functionality
# hooked up to / integrated with
# the new generic fwupd.org Linux service
# Yes indeed, "SELIALNO"
# (as used both in this here document and in script parts below)
# is a spelling issue crime committed by original vendor parts
# and thus expected to remain exactly as wrongly written.
# Thus it obviously should *not* be "corrected" here.
reqInfo = '''
<REQUESTINFO>
<FIRMUPDATETOOLINFO>
<FIRMCATEGORY></FIRMCATEGORY>
<OS>LINUX</OS>
<INSPECTMODE>1</INSPECTMODE>
</FIRMUPDATETOOLINFO>
<FIRMUPDATEINFO>
<MODELINFO>
<SELIALNO></SELIALNO>
<NAME></NAME>
<SPEC></SPEC>
<DRIVER></DRIVER>
<FIRMINFO>
<FIRM></FIRM>
</FIRMINFO>
</MODELINFO>
<DRIVERCNT>1</DRIVERCNT>
<LOGNO>2</LOGNO>
<ERRBIT></ERRBIT>
<NEEDRESPONSE>1</NEEDRESPONSE>
</FIRMUPDATEINFO>
</REQUESTINFO>
'''
password = None
verbose = False
debug_dump_web_service_request_content = False
debug_dump_web_service_response_content = False
show_firmware_upgrade_safety_prompt = True
from pysnmp.entity.rfc3413.oneliner import cmdgen
import xml.etree.ElementTree as ET
import xml.dom.minidom as minidom
import sys
def print_pretty(elem):
s = ET.tostring(elem, 'utf-8')
print minidom.parseString(s).toprettyxml(indent = ' ')
# Parse args
if len(sys.argv) < 2:
print 'Usage: %s [OPTIONS] <printer IP address>' % sys.argv[0]
print
print 'Options:'
print ' -v Verbose mode'
sys.exit(1)
for arg in sys.argv[1:]:
if arg == '-v': verbose = True
else: ip = arg
# Get SNMP data
print 'Getting SNMP data from printer at %s...' % ip
sys.stdout.flush()
cg = cmdgen.CommandGenerator()
error, status, index, table = cg.nextCmd(
cmdgen.CommunityData('public'),
cmdgen.UdpTransportTarget((ip, 161)),
'1.3.6.1.4.1.2435.2.4.3.99.3.1.6.1.2')
print 'done'
if error: raise error
if status:
raise 'ERROR: %s at %s' % (status.prettyPrint(),
index and table[-1][int(index) - 1] or '?')
# Process SNMP data
serial = None
model = None
spec = None
firmId = None
firmInfo = []
if verbose: print table
for row in table:
for name, value in row:
value = value.prettyPrint()
if value.find('=') != -1:
name, value = value.split('=')
value = value.strip('" ')
if name == 'MODEL': model = value
if name == 'SERIAL': serial = value
if name == 'SPEC': spec = value
if name == 'FIRMID': firmId = value
if name == 'FIRMVER':
firmInfo.append({'cat': firmId, 'version': value})
# Print SNMP info
print
print ' serial =', serial
print ' model =', model
print ' spec =', spec
print ' firmwares'
for entry in firmInfo:
print ' category = %(cat)s, version = %(version)s' % entry
print
# We need SSLv3
import ssl
from functools import wraps
def sslwrap(func):
@wraps(func)
def bar(*args, **kw):
kw['ssl_version'] = ssl.PROTOCOL_TLSv1
return func(*args, **kw)
return bar
ssl.wrap_socket = sslwrap(ssl.wrap_socket)
def update_firmware(cat, version):
global password
print 'Updating %s version %s' % (cat, version)
# Build XML request info
xml = ET.ElementTree(ET.fromstring(reqInfo))
# At least for MFC-J4510DW M1405200717:EFAC (see Internet dumps)
# and MFC-J4625DW,
# this element's value is *not* equal to per-firmware cat[egory] value
# (a "MAIN"-deviating "FIRM" in these cases!),
# but rather a *fixed* "MAIN" value which is a completely unrelated item,
# thus I assume this to model-unconditionally have been a BUG
# (which causes a failure response of the web service request).
#xml.find('FIRMUPDATETOOLINFO/FIRMCATEGORY').text = cat
xml.find('FIRMUPDATETOOLINFO/FIRMCATEGORY').text = 'MAIN'
modelInfo = xml.find('FIRMUPDATEINFO/MODELINFO')
modelInfo.find('SELIALNO').text = serial
modelInfo.find('NAME').text = model
modelInfo.find('SPEC').text = spec
firm = modelInfo.find('FIRMINFO/FIRM')
ET.SubElement(firm, 'ID').text = cat
ET.SubElement(firm, 'VERSION').text = version
requestInfo = ET.tostring(xml.getroot(), encoding = 'utf8')
if debug_dump_web_service_request_content:
print 'request: %s' % requestInfo
# Request firmware data
url = 'https://firmverup.brother.co.jp/kne_bh7_update_nt_ssl/ifax2.asmx/' + \
'fileUpdate'
hdrs = {'Content-Type': 'text/xml'}
print 'Looking up printer firmware info at vendor server...'
sys.stdout.flush()
import urllib2
req = urllib2.Request(url, requestInfo, hdrs)
response = urllib2.urlopen(req)
response = response.read()
print 'done'
if debug_dump_web_service_response_content:
print 'response: %s' % response
# Parse response
xml = ET.fromstring(response)
if verbose: print_pretty(xml)
# Check version
versionCheck = xml.find('FIRMUPDATEINFO/VERSIONCHECK')
if versionCheck is not None and versionCheck.text == '1':
print 'Firmware already up to date'
return
# Get firmware URL
firmwareURL = xml.find('FIRMUPDATEINFO/PATH')
if firmwareURL is None:
print 'No firmware update info path found'
sys.exit(1)
firmwareURL = firmwareURL.text
filename = firmwareURL.split('/')[-1]
# Download firmware
f = open(filename, 'w')
print 'Downloading firmware file %s from vendor server...' % filename
sys.stdout.flush()
req = urllib2.Request(firmwareURL)
response = urllib2.urlopen(req)
while True:
block = response.read(102400)
if not block: break
f.write(block)
sys.stdout.write('.')
sys.stdout.flush()
print 'done'
f.close()
if show_firmware_upgrade_safety_prompt:
print 'About to upload the firmware to printer.'
print 'This is a dangerous action since it is potentially destructive.'
print 'Thus please double-check / review to ensure that:'
print '- firmware file version is compatible with your hardware'
print '- network connection is maximally reliable (strongly prefer wired connection to WLAN)'
print '- power supply is maximally reliable (may be achieved by using a UPS)'
raw_input("Press Ctrl-C to prevent firmware upgrade, or possibly Enter to continue...")
# Get printer password
if password is None:
import getpass
print
password = getpass.getpass('Enter printer admin password: ')
# Upload firmware to printer
from ftplib import FTP
print 'Now uploading firmware to printer (DO NOT REMOVE POWER!)...'
sys.stdout.flush()
ftp = FTP(ip, user = password) # Yes send password as user
ftp.storbinary('STOR ' + filename, open(filename, 'r'))
ftp.quit()
print 'done'
print
print 'Wait for printer to finish updating and reboot before continuing.'
raw_input("Press Enter to continue...")
for entry in firmInfo:
print
update_firmware(entry['cat'], entry['version'])
print
print 'Success'