-
Notifications
You must be signed in to change notification settings - Fork 0
/
nwifictl
executable file
·163 lines (134 loc) · 3.56 KB
/
nwifictl
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
#!/bin/bash
RUND="/run"
ALTD="/var/run"
which dhclient >/dev/null
# test for the appropriate DHCP client
if [ "$?" -eq 0 ]; then
CLIENT=dhclience
elif [ "$?" -eq 1 ]; then
CLIENT=dhcpcd
else
echo "[!] Unknown DHCP client. Exiting..."
exit 1
fi
function help() {
echo "A WiFi Connection Tool"
echo -e "\nUsage: nwifictl -e <essid> -i <iface> [options]"
echo "[options]:"
echo -e "\t-e|-essid=[ESSID]\tSpecify the SSID to connect to "
echo -e "\t-p|-psk=[PSK]\t\tSpecify the password"
echo -e "\t-i|-iface=[IFACE]\tSpecify the interface to use"
echo -e "\t-s|-spoof=[ADDR]\tSpoof interface MAC address"
echo -e "\t-c|-config=[CONFIG]\tSpecify a custom wpa_supplicant config"
echo -e "\t-help\t\t\tPrints this help text"
exit 0
}
function connection_test() {
# get local IP address
local_ip=`ip addr |grep 'state UP' -A2 |tail -n1 |awk '{ print $2 }' |cut -d/ -f1`
echo "Your local IP address is: $local_ip"
echo "[*] Testing internet connection, this may take a few moments."
# see if we can reach the interwebz
ping -c1 -q 208.67.222.222 &>/dev/null
if [ $? -eq 0 ]; then
echo "[+] Successfully connected to $1"
else
echo "[-] Failed to connect to $1"
exit 2
fi
}
function connect() {
# stop NetworkManager
service NetworkManager stop 2>/dev/null
# clean up old leases
killall $CLIENT 2>/dev/null && killall wpa_supplicant 2>/dev/null
rm $RUND/$CLIENT.pid 2>/dev/null && rm $ALTD/$CLIENT.pid
rm $RUND/wpa_supplicant/$IFACE 2>/dev/null && rm $ALTD/wpa_supplicant/$IFACE 2>/dev/null
if [ -z "$2" ]; then
# if it's an open network
# simple connect with iwconfig
ip link set $4 down
iwconfig $4 essid $1
ip link set $4 up
else
# if it's a password-protected network
# create and initialize config if it doesn't exist
if [ ! -e "$3" ]; then
touch $3
echo -e "ctrl_interface=$RUND/wpa_supplicant\nctrl_interface_group=root\n" > $3
fi
# add network to the configuration file and initialize
wpa_passphrase $1 $2 >> $3
wpa_supplicant -B -Dnl80211 -i $4 -c $3 2>/dev/null
fi
# get an IP address from the AP
$CLIENT $4 2>/dev/null
echo "Waiting to finish connecting..."
# wait for it to fully connect
sleep 10
# cheap workaround (will fix soon)
echo nameserver 208.67.222.222 >> /etc/resolv.conf
}
# begin command-line parsing
while [ "$1" != "" ]; do
param=`echo $1|awk -F= '{print $1}'`
value=`echo $1|awk -F= '{print $2}'`
# help text
if [ $1 == "-help" ]; then
help
fi
case $param in
-e|-essid)
SSID=$value
;;
-p|-psk)
PASS="$value"
;;
-i|-iface)
IFACE=$value
;;
-s|-spoof)
MAC=$value
;;
-c|-config)
CONFIG=$value
;;
*)
echo "[!] $param is an incorrect option. See -help for more info."
exit 1
;;
esac
shift
done
# require -i option
if [ -z "$IFACE" ]; then
echo "[!] You must specify an interface to use."
exit 1
fi
# require -e option
if [ -z "$SSID" ]; then
echo "[!] You must specify a network to connect to."
exit 1
fi
# set default config if none specified
if [ -z "$CONFIG" ]; then
CONFIG="/etc/wpa_supplicant.conf"
fi
# spoof mac if -s option is specified
if [ -n "$MAC" ]; then
ip link set $IFACE down
ip link set dev $IFACE address $MAC
ip link set $IFACE up
fi
# do stuff depending on auth or no auth
if [ -n "$PASS" ]; then
echo "[*] Connecting to $SSID, this may take a few moments... "
# if there is a password specified
connect $SSID $PASS $CONFIG $IFACE
connection_test $SSID
else
echo "[*] Connecting to $SSID, this may take a few moments... "
# if it's an open network
connect $SSID "" $CONFIG $IFACE
connection_test $SSID
fi