-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfind-nics.py
executable file
·61 lines (53 loc) · 2.21 KB
/
find-nics.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
#!/usr/bin/env python
'''
Takes the pci file (from hdt dump) as an argument, parses it into a list of
dictionaries, then searches that list for dictionaries with network devices, and looks in
and returns some ethernet (wired) and wlan (wireless) device models and manufacturers.
Takes two arguments: an '-e' or a '-w' and the pci file. -e will return the wired
ethernet info, -w will return the wireless info.
'''
import re
import sys
# Define a function that returns a dictionary
def new_dict(): return {}
def main():
nic = ''
dicts = []
index = 0
count = 0
f = open(sys.argv[2])
for line in f:
line = re.sub('"','',line) # strip out quotes; strip, below, only removes one instance of quote
line = line.strip('\n, ') # strip off newline, commas, whitespace
if re.match('^{', line):
dicts.append(new_dict())
elif re.match('^}{', line):
dicts.append(new_dict())
index += 1
elif re.match('^}$', line):
pass
else:
key,value = line.split(':',1) # don't use split because some values contain colons
key = key.strip() # get rid of extra white space
value = value.strip() # get rid of extra white space
dicts[index][key] = value.strip()
#print "index is", index, "key is", key, "and value is", value
for d in dicts:
if 'pci_device.class_id' in d.keys():
if d['pci_device.class_id'] == '02.80.00' and sys.argv[1] == '-w':
count += 1
if count >= 3:
nic = nic + ' ' + str(count) + '-'
elif count == 2:
nic = '1-' + nic + ' 2-'
nic += d['pci_device.vendor_name'] + ' - ' + d['pci_device.product_name'] + ' '
if d['pci_device.class_id'] == '02.00.00' and sys.argv[1] == '-e':
count += 1
if count >= 3:
nic = nic + ' NIC' + str(count) + '-'
elif count == 2:
nic = 'NIC1-' + nic + ' NIC2-'
nic += d['pci_device.vendor_name'] + ' - ' + d['pci_device.product_name'] + ' '
print nic.replace('/','-')
if __name__ == '__main__':
main()