slitaz-boot-scripts view etc/init.d/network.sh @ rev 429

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