forked from IvanIFChen/Passiotic
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathchannel_hopping.py
41 lines (32 loc) · 1.2 KB
/
channel_hopping.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
import threading
import subprocess
import time
class ChannelHopper(threading.Thread):
"""
A thread worker that hops between an array of channels in order every n seconds.
"""
def __init__(self, channels, interval, interface):
"""
channels -- An array of integers representing the channels to hop between.
frequency -- The number of seconds between each hop.
interface -- The wireless interface to switch.
"""
super().__init__()
self.channels = channels
self.interval = interval
self.stop = threading.Event()
self.interface = interface
def run(self):
chan_index = 0
while not self.stop.isSet():
chan_integer = self.channels[chan_index]
chan_string = str(
chan_integer) if chan_integer > 9 else '0' + str(chan_integer)
subprocess.run(
['iwconfig', self.interface, 'channel', chan_string])
print('Current channel is {}'.format(chan_string))
chan_index = (chan_index + 1) % len(self.channels)
time.sleep(self.interval)
def join(self, timeout=None):
self.stop.set()
super().join(timeout)