#!/usr/bin/env python # vim: tabstop=4 softtabstop=4 shiftwidth=4 textwidth=80 smarttab expandtab """ * Copyright (C) 2012 Sangoma Technologies Corp. * All Rights Reserved. * * Author(s) * Travis Semczyszyn * * This is a simple script to scan for USB mass storage devices * and find the serial number of a newly inserted device * """ from os import listdir from os import path import sys import signal import re def sig_handler(signal, frame): print "Exiting..." sys.exit(0) signal.signal(signal.SIGINT, sig_handler) print "#################################################################################" print "Sangoma's USB Serial Scanner (you can press CTRL + C anytime to quit)" print "#################################################################################\n" if (path.exists('/proc/scsi/usb-storage')): list1 = listdir('/proc/scsi/usb-storage') else: list1= [] while (1): sys.stdout.write("Please insert the USB key you want to use for the Sangoma license and press any key ...") sys.stdout.flush() sys.stdin.read(1) try: list2 = listdir('/proc/scsi/usb-storage') except OSError: print "No new USB key detected (failed to list /proc/scsi/usb-storage) ...\n" continue listdiff = set(list2) - set(list1) newlist = list(listdiff) if not newlist: print "No new USB key detected ...\n" continue serialPath = '/proc/scsi/usb-storage/' + newlist[0] try: procfile = open(serialPath, 'r') except OSError: print "No new USB key detected (Failed to open: %s) ...\n" % str(serialPath) continue matchtext = procfile.read() serialNumber = re.search("^Serial\s*Number:\s*(.+)?$", matchtext, re.MULTILINE) print "\n" print "###########################################" print "Your USB Serial number is " + serialNumber.group(1) print "###########################################" break