This repository has been archived by the owner on Jun 7, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwifi_setup.sh
executable file
·155 lines (132 loc) · 6.64 KB
/
wifi_setup.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
154
155
#/usr/bin/sh
################################################################################
#===================================LICENSE====================================#
# This is licensed under the WILY license ©somewhere in time #
#==============================================================================#
#==============================================================================#
# Establish wireless connection with wpa_supplicant and dhcpd #
# -----------------------------------------------------------------------------#
# Some say the wheel shouldn't be reinvented #
# Others claims it should be reinvented to lose some performance #
# I agree with the later group #
# If that's not your case, consider visiting this link: #
# https://wiki.archlinux.org/index.php/WPA_supplicant #
# -----------------------------------------------------------------------------#
# Most of this is copy-pasta from various website that I won't credit here #
# I'm mostly proud of the parts consisting of the symbols: -, =, # #
#==============================================================================#
################################################################################
#------------------------------------------------------------------------------#
# Functions declarations #
#------------------------------------------------------------------------------#
# Connection without encryption
function no_encrypt () {
# iw dev interface connect "your_essid"
printf "$PROMPT Connecting to $2 with interface $1\n\n"
iw dev $1 connect $2
}
# Connection to WEP
# The implementation of this function is left as an exercice for the reader :)
#function wep () {
#}
# Connection to a WPA/WPA2
function wpa_2 () {
printf "$PROMPT Connecting to $2 with interface $1\n"
wpa_passphrase $2 $3 >> .temp.wifi_setup
wpa_supplicant -i $1 -c .temp.wifi_setup -B
rm .temp.wifi_setup
}
# Set interface up
function set_i_up () {
ip link set $1 up
}
# Kill all wpa_supplicant and dhcpcd processes
function kill_all {
printf "$PROMPT Killing all wpa_supplicant and dhcpcd processes\n"
killall wpa_supplicant`
killall dhcpcd`
}
################################################################################
#------------------------------------------------------------------------------#
# VARIABLES #
#------------------------------------------------------------------------------#
HELP='wifi_setup.help' # Help file location
PROMPT='[WIFI_SETUP] : ' # Prompt :)
declare interface # Interface to use, i.e wlan0
declare e_ssid # Network name
declare psswd # Password
#------------------------------------------------------------------------------#
# Argument parsing #
#------------------------------------------------------------------------------#
# Use -gt 1 to consume two arguments per pass in the loop (e.g. each #
# argument has a corresponding value to go with it). #
# To you bash illiterate who won't look up what gt is -> greater than #
# The meaning of -lt is left has an exercise for the reader #
# This means you have to associate each flag with a value #
# Otherwise you're screwed #
# -----------------------------------------------------------------------------#
if [[ $# -lt 2 ]]; then
cat $HELP
exit 1
fi
# Save arguments in case whoami != root
args=("$@")
while [[ $# -gt 1 ]]
do
key="$1"
case $key in
-i|--interface)
interface="$2"
shift # past argument
;;
-e|-s|--essid|--ssid)
e_ssid="$2"
shift # past argument
;;
-p|--pswd)
psswd="$2"
shift # past argument
;;
-h|--help) # A loser is asking for help
printf "$PROMPT Asking for help without shame huh?\n"
cat $HELP # Quick
exit 1 # Let's exit
;;
*) # unknown option
printf "$PROMPT Unknown argument $key\n$PROMPT Ignoring\n"
cat $HELP | head -n 11 | tail -n 4
shift # To shift or not to shift? In the doubt, shift!
;;
esac
shift # past argument or value
done
################################################################################
#------------------------------------------------------------------------------#
# You shall not run this script if you're not root, move along #
# Putting this down there because you don't need root to ask for help #
# You don't need root to call the script wrongly neither #
# -----------------------------------------------------------------------------#
[ "$(whoami)" != "root" ] && exec sudo -- "$0" "${args[@]}"
################################################################################
#------------------------------------------------------------------------------#
# Validity checks and functions calling #
#------------------------------------------------------------------------------#
# If both the interface and the e?ssid are provided
if [[ -n "$interface" ]] && [[ -n "$e_ssid" ]]; then
kill_all
set_i_up $interface
if [[ -n "$psswd" ]]; then
wpa_2 $interface $e_ssid $psswd
else
no_encrypt $interface $e_ssid
fi
dhcpcd $interface # This
else
# Print this to help loosers figure out the usage
printf "$PROMPT Invalid arguments\n"
cat $HELP
exit 3
fi
################################################################################
# THE END #
################################################################################