-
Notifications
You must be signed in to change notification settings - Fork 0
/
wpa_connect
executable file
·126 lines (104 loc) · 2.77 KB
/
wpa_connect
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
#!/bin/bash
# check root
if [ $UID -ne 0 ]; then
echo "don't try to run this without superuser powers"
exit 1
fi
# set default vars
scanaps=1
JOL=0
# set env (need to figure out a better way for this)
home='/home/jinn'
# set default interface
IF="wlan0"
# switch off enoying getopts errmsgs
export OPTERR=0
# scan for options
while getopts "ni:" opt; do
case "$opt" in
n)
echo "I won't scan for the AP"
scanaps=0
;;
i)
IF=$OPTARG
echo "selected interface is $IF"
;;
esac
done
# set $ap
while [ $# -ge 1 ]; do
if [ -n "$(echo "$1k" | grep '-')" ]; then # i don't know why, but echo acts different on "-n" alone, but with the k (or any other letter/word) it works...
shift
else
ap="$BASH_ARGV"
break
fi
done
# colors
RED="\e[31m"
BLUE="\e[34m"
DEFAULT="\e[0m"
# create vars and set to default
essid=""
pw=""
# look for password file and if exists get essid and pw
if [ -e $home/.wifisec ]; then
essid=$(cat $home/.wifisec | grep "^$ap," | sed 's/.*,\(.*\),.*/\1/')
pw=$(cat $home/.wifisec | grep "^$ap," | sed 's/.*,.*,\(.*\)/\1/')
else
echo 'Warning: no password file found'
fi
# when there is no pw file, or the ap is not in it, read info from cli
if [ "$essid" == "" ]; then
echo -n "essid? "
read essid
echo -n "pw? "
read pw
echo 'Adding AP to password file.'
echo "$ap,$essid,$pw" >> $home/.wifisec
fi
# start the connection magic
if [ $scanaps == 1 ]; then
echo "checking if ap exists: $essid"
string=$(iwlist $IF scan | grep "$essid")
else
string="valid"
fi
if [ -n "$string" ]; then
echo -e $BLUE"connecting to $RED$essid$BLUE...$DEFAULT"
# kill previous connections
#killall wpa_supplicant 2> /dev/null
#killall dhclient 2> /dev/null
# get the interface down
ifconfig $IF down
# release the dhcp lease
dhclient -r $IF
# configure wpa support
echo -e $BLUE"wpa_supplicant: $DEFAULT"
wpa_passphrase "$essid" "$pw" > /etc/wpa_supplicant.conf
wpa_supplicant -d -B -i $IF -Dwext -c /etc/wpa_supplicant.conf
# start the interface
ifconfig $IF up
# set the interface into managed mode
iwconfig $IF mode Managed
# request ip
echo -e $BLUE"dhclient: $DEFAULT"
dhclient -v $IF
dhc_ex_cd=$?
if [ "$dhc_ex_cd" -eq "0" ]; then
figlet -f banner "Muh, Connected to $essid" | cowsay -n
else
figlet -f banner "Muh, Connecting failed :(" | cowsay -n -e xx
exit
fi
# set name server
echo -n "" > /etc/resolv.conf
if [ $JOL == 1 ]; then
echo "nameserver 192.168.66.10" >> /etc/resolv.conf
fi
echo "nameserver 8.8.8.8" >> /etc/resolv.conf
echo "nameserver 8.8.4.4" >> /etc/resolv.conf
else
figlet -f banner "Error: ap not available" | cowsay -n -e 'xx'
fi