-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathchanhop.sh
153 lines (138 loc) · 3.09 KB
/
chanhop.sh
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
#!/bin/bash
# Channel hopping shell script
# GPLv2
# Portions of code graciously taken from Bill Stearns defragfile
# http://www.stearns.org/defragfile/
#
# jwright@hasborg.com
# Defaults
BANDS="IEEE80211B"
DWELLTIME=".25"
CH1="1 2 3 4 5 6 7 8 9 10 11"
CH2="1 6 11"
CH3="1 7 13 2 8 3 9 4 10 5 11 6 12"
CH4="1 6 11 2 7 3 8 4 9 5 10"
CHANB="1 6 11 2 7 3 8 4 9 5 10"
CHANBJP="1 13 6 11 2 12 7 3 8 14 4 9 5 10"
CHANBINTL="1 13 6 11 2 12 7 3 8 4 9 5 10"
CHANA="36 40 44 48 52 56 60 149 153 157 161"
requireutil () {
while [ -n "$1" ]; do
if ! type -path "$1" >/dev/null 2>/dev/null ; then
echo Missing utility "$1". Please install it. >&2
return 1 #False, app is not available.
fi
shift
done
return 0 #True, app is there.
} #End of requireutil
fail () {
while [ -n "$1" ]; do
echo "$1" >&2
shift
done
echo "Exiting." >&2
echo
exit 1
} #End of fail
usage () {
fail 'chanhop.sh: Usage:' \
"$0 [-i|--interface] [-b|--band] [-d|--dwelltime]" \
'-i or --interface specifies the interface name to hop on [mandatory]' \
'-b or --band specifies the bands to use for channel hopping, one of' \
' IEEE80211B Channels 1-11 [default]' \
' IEEE80211BINTL Channels 1-13' \
' IEEE80211BJP Channels 1-14' \
' IEEE80211A Channels 36-161' \
' Use multiple -b arguments for multiple channels' \
"-d or --dwelltime amount of time to spend on each channel [default $DWELLTIME seconds]" \
' ' \
"e.x. $0 -i ath0 -b IEEE80211BINTL -b IEEE80211A -d .10"
} #End of usage
# main
requireutil sleep whoami iwconfig || exit 1
if [ `whoami` != root ]; then
echo "You must run this script as root, or under \"sudo\"."
usage
fail
fi
while [ -n "$1" ]; do
case "$1" in
-i|--interface)
INTERFACE="$2"
shift
;;
-b|--band)
ARG_BANDS="$2 $ARG_BANDS"
shift
;;
-d|--dwelltime)
ARG_DWELLTIME="$2"
shift
;;
*)
echo "Unsupported argument \"$1\"."
usage
fail
;;
esac
shift
done
if [ -z "$INTERFACE" ]; then
usage;
exit 1
fi
# Test the sleep duration value
if [ ! -z "$ARG_DWELLTIME" ] ; then
sleep $ARG_DWELLTIME 2>/dev/null
if [ $? -ne 0 ] ; then
fail "Invalid dwell time specified: \"$ARG_DWELLTIME\"."
fi
DWELLTIME=$ARG_DWELLTIME
fi
# If the user specified the -b argument, we use that instead of default
if [ ! -z "$ARG_BANDS" ] ; then
BANDS=$ARG_BANDS
fi
# Expand specified bands into a list of channels
for BAND in $BANDS ; do
case "$BAND" in
CH1)
CHANNELS="$CH1"
;;
CH2)
CHANNELS="$CH2"
;;
CH3)
CHANNELS="$CH3"
;;
CH4)
CHANNELS="$CH4"
;;
IEEE80211B|IEEE80211b|ieee80211b)
CHANNELS="$CHANNELS $CHANB"
;;
IEEE80211BJP|IEEE80211bjp|ieee80211bjp)
CHANNELS="$CHANNELS $CHANBJP"
;;
IEEE80211BINTL|IEEE80211bintl|ieee80211bintl)
CHANNELS="$CHANNELS $CHANBINTL"
;;
IEEE80211A|IEEE80211a|ieee80211a)
CHANNELS="$CHANNELS $CHANA"
;;
*)
fail "Unsupported band specified \"$BAND\"."
;;
esac
done
echo "Starting channel hopping, press CTRL/C to exit."
while true; do
for CHANNEL in $CHANNELS ; do
iwconfig $INTERFACE channel $CHANNEL
if [ $? -ne 0 ] ; then
fail "iwconfig returned an error when setting channel $CHANNEL"
fi
sleep $DWELLTIME
done
done