-
Notifications
You must be signed in to change notification settings - Fork 0
/
usb_rules.py
136 lines (121 loc) · 5.61 KB
/
usb_rules.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
#!/usr/bin/env python
"""
File: usb_rules.py
Date: 2012-04-15
A script to quickly scan for new USB drives and add a custom symlink to each one using udev rules.
"""
import re, subprocess, fileinput
from optparse import OptionParser
rules_name = '/etc/udev/rules.d/65-usb.rules'
class usbRules:
def __init__(self):
try:
self.rules_file = open(rules_name,'r')
except IOError:
print "I could not find your file! Ahhhhhh! I'll just create one for you ;)"
self.rules_file = open(rules_name, 'a+')
self.count = ""
self.symlink_name = ''
for s in self.rules_file.readline().strip():
if s.isdigit():
self.count += s
self.content = ''.join(self.rules_file.readlines())
if self.content == '':
self.count = 0
def add(self, usb_serial):
match = re.findall('\\b'+usb_serial+'\\b',self.content)
if match:
print "You've already have this serial number!"
return
command = str("\nSUBSYSTEMS==\"usb\", KERNEL==\"sd?1\", ATTRS{serial}==\""+usb_serial+"\", SYMLINK=\""+self.symlink_name+""+str(self.count)+"part1\"\"\n")
command2 = str("SUBSYSTEMS==\"usb\", KERNEL==\"sd?2\", ATTRS{serial}==\""+usb_serial+"\", SYMLINK=\""+self.symlink_name+""+str(self.count)+"part2\"")
commands = [command,command2]
self.content += ''.join(commands)
self.count = int(self.count)+1
self.rules_file.close()
self.rules_file = open(rules_name, 'w')
self.rules_file.writelines(self.content)
self.rules_file.close()
print "successfully added " + usb_serial + "!"
def delete(self, usb_serial):
if self.content.find(usb_serial) != -1:
input = raw_input("Found it! You sure you wanna delete? (Y/N)")
while True:
if input not in ['y','Y','n','N']:
print "Please enter a valid input"
continue
if input == 'y' or input == 'Y':
self.count = int(self.count)-1
self.contents = ''.split(self.content)
for item in self.contents:
if usb_serial in item:
self.contents.remove(item)
self.contents = ('#'+str(self.count)) + '\n' + ''.join(self.contents)
self.rules_file.close()
self.rules_file = open(rules_name, 'w')
self.rules_file.writelines(self.contents)
self.rules_file.close()
if input == 'n' or input =='N':
return
else:
"Serial number not found!"
def scan(self):
# Grab output from command "blkid" and split it up by device
blkid = subprocess.Popen("sudo blkid -t TYPE=\"vfat\"", shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE).communicate()[0]
blkid = blkid.split("\n")
blkid = [item.split() for item in blkid]
# Last element of blkid is empty or unnecessary, so strip it out
blkid = blkid[:len(blkid)-1]
partitions = []
for element in blkid:
partitions.append({})
for info in element:
if '/dev/' in info:
partitions[len(partitions)-1]= info[:len(info)-1]
for item in partitions:
print item
get_serial = subprocess.Popen("udevadm info --name="+str(item)+" --query=all | grep SERIAL_SHORT | cut -d= -f2", shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE).communicate()[0]
get_serial = get_serial.strip()
print get_serial
if self.content.find(get_serial) != -1:
print "You've already have this serial number!"
else:
header = '#' + str(self.count) + '\n'
command = str("\nSUBSYSTEMS==\"usb\", KERNEL==\"sd?1\", ATTRS{serial}==\""+get_serial+"\", SYMLINK=\""+self.symlink_name+""+str(self.count)+"part1\"\n")
command2 = str("SUBSYSTEMS==\"usb\", KERNEL==\"sd?2\", ATTRS{serial}==\""+get_serial+"\", SYMLINK=\""+self.symlink_name+""+str(self.count)+"part2\"")
commands = [header,command,command2]
self.content += ''.join(commands)
print "Adding USB"+str(self.count)
self.count = int(self.count)+1
self.content = ('#'+str(self.count)) + '\n' + self.content
self.rules_file.close()
self.rules_file = open(rules_name, 'w')
self.rules_file.writelines(self.content)
self.rules_file.close()
if __name__=="__main__":
main = usbRules()
parser = OptionParser(
usage = "usage: %prog [options]",
parser.add_option("-a",
"--add",
dest = "add",
default = '',
help = "Adds a new USB serial number.")
parser.add_option("-d",
"--delete",
dest = "delete",
default = '',
help = "Removes a serial number.")
parser.add_option("-s",
"--scan",
action = "store_true",
dest = "scan",
default = False,
help = "Scans for new drives in the hub and adds them.")
(options, args) = parser.parse_args()
if options.add != '':
main.add(options.add)
if options.delete != '':
main.delete(options.delete)
if options.scan == True:
main.scan()