slitaz-boot-scripts annotate etc/init.d/network.sh @ rev 446

network.sh: stop wpa_cli interactive mode with static_ip
author Richard Dunbar <mojo@slitaz.org>
date Fri Oct 27 12:18:19 2017 -0400 (21 months ago)
parents 354d476130a4
children 01ab2b1d9b4d
rev   line source
pankso@8 1 #!/bin/sh
pankso@302 2 #
pankso@302 3 # /etc/init.d/network.sh : Network initialization boot script
al@393 4 # /etc/network.conf : Main SliTaz network configuration file
al@393 5 # /etc/wpa/wpa.conf : Wi-Fi networks configuration file
al@393 6
al@428 7
pankso@8 8 . /etc/init.d/rc.functions
pankso@8 9
al@393 10 CONF="${2:-/etc/network.conf}"
al@431 11 [ "$1" != 'netapplet' ] && echo "Loading network settings from $CONF"
al@393 12 . "$CONF"
al@393 13
al@428 14
al@428 15 # Change LXPanel Network applet settings
al@428 16
al@428 17 if [ "$1" == 'netapplet' ]; then
al@428 18 if [ "$WIFI" == 'yes' ]; then
al@428 19 interface="$WIFI_INTERFACE"
al@428 20 else
al@428 21 interface="$INTERFACE"
al@428 22 fi
al@428 23
al@431 24 for i in $(find ${XDG_CONFIG_HOME:-$HOME/.config}/lxpanel -name panel 2>/dev/null); do
al@428 25 fgrep -q netstatus "$i" || continue
al@428 26 sed -i '/iface/s|=.*$|='$interface'|' "$i"
al@428 27 done
al@428 28 exit 0
al@428 29 fi
al@428 30
al@393 31 WPA_CONF='/etc/wpa/wpa.conf'
al@428 32 [ ! -e "$WPA_CONF" ] && cp /etc/wpa/wpa_empty.conf $WPA_CONF 2>/dev/null
al@428 33 npid='/tmp/notify_pid'
al@393 34
al@393 35 # Migrate existing settings to a new format file
al@393 36
al@393 37 . /usr/share/slitaz/network.conf_migration
al@393 38
al@393 39
al@393 40 # Actions executing on boot time (running network.sh without parameters)
pascal@109 41
pankso@263 42 boot() {
al@393 43 # Set hostname
al@436 44 action 'Setting hostname to: %s' "$(cat /etc/hostname)"
pascal@112 45 /bin/hostname -F /etc/hostname
pascal@112 46 status
pascal@112 47
al@393 48 # Configure loopback interface
al@416 49 action 'Configuring loopback...'
pascal@112 50 /sbin/ifconfig lo 127.0.0.1 up
pankso@339 51 /sbin/route add -net 127.0.0.0 netmask 255.0.0.0 dev lo
gokhlayeh@241 52 status
pascal@322 53
pascal@322 54 [ -s /etc/sysctl.conf ] && sysctl -p /etc/sysctl.conf
jozee@188 55 }
jozee@188 56
al@393 57
al@428 58 # Freedesktop notification
al@428 59
al@428 60 notification() {
al@431 61 ps aux 2>/dev/null | grep -q [X]org || return
al@433 62 [ -n "$DISPLAY" ] || return
al@433 63 which notify-send >/dev/null || return
al@433 64
al@433 65 icon=$(echo $1 | awk -vw=$WIFI '{printf("notification-network-%s%s\n",
al@433 66 w=="yes"?"wireless":"wired", /start/?"":"-disconnected");}')
al@433 67
al@428 68 # FIXME: this valid only for lxde-session
al@431 69 local user="$(ps aux 2>/dev/null | grep [l]xde-session | awk 'END{print $2}')"
al@433 70 local rpid=''
al@428 71 [ -s "$npid" ] && rpid="-r $(cat $npid)"
al@433 72 su -c "notify-send $rpid -p -i $icon 'Network' \"$2\"" - $user | tr -c 0-9 '\n' | tail -n1 > $npid
al@428 73 }
al@428 74
al@428 75
al@428 76 # Change LXPanel Network applet interface
al@428 77
al@428 78 ch_netapplet() {
al@428 79 for user in $(awk -F: '$6 ~ "/home/" {print $1}' /etc/passwd); do
al@428 80 # need to be executed as user, due to different XDG variables
al@428 81 su -l -c "$0 netapplet" - "$user"
al@428 82 done
al@428 83 # restart if LXPanel running
al@433 84 if [ -n "$DISPLAY" -a -n "$(which lxpanelctl)" ]; then
al@433 85 lxpanelctl restart
al@433 86 fi
al@428 87 }
al@428 88
al@428 89
pankso@263 90 # Use ethernet
al@393 91
jozee@188 92 eth() {
al@428 93 if [ "$WIFI" != 'yes' ]; then
al@433 94 notification start "$(_ 'Starting Ethernet interface %s...' "$INTERFACE")"
al@428 95 ifconfig $INTERFACE up
al@428 96 sleep 5
al@428 97 fi
jozee@188 98 }
jozee@188 99
al@393 100
al@393 101 # Start wpa_supplicant with prepared settings in wpa.conf
al@393 102
al@393 103 start_wpa_supplicant() {
al@393 104 echo "Starting wpa_supplicant for $1..."
al@393 105 wpa_supplicant -B -W -c$WPA_CONF -D$WIFI_WPA_DRIVER -i$WIFI_INTERFACE
al@393 106 }
al@393 107
al@393 108
al@393 109 # Reconnect to the given network
al@393 110
al@393 111 reconnect_wifi_network() {
al@393 112 if [ "$WIFI" == 'yes' ]; then
al@393 113 # Wpa_supplicant will auto-connect to the first network
al@393 114 # notwithstanding to priority when scan_ssid=1
al@393 115 current_ssid="$(wpa_cli list_networks 2>/dev/null | fgrep '[CURRENT]' | cut -f2)"
al@393 116 if [ "$current_ssid" != "$WIFI_ESSID" ]; then
al@433 117 notification start "$(_ 'Connecting to %s...' "$WIFI_ESSID")"
al@436 118 action 'Connecting to %s...' "$WIFI_ESSID"
al@398 119 for i in $(seq 5); do
al@393 120 index=$(wpa_cli list_networks 2>/dev/null | \
al@393 121 grep -m1 -F $'\t'$WIFI_ESSID$'\t' | head -n1 | cut -f1)
al@393 122 [ -z "$index" ] && echo -n '.' && sleep 1
al@393 123 done
mojo@440 124 wpa_cli select_network $index 2>/dev/null; status
al@393 125 fi
al@393 126 fi
al@393 127 }
al@393 128
al@393 129
al@398 130 # Remove selected network settings from wpa.conf
al@398 131
al@398 132 remove_network() {
al@398 133 mv -f $WPA_CONF $WPA_CONF.old
al@398 134 cat $WPA_CONF.old | tr '\n' '\a' | sed 's|[^#]\(network={\)|\n\1|g' | \
al@398 135 fgrep -v "ssid=\"$1\"" | tr '\a' '\n' > $WPA_CONF
al@398 136 }
al@398 137
al@398 138
al@393 139 # For Wi-Fi. Users just have to enable it through WIFI="yes" and usually
al@393 140 # ESSID="any" will work and the interface is autodetected.
al@393 141
pankso@302 142 wifi() {
al@393 143 if [ "$WIFI" == 'yes' ]; then
pankso@263 144 ifconfig $INTERFACE down
gokhlayeh@241 145
al@393 146 # Confirm if $WIFI_INTERFACE is the Wi-Fi interface
jozee@188 147 if [ ! -d /sys/class/net/$WIFI_INTERFACE/wireless ]; then
al@393 148 echo "$WIFI_INTERFACE is not a Wi-Fi interface, changing it."
al@393 149 WIFI_INTERFACE=$(iwconfig 2>/dev/null | awk 'NR==1{print $1}')
pankso@282 150 [ -n "$WIFI_INTERFACE" ] && sed -i \
al@393 151 "s|^WIFI_INTERFACE=.*|WIFI_INTERFACE=\"$WIFI_INTERFACE\"|" \
pankso@282 152 /etc/network.conf
jozee@188 153 fi
gokhlayeh@241 154
al@433 155 notification start "$(_ 'Starting Wi-Fi interface %s...' "$WIFI_INTERFACE")"
al@436 156 action 'Configuring Wi-Fi interface %s...' "$WIFI_INTERFACE"
pankso@282 157 ifconfig $WIFI_INTERFACE up 2>/dev/null
al@393 158 if iwconfig $WIFI_INTERFACE | fgrep -q 'Tx-Power'; then
jozee@188 159 iwconfig $WIFI_INTERFACE txpower on
jozee@188 160 fi
jozee@188 161 status
pankso@302 162
al@393 163 IWCONFIG_ARGS=''
al@393 164 [ -n "$WIFI_WPA_DRIVER" ] || WIFI_WPA_DRIVER='wext'
al@393 165 [ -n "$WIFI_MODE" ] && IWCONFIG_ARGS="$IWCONFIG_ARGS mode $WIFI_MODE"
jozee@188 166 [ -n "$WIFI_CHANNEL" ] && IWCONFIG_ARGS="$IWCONFIG_ARGS channel $WIFI_CHANNEL"
al@393 167 [ -n "$WIFI_AP" ] && IWCONFIG_ARGS="$IWCONFIG_ARGS ap $WIFI_AP"
al@393 168
al@398 169 # Use "any" network only when it is needed
al@398 170 [ "$WIFI_ESSID" != 'any' ] && remove_network 'any'
al@398 171
al@393 172 # Clean all / add / change stored networks settings
al@393 173 if [ "$WIFI_BLANK_NETWORKS" == 'yes' ]; then
al@393 174 echo "Creating new $WPA_CONF"
al@393 175 cat /etc/wpa/wpa_empty.conf > $WPA_CONF
al@393 176 else
pascal@434 177 if fgrep -q "ssid=\"$WIFI_ESSID\"" $WPA_CONF; then
al@393 178 echo "Change network settings in $WPA_CONF"
paul@395 179 # Remove given existing network (it's to be appended later)
al@398 180 remove_network "$WIFI_ESSID"
al@393 181 else
al@393 182 echo "Append existing $WPA_CONF"
al@393 183 fi
pankso@198 184 fi
al@393 185
al@393 186 # Each new network has a higher priority than the existing
al@393 187 MAX_PRIORITY=$(sed -n 's|[\t ]*priority=\([0-9]*\)|\1|p' $WPA_CONF | sort -g | tail -n1)
al@393 188 PRIORITY=$(( ${MAX_PRIORITY:-0} + 1 ))
al@393 189
al@393 190 # Begin network description
al@393 191 cat >> $WPA_CONF <<EOT
pankso@282 192 network={
pankso@282 193 ssid="$WIFI_ESSID"
al@393 194 EOT
al@393 195
al@396 196 # For networks with hidden SSID: write its BSSID
al@393 197 [ -n "$WIFI_BSSID" ] && cat >> $WPA_CONF <<EOT
al@393 198 bssid=$WIFI_BSSID
al@396 199 EOT
al@396 200 # Allow probe requests (for all networks)
al@396 201 cat >> $WPA_CONF <<EOT
pankso@282 202 scan_ssid=1
al@393 203 EOT
al@393 204
al@393 205 case x$(echo -n $WIFI_KEY_TYPE | tr a-z A-Z) in
al@393 206 x|xNONE) # Open network
al@393 207 cat >> $WPA_CONF <<EOT
pankso@282 208 key_mgmt=NONE
al@393 209 priority=$PRIORITY
pankso@282 210 }
pankso@346 211 EOT
al@393 212 # start_wpa_supplicant NONE
al@393 213 iwconfig $WIFI_INTERFACE essid "$WIFI_ESSID" $IWCONFIG_ARGS
al@393 214 ;;
al@393 215
al@393 216 xWEP) # WEP security
al@393 217 # Encryption key length: 64 bit (5 ASCII or 10 HEX)
al@393 218 # Encryption key length: 128 bit (13 ASCII or 26 HEX)
al@393 219 # ASCII key in "quotes", HEX key without quotes
al@393 220 case "${#WIFI_KEY}" in
al@393 221 10|26) Q='' ;;
al@393 222 *) Q='"' ;;
al@393 223 esac
al@393 224 cat >> $WPA_CONF <<EOT
al@393 225 key_mgmt=NONE
al@393 226 auth_alg=OPEN SHARED
al@393 227 wep_key0=$Q$WIFI_KEY$Q
al@393 228 priority=$PRIORITY
pankso@346 229 }
pankso@346 230 EOT
al@393 231 start_wpa_supplicant WEP ;;
al@393 232
al@393 233 xWPA) # WPA/WPA2-PSK security
al@393 234 cat >> $WPA_CONF <<EOT
jozee@188 235 psk="$WIFI_KEY"
al@393 236 key_mgmt=WPA-PSK
al@393 237 priority=$PRIORITY
jozee@188 238 }
pankso@346 239 EOT
al@393 240 start_wpa_supplicant WPA/WPA2-PSK ;;
al@393 241
al@393 242 xEAP) # 802.1x EAP security
al@393 243 {
al@393 244 cat <<EOT
al@393 245 key_mgmt=WPA-EAP IEEE8021X
al@393 246 eap=$WIFI_EAP_METHOD
al@393 247 EOT
al@393 248 if [ "$WIFI_EAP_METHOD" == 'PWD' ]; then
al@393 249 WIFI_PHASE2=''; WIFI_CA_CERT=''; WIFI_USER_CERT=''; WIFI_ANONYMOUS_IDENTITY=''
al@393 250 fi
al@393 251 [ -n "$WIFI_CA_CERT" ] && echo -e "\tca_cert=\"$WIFI_CA_CERT\""
al@393 252 [ -n "$WIFI_CLIENT_CERT" ] && echo -e "\tclient_cert=\"$WIFI_CLIENT_CERT\""
al@393 253 [ -n "$WIFI_IDENTITY" ] && echo -e "\tidentity=\"$WIFI_IDENTITY\""
al@393 254 [ -n "$WIFI_ANONYMOUS_IDENTITY" ] && echo -e "\tanonymous_identity=\"$WIFI_ANONYMOUS_IDENTITY\""
al@393 255 [ -n "$WIFI_KEY" ] && echo -e "\tpassword=\"$WIFI-KEY\""
al@393 256 [ -n "$WIFI_PHASE2" ] && echo -e "\tphase2=\"auth=$WIFI_PHASE2\""
llevrel@421 257 echo }
al@393 258 } >> $WPA_CONF
al@393 259 start_wpa_supplicant '802.1x EAP' ;;
al@393 260
al@393 261 xANY)
al@393 262 cat >> $WPA_CONF <<EOT
jozee@188 263 key_mgmt=WPA-EAP WPA-PSK IEEE8021X NONE
jozee@188 264 group=CCMP TKIP WEP104 WEP40
jozee@188 265 pairwise=CCMP TKIP
jozee@188 266 psk="$WIFI_KEY"
al@393 267 password="$WIFI_KEY"
al@393 268 priority=$PRIORITY
jozee@188 269 }
pankso@346 270 EOT
al@393 271 start_wpa_supplicant 'any key type' ;;
al@393 272
jozee@188 273 esac
gokhlayeh@241 274 INTERFACE=$WIFI_INTERFACE
jozee@188 275 fi
jozee@188 276 }
jozee@188 277
al@393 278
pankso@340 279 # WPA DHCP script
al@393 280
pankso@340 281 wpa() {
pankso@340 282 wpa_cli -a"/etc/init.d/wpa_action.sh" -B
jozee@188 283 }
jozee@188 284
al@393 285
al@393 286 # For a dynamic IP with DHCP
al@393 287
jozee@188 288 dhcp() {
al@393 289 if [ "$DHCP" == 'yes' ]; then
gokhlayeh@241 290 echo "Starting udhcpc client on: $INTERFACE..."
al@393 291 # Is wpa wireless && wpa_ctrl_open interface up?
al@393 292 if [ -d /var/run/wpa_supplicant ] && [ "$WIFI" == 'yes' ]; then
pankso@263 293 wpa
al@393 294 else
al@393 295 # fallback on udhcpc: wep, eth
pankso@263 296 /sbin/udhcpc -b -T 1 -A 12 -i $INTERFACE -p \
al@393 297 /var/run/udhcpc.$INTERFACE.pid
gokhlayeh@241 298 fi
jozee@188 299 fi
jozee@188 300 }
jozee@188 301
al@393 302
al@393 303 # For a static IP
al@393 304
jozee@188 305 static_ip() {
al@393 306 if [ "$STATIC" == 'yes' ]; then
paul@190 307 echo "Configuring static IP on $INTERFACE: $IP..."
pascal@329 308 if [ -n "$BROADCAST" ]; then
erjo@327 309 /sbin/ifconfig $INTERFACE $IP netmask $NETMASK broadcast $BROADCAST up
erjo@327 310 else
erjo@327 311 /sbin/ifconfig $INTERFACE $IP netmask $NETMASK up
erjo@327 312 fi
al@393 313
paul@344 314 # Use ip to set gateways if iproute.conf exists
erjo@327 315 if [ -f /etc/iproute.conf ]; then
al@393 316 while read line; do
erjo@327 317 ip route add $line
erjo@327 318 done < /etc/iproute.conf
erjo@327 319 else
erjo@327 320 /sbin/route add default gateway $GATEWAY
erjo@327 321 fi
al@393 322
jozee@205 323 # wpa_supplicant waits for wpa_cli
mojo@446 324 [ -d /var/run/wpa_supplicant ] && wpa_cli -a"/etc/init.d/wpa_cli_static.sh" -B
al@393 325
al@393 326 # Multi-DNS server in $DNS_SERVER
jozee@188 327 /bin/mv /etc/resolv.conf /tmp/resolv.conf.$$
al@393 328 {
al@393 329 printf 'nameserver %s\n' $DNS_SERVER # Multiple allowed
al@393 330 [ -n "$DOMAIN" ] && echo "search $DOMAIN"
al@393 331 } >> /etc/resolv.conf
pascal@272 332 for HELPER in /etc/ipup.d/*; do
pascal@272 333 [ -x $HELPER ] && $HELPER $INTERFACE $DNS_SERVER
pascal@272 334 done
jozee@188 335 fi
pascal@112 336 }
pascal@112 337
al@393 338
al@393 339 # Stopping everything
al@393 340
pankso@346 341 stop() {
al@428 342 ch_netapplet
al@433 343 notification stop "$(_ 'Stopping all interfaces')"
al@393 344 echo 'Stopping all interfaces'
al@396 345 for iface in $(ifconfig | sed -e '/^[^ ]/!d' -e 's|^\([^ ]*\) .*|\1|' -e '/lo/d'); do
al@396 346 ifconfig $iface down
al@396 347 done
pascal@112 348 ifconfig $WIFI_INTERFACE down
pascal@112 349
al@393 350 echo 'Killing all daemons'
pascal@112 351 killall udhcpc
pankso@141 352 killall wpa_supplicant 2>/dev/null
gokhlayeh@241 353
al@393 354 if iwconfig $WIFI_INTERFACE | fgrep -q 'Tx-Power'; then
al@393 355 echo 'Shutting down Wi-Fi card'
pankso@141 356 iwconfig $WIFI_INTERFACE txpower off
pankso@141 357 fi
pascal@112 358 }
pascal@112 359
al@393 360
pankso@346 361 start() {
al@428 362 ch_netapplet
al@403 363 # stopping only unspecified interfaces
al@403 364 interfaces="$(ifconfig | sed -e '/^[^ ]/!d' -e 's|^\([^ ]*\) .*|\1|' -e '/lo/d')"
al@403 365 case $WIFI in
al@403 366 # don't stop Wi-Fi Interface if Wi-Fi selected
al@403 367 yes) interfaces="$(echo "$interfaces" | sed -e "/^$WIFI_INTERFACE$/d")";;
al@403 368 esac
al@403 369 for iface in $interfaces; do
al@403 370 ifconfig $iface down
al@403 371 done
al@403 372
al@396 373 eth; wifi
al@396 374 dhcp; static_ip
al@393 375 reconnect_wifi_network
al@393 376
al@393 377 # change default LXPanel panel iface
pankso@346 378 if [ -f /etc/lxpanel/default/panels/panel ]; then
al@393 379 sed -i "s/iface=.*/iface=$INTERFACE/" /etc/lxpanel/default/panels/panel
pankso@346 380 fi
pascal@112 381 }
pascal@112 382
al@393 383
al@393 384 # Looking for arguments:
al@393 385
al@393 386 case "$1" in
al@393 387 '')
al@393 388 boot; start ;;
al@393 389 start)
al@393 390 start ;;
al@393 391 stop)
al@393 392 stop ;;
al@393 393 restart)
al@393 394 stop; sleep 2; start ;;
al@393 395 *)
al@393 396 cat <<EOT
al@393 397
al@393 398 $(boldify 'Usage:') /etc/init.d/$(basename $0) [start|stop|restart]
al@393 399
al@393 400 Default configuration file is $(boldify '/etc/network.conf')
al@393 401 You can specify another configuration file in the second argument:
al@393 402 /etc/init.d/$(basename $0) [start|stop|restart] file.conf
al@393 403
al@393 404 EOT
al@393 405 ;;
al@393 406 esac
al@428 407
al@428 408 [ -f "$npid" ] && rm "$npid"