This repository has been archived by the owner on Sep 20, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 36
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #82 from miri64/scripts/enh/black
sniffer / spectrum-scanner: reformat using black
- Loading branch information
Showing
2 changed files
with
84 additions
and
54 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
#!/usr/bin/env python3 | ||
# -*- coding: utf-8 -*- | ||
''' | ||
""" | ||
(C) 2012, Mariano Alvira <[email protected]> | ||
(C) 2014, Oliver Hahm <[email protected]> | ||
(C) 2015, Hauke Petersen <[email protected]> | ||
|
@@ -30,7 +30,7 @@ | |
LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | ||
OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | ||
SUCH DAMAGE. | ||
''' | ||
""" | ||
|
||
from __future__ import print_function | ||
import argparse | ||
|
@@ -42,70 +42,69 @@ | |
from serial import Serial | ||
|
||
# PCAP setup | ||
MAGIC = 0xa1b2c3d4 | ||
MAGIC = 0xA1B2C3D4 | ||
MAJOR = 2 | ||
MINOR = 4 | ||
ZONE = 0 | ||
SIG = 0 | ||
SNAPLEN = 0xffff | ||
NETWORK = 230 # 802.15.4 no FCS | ||
SNAPLEN = 0xFFFF | ||
NETWORK = 230 # 802.15.4 no FCS | ||
|
||
DEFAULT_BAUDRATE = 115200 | ||
|
||
|
||
def configure_interface(port, channel): | ||
line = "" | ||
iface = 0 | ||
port.write('ifconfig\n'.encode()) | ||
port.write("ifconfig\n".encode()) | ||
while True: | ||
line = port.readline() | ||
if line == b'': | ||
print("Application has no network interface defined", | ||
file=sys.stderr) | ||
if line == b"": | ||
print("Application has no network interface defined", file=sys.stderr) | ||
sys.exit(2) | ||
match = re.search(r'^Iface +(\d+)', line.decode(errors="ignore")) | ||
match = re.search(r"^Iface +(\d+)", line.decode(errors="ignore")) | ||
if match is not None: | ||
iface = int(match.group(1)) | ||
break | ||
|
||
# set channel, raw mode, and promiscuous mode | ||
print('ifconfig %d set chan %d' % (iface, channel), file=sys.stderr) | ||
print('ifconfig %d raw' % iface, file=sys.stderr) | ||
print('ifconfig %d promisc' % iface, file=sys.stderr) | ||
port.write(('ifconfig %d set chan %d\n' % (iface, channel)).encode()) | ||
port.write(('ifconfig %d raw\n' % iface).encode()) | ||
port.write(('ifconfig %d promisc\n' % iface).encode()) | ||
print("ifconfig %d set chan %d" % (iface, channel), file=sys.stderr) | ||
print("ifconfig %d raw" % iface, file=sys.stderr) | ||
print("ifconfig %d promisc" % iface, file=sys.stderr) | ||
port.write(("ifconfig %d set chan %d\n" % (iface, channel)).encode()) | ||
port.write(("ifconfig %d raw\n" % iface).encode()) | ||
port.write(("ifconfig %d promisc\n" % iface).encode()) | ||
|
||
|
||
def generate_pcap(port, out): | ||
# count incoming packets | ||
count = 0 | ||
# output overall PCAP header | ||
out.write(pack('<LHHLLLL', MAGIC, MAJOR, MINOR, ZONE, SIG, SNAPLEN, | ||
NETWORK)) | ||
out.write(pack("<LHHLLLL", MAGIC, MAJOR, MINOR, ZONE, SIG, SNAPLEN, NETWORK)) | ||
sys.stderr.write("RX: %i\r" % count) | ||
while True: | ||
line = port.readline().rstrip() | ||
|
||
pkt_header = re.match(r">? *rftest-rx --- len (\w+).*", | ||
line.decode(errors="ignore")) | ||
pkt_header = re.match( | ||
r">? *rftest-rx --- len (\w+).*", line.decode(errors="ignore") | ||
) | ||
if pkt_header: | ||
now = time() | ||
sec = int(now) | ||
usec = int((now - sec) * 1000000) | ||
length = int(pkt_header.group(1), 16) | ||
out.write(pack('<LLLL', sec, usec, length, length)) | ||
out.write(pack("<LLLL", sec, usec, length, length)) | ||
out.flush() | ||
count += 1 | ||
sys.stderr.write("RX: %i\r" % count) | ||
continue | ||
|
||
pkt_data = re.match(r"(\w\w )+", line.decode(errors="ignore")) | ||
if pkt_data: | ||
for part in line.decode(errors="ignore").split(' '): | ||
for part in line.decode(errors="ignore").split(" "): | ||
byte = re.match(r"(\w\w)", part) | ||
if byte: | ||
out.write(pack('<B', int(byte.group(1), 16))) | ||
out.write(pack("<B", int(byte.group(1), 16))) | ||
out.flush() | ||
|
||
|
||
|
@@ -114,15 +113,14 @@ def connect(args): | |
if args.conn.startswith("/dev/tty") or args.conn.startswith("COM"): | ||
# open serial port | ||
try: | ||
conn = Serial(args.conn, args.baudrate, dsrdtr=0, rtscts=0, | ||
timeout=1) | ||
conn = Serial(args.conn, args.baudrate, dsrdtr=0, rtscts=0, timeout=1) | ||
except IOError: | ||
print("error opening serial port %s" % args.conn, file=sys.stderr) | ||
sys.exit(2) | ||
else: | ||
try: | ||
port = args.conn.split(":")[-1] | ||
host = args.conn[:-(len(port)+1)] | ||
host = args.conn[: -(len(port) + 1)] | ||
port = int(port) | ||
except (IndexError, ValueError): | ||
print("Can't parse host:port pair %s" % args.conn, file=sys.stderr) | ||
|
@@ -143,17 +141,29 @@ def main(): | |
else: | ||
default_outfile = sys.stdout | ||
p = argparse.ArgumentParser() | ||
p.add_argument("-b", "--baudrate", type=int, default=DEFAULT_BAUDRATE, | ||
help="Baudrate of the serial port (only evaluated " | ||
"for non TCP-terminal, default: %d)" % | ||
DEFAULT_BAUDRATE) | ||
p.add_argument("conn", metavar="tty/host:port", type=str, | ||
help="Serial port or TCP (host, port) tuple to " | ||
"terminal with sniffer application") | ||
p.add_argument( | ||
"-b", | ||
"--baudrate", | ||
type=int, | ||
default=DEFAULT_BAUDRATE, | ||
help="Baudrate of the serial port (only evaluated " | ||
"for non TCP-terminal, default: %d)" % DEFAULT_BAUDRATE, | ||
) | ||
p.add_argument( | ||
"conn", | ||
metavar="tty/host:port", | ||
type=str, | ||
help="Serial port or TCP (host, port) tuple to " | ||
"terminal with sniffer application", | ||
) | ||
p.add_argument("channel", type=int, help="Channel to sniff on") | ||
p.add_argument("outfile", type=argparse.FileType("w+b"), | ||
default=default_outfile, nargs="?", | ||
help="PCAP file to output to (default: stdout)") | ||
p.add_argument( | ||
"outfile", | ||
type=argparse.FileType("w+b"), | ||
default=default_outfile, | ||
nargs="?", | ||
help="PCAP file to output to (default: stdout)", | ||
) | ||
args = p.parse_args() | ||
|
||
conn = connect(args) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters