-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpoprow_setup_interface.py
More file actions
executable file
·181 lines (158 loc) · 7.31 KB
/
poprow_setup_interface.py
File metadata and controls
executable file
·181 lines (158 loc) · 7.31 KB
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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
#!/usr/bin/env python
#
# Copyright (C) 2017 Michele Segata <msegata@disi.unitn.it>
# Copyright (C) 2017 Nicolo' Facchi <nicolo.facchi@unitn.it>
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
# 02110-1301, USA.
#
import argparse
import subprocess
from pyric import pyw
import pyric.utils.channels as channels
def run_command(command):
'''
Method to start the shell commands
and get the output as iterater object
'''
sp = subprocess.Popen(command, stdout=subprocess.PIPE,
stderr=subprocess.PIPE, shell=True)
out, err = sp.communicate()
if err:
raise Exception('An error occurred in Dot80211Linux: %s' % err)
return [sp.returncode, out.decode('utf-8'), err.decode('utf-8')]
if __name__ == '__main__':
parser = argparse.ArgumentParser()
parser.add_argument('--intcap', dest='intcap', choices=['HT', 'VHT'],
type=str, help='Interface capabilities',
required=True)
# parser.add_argument('--band', dest='band', choices=['5GHz', '2GHz'],
# type=str, help='Band to use: 2.4GHz or 5GHz',
# required=True)
allowed_channels_24ghz = range(1, 14)
allowed_channels_5ghz = [36, 40, 44, 48, 52, 56, 60, 149, 153, 157, 161]
parser.add_argument('--chan', dest='chan',
choices=allowed_channels_24ghz + allowed_channels_5ghz,
type=int, help='Channel',
required=True)
# allowed_freqs_24ghz = [2412, 2417, 2422, 2427, 2432, 2437, 2442, 2447,
# 2452, 2457, 2462, 2467, 2472]
# allowed_freqs_5ghz = [5180, 5200, 5220]
# parser.add_argument("--freq", dest="freq",
# choices=allowed_freqs_24ghz + allowed_freqs_5ghz,
# type=int, help="Channel center frequency",
# required=True)
# parser.add_argument("--mcs", dest="mcs", help="TX MCS", required=True)
parser.add_argument('--legacyrate', dest='legacyrate',
choices=[6, 9, 12, 18, 24, 36, 48, 54], type=int,
help='Transmission legacy rate', required=True)
parser.add_argument('--txpower', dest='txpower', type=int,
choices=range(0, 3001), metavar='[1-3001]',
help='TX power in millibel-milliwatts (mBm) '
'(<power in mBm> = 100 * <power in dBm>)',
required=True)
parser.add_argument('--inet', dest='inet',
help='IPv4 address (x.y.z.t/nm)',
required=True)
# parser.add_argument("--nmask", dest="nmask", help="IPv4 netmask",
# required=True)
# parser.add_argument("--bcast", dest="bcast", help="IPv4 broadcast",
# required=True)
parser.add_argument('--ibssid', dest='ibssid', help='Hadoc SSID',
required=True)
parser.add_argument('--ibssiname', dest='ibssiname',
help='Ad hoc interface name',
required=True)
parser.add_argument('--moniname', dest='moniname',
help='Monitor interface name',
required=True)
parser.add_argument('--bint', dest='bint', type=int, default=100,
help='Beacon interval in TUs', required=False)
args = parser.parse_args()
# Delete all Wi-Fi interfaces
wifi_int = pyw.winterfaces()
for wifi_int_name in wifi_int:
pyw.devdel(pyw.getcard(wifi_int_name))
band = "2GHz"
if args.chan > 14:
band = "5GHz"
# Look for a candidate PHY (currently based on frequency and capabilities
# (HT or VHT) support
phys = pyw.phylist()
selected_phy = None
for phy in phys:
phy_info = pyw.phyinfo(pyw.Card(phy[0], None, 0))
if phy_info['bands'].get(band) and phy_info.get('modes'):
if bool(phy_info['bands'][band][args.intcap]):
if 'ibss' in phy_info['modes']:
selected_phy = phy
# print(selected_phy)
break
# Add and configure mesh and monitor interfaces
if selected_phy is not None:
phy_info = pyw.phyinfo(pyw.Card(selected_phy[0], None, 0))
# print(phy_info)
# Create ibss point interface
ibss_cmd = 'iw phy ' + selected_phy[1] +\
' interface add ' + args.ibssiname +\
' type ibss'
print(ibss_cmd)
[rcode, sout, serr] = run_command(ibss_cmd)
# Set ibss interface IP address
ibss_ip_cmd = 'ip addr add ' + args.inet +\
' dev ' + args.ibssiname
print(ibss_ip_cmd)
[rcode, sout, serr] = run_command(ibss_ip_cmd)
# Add monitor interface
ibss_mon_cmd = 'iw dev ' + args.ibssiname + ' interface add ' +\
args.moniname + ' type monitor'
print(ibss_mon_cmd)
[rcode, sout, serr] = run_command(ibss_mon_cmd)
# Bring interfaces up
ibss_up_cmd = 'ip link set dev ' + args.ibssiname + ' up'
print(ibss_up_cmd)
[rcode, sout, serr] = run_command(ibss_up_cmd)
ibss_mon_up_cmd = 'ip link set dev ' + args.moniname + ' up'
print(ibss_mon_up_cmd)
[rcode, sout, serr] = run_command(ibss_mon_up_cmd)
# ibss_chan_cmd = 'iw dev ' + args.ibssiname + ' set channel ' +\
# str(args.chan)
# print(ibss_chan_cmd)
# [rcode, sout, serr] = run_command(ibss_chan_cmd)
ibss_join_cmd = 'iw dev ' + args.ibssiname + ' ibss join ' +\
args.ibssid + ' ' + str(channels.ch2rf(args.chan)) +\
' fixed-freq 00:11:22:33:44:55 beacon-interval ' +\
str(args.bint)
print(ibss_join_cmd)
[rcode, sout, serr] = run_command(ibss_join_cmd)
# Set legacy rate
legacy_str = 'legacy-'
if band == '2GHz':
legacy_str += '2.4'
else:
legacy_str += '5'
ibss_rate_cmd = 'iw dev ' + args.ibssiname + ' set bitrates ' +\
legacy_str + ' ' + str(args.legacyrate)
print(ibss_rate_cmd)
[rcode, sout, serr] = run_command(ibss_rate_cmd)
ibss_ps_off_cmd = 'iw dev ' + args.ibssiname + ' set power_safe off'
print(ibss_ps_off_cmd)
[rcode, sout, serr] = run_command(ibss_ps_off_cmd)
ibss_tx_pwr_cmd = 'iw dev ' + args.ibssiname +\
' set txpower fixed ' + str(args.txpower)
print(ibss_tx_pwr_cmd)
[rcode, sout, serr] = run_command(ibss_tx_pwr_cmd)
[rcode, sout, serr] = run_command('iw dev')
print(sout)