jemfinch
12-14-2000, 08:59 PM
Yeah. We don't need no stinkin' parentheses around the s! I've got more than one JUST BY MYSELF!!
http://www.linuxnewbie.org/ubb/smile.gif
Anyway, here are a few things I've been whipping up.
md5sum.py:
import struct, string, md5
def md5sum(filename):
m = md5.new()
fd = open(filename, 'r')
m.update(fd.read())
return string.join(map(lambda x: '%02x' % x, struct.unpack('B' * 16, m.digest())), '')
if __name__ == '__main__':
import sys
if len(sys.argv) < 2:
print '\n' + md5sum('/dev/fd/0')
sys.exit(0)
for filename in sys.argv[1:]:
try:
sum = md5sum(filename)
except Exception, e:
sys.stderr.write('%s\n' % e)
print '%s %s' % (sum, filename)
sys.exit(0)
shasum.py:
import struct, string, sha
def shasum(filename):
s = sha.new()
fd = open(filename, 'r')
s.update(fd.read())
return string.join(map(lambda x: '%02x' % x, struct.unpack('B' * 20, s.digest())), '')
if __name__ == '__main__':
import sys
if len(sys.argv) < 2:
print '\n' + shasum('/dev/fd/0')
sys.exit(0)
for filename in sys.argv[1:]:
try:
sum = shasum(filename)
except Exception, e:
sys.stderr.write('%s\n' % e)
print '%s %s' % (sum, filename)
sys.exit(0)
check_debian_package_sums.py:
import glob, sys, string, md5sum # (md5sum from above)
if len(sys.argv) < 2:
sys.stderr.write('Usage: %s <logfile>\n' % sys.argv[0])
sys.exit(-1)
try:
log = open(sys.argv[1], 'w+')
except Exception, e:
sys.stderr.write('%s\n' % e)
sys.exit(-1)
for sums_file in glob.glob('/var/lib/dpkg/info/*.md5sums'):
for line in open(sums_file, 'r').readlines():
expected_sum, filename = string.split(line)
filename = '/' + filename
try:
real_sum = md5sum.md5sum(filename)
except Exception, e:
log.write('%s\n' % e)
continue
if real_sum != expected_sum:
log.write('%s did not match expected sum\n' % filename)
Now, I didn't do an exact copy/paste since that code is on a different computer than the one I'm posting this from. I hope I didn't mess up any of the typing, but if someone goes to all the trouble to run this and does get an error, please, let me know so everyone doesn't get the same error.
I've got a few other tools I've whipped up; a "find" program that finds based on regular expressions rather than shell globs, and a small, robust, spoofing identd server. If anyone's interested, I can post those, but it's tough to roll back and forth between these two not-networked computers, so only if someone asks http://www.linuxnewbie.org/ubb/smile.gif
Anyone else have snippets? As opposed to some other languages, reading other people's code in python is actually enjoyable http://www.linuxnewbie.org/ubb/smile.gif
Jeremy
http://www.linuxnewbie.org/ubb/smile.gif
Anyway, here are a few things I've been whipping up.
md5sum.py:
import struct, string, md5
def md5sum(filename):
m = md5.new()
fd = open(filename, 'r')
m.update(fd.read())
return string.join(map(lambda x: '%02x' % x, struct.unpack('B' * 16, m.digest())), '')
if __name__ == '__main__':
import sys
if len(sys.argv) < 2:
print '\n' + md5sum('/dev/fd/0')
sys.exit(0)
for filename in sys.argv[1:]:
try:
sum = md5sum(filename)
except Exception, e:
sys.stderr.write('%s\n' % e)
print '%s %s' % (sum, filename)
sys.exit(0)
shasum.py:
import struct, string, sha
def shasum(filename):
s = sha.new()
fd = open(filename, 'r')
s.update(fd.read())
return string.join(map(lambda x: '%02x' % x, struct.unpack('B' * 20, s.digest())), '')
if __name__ == '__main__':
import sys
if len(sys.argv) < 2:
print '\n' + shasum('/dev/fd/0')
sys.exit(0)
for filename in sys.argv[1:]:
try:
sum = shasum(filename)
except Exception, e:
sys.stderr.write('%s\n' % e)
print '%s %s' % (sum, filename)
sys.exit(0)
check_debian_package_sums.py:
import glob, sys, string, md5sum # (md5sum from above)
if len(sys.argv) < 2:
sys.stderr.write('Usage: %s <logfile>\n' % sys.argv[0])
sys.exit(-1)
try:
log = open(sys.argv[1], 'w+')
except Exception, e:
sys.stderr.write('%s\n' % e)
sys.exit(-1)
for sums_file in glob.glob('/var/lib/dpkg/info/*.md5sums'):
for line in open(sums_file, 'r').readlines():
expected_sum, filename = string.split(line)
filename = '/' + filename
try:
real_sum = md5sum.md5sum(filename)
except Exception, e:
log.write('%s\n' % e)
continue
if real_sum != expected_sum:
log.write('%s did not match expected sum\n' % filename)
Now, I didn't do an exact copy/paste since that code is on a different computer than the one I'm posting this from. I hope I didn't mess up any of the typing, but if someone goes to all the trouble to run this and does get an error, please, let me know so everyone doesn't get the same error.
I've got a few other tools I've whipped up; a "find" program that finds based on regular expressions rather than shell globs, and a small, robust, spoofing identd server. If anyone's interested, I can post those, but it's tough to roll back and forth between these two not-networked computers, so only if someone asks http://www.linuxnewbie.org/ubb/smile.gif
Anyone else have snippets? As opposed to some other languages, reading other people's code in python is actually enjoyable http://www.linuxnewbie.org/ubb/smile.gif
Jeremy