-
Notifications
You must be signed in to change notification settings - Fork 0
/
create-devices-txt.py
executable file
·139 lines (119 loc) · 5.1 KB
/
create-devices-txt.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
#!/usr/bin/env python3
import argparse
import osmosdr
import re
def argument_parser():
parser = argparse.ArgumentParser(description="A script to detect SDR devices and generate a devices.txt file for challengectl.")
parser.add_argument("-f", "--file", default="devices.txt", help="Output file")
parser.add_argument("-p", "--printonly", action="store_true", help="Print results to screen, and do not write to file.")
parser.add_argument("-b", "--disablebiastee", action="store_true", help="Disable bladeRF biastee (enabled by default).")
parser.add_argument("-v", "--verbose", action="store_true")
return parser
def main(options=None):
if options is None:
options = argument_parser().parse_args()
args = options
verbose = args.verbose
printonly = args.printonly
outputfile = args.file
disablebiastee = args.disablebiastee
# This list will hold the GrOsmoSDR device strings
sdrstrings = []
# Find devices through osmosdr
devices = osmosdr.device.find()
# Variable to track index of hackrf devices
hackrfindex = 0;
for device in devices:
# Get strings for each device identified by osmosdr
devicestring = device.to_string()
# Split string so we can find the attributes that we want
attributes = devicestring.split(',')
# Find UHD / USRP devices
if("uhd" in attributes):
# uhd_find_devices returns non-USRP devices from soapy
# Ignore any soapy entries
if("type=soapy" in attributes):
continue
uhdtype = ""
uhdserial = ""
for item in attributes:
# USRP type
if(item.startswith("type=")):
uhdtype = item
# USRP serial number
elif(item.startswith("serial=")):
uhdserial = item
# Skip device if either type or serial are empty
if(uhdtype != "" and uhdserial != ""):
# Generate UHD device string
# Example: "uhd,type=b200,serial=<serialnumber>"
uhdstring = "uhd,{},{}".format(uhdtype, uhdserial)
if(verbose):
print(uhdstring)
# Add UHD device string to the list
sdrstrings.append(uhdstring)
else:
print("WARNING: UHD device skipped")
print("uhdtype: {}".format(uhdtype))
print("uhdserial: {}".format(uhdserial))
# Find bladeRF devices
elif("driver=bladerf" in attributes):
for item in attributes:
bladerfstring = "bladerf=<serial>,biastee=1"
if(item.startswith("serial=")):
# Remove 'serial=' bladeRF serial number
bladerfserial = item[7:]
# Skip device if serial is empty
if(bladerfserial != ""):
bladerfbiastee = "biastee=1"
if(disablebiastee == True):
bladerfbiastee = "biastee=0"
# Generate bladeRF device string
# Example: "bladerf=<serialnumber>,biastee=1"
bladerfstring = "bladerf={},{}".format(bladerfserial, bladerfbiastee)
if(verbose):
print(bladerfstring)
sdrstrings.append(bladerfstring)
else:
print("WARNING: bladeRF device skipped")
print("bladerfserial: {}".format(bladerfserial))
# Find HackRF devices
elif("driver=hackrf" in attributes):
for item in attributes:
if(item.startswith("serial=")):
# Remove 'serial=' and leading zeros from HackRF serial number
hackrfserial = re.sub("^serial=0+(?!$)", "", item)
elif(item.startswith("label=")):
label = item
# Skip device if serial is empty
if(hackrfserial != ""):
# Generate hackRF device string
# Example: "hackrf=<index>,label='HackRF One #X <serial>'"
hackrfstring = "hackrf={},{}".format(hackrfindex,label)
# Increment hackrf index
hackrfindex += 1
if(verbose):
print(hackrfstring)
sdrstrings.append(hackrfstring)
else:
print("WARNING: HackRF device skipped")
print("hackrfserial: {}".format(hackrfserial))
# Generate strings for challengectl devices.txt file
outputstrings = []
index = 0
for sdr in sdrstrings:
sdrstring = "{},\"{}\"".format(index, sdr)
outputstrings.append(sdrstring)
index += 1
# Print devices.txt strings to screen, and write to outputfile if printonly is not set.
if(printonly == True):
for string in outputstrings:
print(string)
else:
with open(outputfile, 'w') as fh:
for string in outputstrings:
print(string)
fh.write(string)
fh.write('\n')
if __name__ == '__main__':
main()