-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinternet
More file actions
executable file
·164 lines (143 loc) · 4.19 KB
/
internet
File metadata and controls
executable file
·164 lines (143 loc) · 4.19 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
#!/bin/bash
get_interface(){
interface=$( ip route | awk '/^default/ {print $5}' )
}
get_active(){
active=`nmcli -f name con show --active | sed '1d;s/\s*$//;q'`
[[ -z "$active" ]] && active=None
}
get_wifi_list(){
list=$(nmcli -f IN-USE,ssid,mode,chan,rate,signal device wifi list)
printf "SrNo\t$(echo "$list" | sed '1p;d')\n"
wifi_list_show=$(echo "$list" | sed '1d' | awk '{print ++i")" "\t" $0}')
echo "$wifi_list_show"
}
get_wifi_name(){
local __wifi_name=$1
read -p "Enter SrNO, r to refresh or q to quit: " srno
if [[ $srno == q ]];then
eval $__wifi_name="'q'"
return 0
elif [[ $srno == r ]];then
eval $__wifi_name="'r'"
return 0
elif [[ $srno == "" ]];then
return 1
fi
[[ -z $srno || ! $srno =~ [0-9]+ ]] && (echo $srno;echo "Sr no. is invalid") || :
local wifi_ssid=$(nmcli -t -f ssid dev wifi list)
local __name=$(echo -e "$wifi_ssid" | sed -e "${srno}p;d")
if [[ -z "$__name" ]];then
echo SrNo. $srno is invalid
else
eval $__wifi_name="'$__name'"
fi
}
get_con_name(){
con=$(nmcli -f name con show | grep "$1" | sed 's/\s*$//;q')
[[ ! -z $con ]] && eval "$2=\"$con\"" || return 1
}
connect_wifi(){
# To check if the wifi is connected to the desired network or no network at all
while [[ "$active" != "$wifi_name" || "$active" == None ]]; do
# To scan for new networks in case of a rescan
nmcli device wifi rescan 2> /dev/null
# Time required for a rescan to complete or waiting in case of high cpu usage
sleep 5
# List available wifi networks
get_wifi_list
# Get SSID
get_wifi_name wifi_name
[[ $wifi_name == q ]] && exit
[[ $wifi_name == r || $wifi_name == "" ]] && continue
echo Selected network: "$wifi_name"
get_active
if [[ $active == $wifi_name ]];then
echo Currently connected to "$wifi_name"
return
fi
# To check if a connection is already configured or not
get_con_name "$wifi_name" con_name
if [[ $con_name > 0 ]];then
echo Connecting to connection: "$con_name"
# If the wifi is connected to any network by default it needs to close that connection
get_active
[[ "$active" == None ]] && : || $(nmcli connection down "$active" > /dev/null)
# If the network is previously configured then activate the connection.
nmcli --ask connection up "$con_name" > /dev/null
else
echo "Connection does not exist. Creating connection."
nmcli --ask dev wifi connect "$wifi_name"
fi
get_active
if [[ "$active" == "$wifi_name" ]];then
local success_string="Successfully connected to $wifi_name"
echo $success_string
#notify-send "$(echo $success_string)"
notify-send "$success_string"
else
nmcli con del "$con_name" > /dev/null
fi
echo
done
}
connect_lan(){
nmcli dev disconnect $wifi_in
nmcli dev con $lan_in
get_interface
[[ $interface == $lan_in ]] && notify-send "Successfull connected to Ethernet" || :
}
lan_refresh(){
sudo ip link set $1 down
sleep 2
sudo ip link set $1 up
}
wifi_refresh(){
get_active
nmcli radio wifi off
sleep 2
nmcli radio wifi on
} #}}}
# Setting Variables
get_active
# getting the interface (wifi/lan) which is active at the moment
interface=$( ip route | awk '/^default/ {print $5}' )
lan_in="enp3s0"
wifi_in="wlp1s0"
#}}}
# Main
if [[ $1 == "" ]];then
echo "$active"
exit 0
fi
while getopts ":wlrd:" arg > /dev/null;do
case $arg in
w) wifi=1;;
l) lan=1;;
r) refresh=1;;
d) delete=$OPTARG;;
esac
done
if [[ $refresh == 1 ]];then
if [[ $interface == $lan_in ]];then
lan_refresh $interface
elif [[ $interface == $wifi_in ]];then
wifi_refresh
fi
elif [[ $wifi == 1 ]];then
nmcli radio wifi on
# Sometimes the wifi gets connected to a network which is preconfigured, automatically
get_active
if [[ "$active" == None || "$interface" != "$wifi_in" ]];then
connect_wifi
else
read -p "Wifi connected to $active. Change?[y/n] " choice
[[ $choice == y ]] && connect_wifi || :
fi
elif [[ $lan == 1 ]];then
# If the interface is enp3s0 then do nothing
# If the no wifi active then do nothing
# Or else disconnect with that wifi
[[ $interface == $lan_in ]] && exit 1 || :
connect_lan
fi