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

Added tag 5.6.6 for changeset ecf17d3e06f4
author Pascal Bellard <pascal.bellard@slitaz.org>
date Sun Jan 11 11:12:35 2015 +0100 (2015-01-11)
parents b2ca17642fab
children 6c2d6362887d
line source
1 #!/bin/sh
2 #
3 # /etc/init.d/network.sh : Network initialization boot script
4 # Configuration file : /etc/network.conf
5 #
6 . /etc/init.d/rc.functions
8 if [ -z "$2" ]; then
9 . /etc/network.conf
10 else
11 . $2
12 fi
14 boot() {
15 # Set hostname.
16 echo -n "Setting hostname to: $(cat /etc/hostname)"
17 /bin/hostname -F /etc/hostname
18 status
20 # Configure loopback interface.
21 echo -n "Configuring loopback..."
22 /sbin/ifconfig lo 127.0.0.1 up
23 /sbin/route add -net 127.0.0.0 netmask 255.0.0.0 dev lo
24 status
26 [ -s /etc/sysctl.conf ] && sysctl -p /etc/sysctl.conf
27 }
29 # Use ethernet
30 eth() {
31 ifconfig $INTERFACE up
32 }
34 # For wifi. Users just have to enable it through yes and usually
35 # essid any will work and the interface is autodetected.
36 wifi() {
37 if [ "$WIFI" == "yes" ]; then
38 ifconfig $INTERFACE down
40 # Confirm if $WIFI_INTERFACE is the wifi interface
41 if [ ! -d /sys/class/net/$WIFI_INTERFACE/wireless ]; then
42 echo "$WIFI_INTERFACE is not a wifi interface, changing it."
43 WIFI_INTERFACE=$(fgrep : /proc/net/dev | cut -d: -f1 | \
44 while read dev; do iwconfig $dev 2>&1 | \
45 fgrep -iq "essid" && { echo $dev ; break; }; \
46 done)
47 [ -n "$WIFI_INTERFACE" ] && sed -i \
48 "s/^WIFI_INTERFACE=.*/WIFI_INTERFACE=\"$WIFI_INTERFACE\"/" \
49 /etc/network.conf
50 fi
52 echo -n "Configuring $WIFI_INTERFACE..."
53 ifconfig $WIFI_INTERFACE up 2>/dev/null
54 if iwconfig $WIFI_INTERFACE | fgrep -q "Tx-Power"; then
55 iwconfig $WIFI_INTERFACE txpower on
56 fi
57 status
59 IWCONFIG_ARGS=""
60 [ -n "$WPA_DRIVER" ] || WPA_DRIVER="wext"
61 [ -n "$WIFI_MODE" ] && IWCONFIG_ARGS="$IWCONFIG_ARGS mode $WIFI_MODE"
62 [ -n "$WIFI_CHANNEL" ] && IWCONFIG_ARGS="$IWCONFIG_ARGS channel $WIFI_CHANNEL"
63 [ -n "$WIFI_AP" ] && IWCONFIG_ARGS="$IWCONFIG_ARGS ap $WIFI_AP"
65 # Unencrypted network
66 if [ "$WIFI_KEY" == "" -o "$WIFI_KEY_TYPE" == "" ]; then
67 iwconfig $WIFI_INTERFACE essid "$WIFI_ESSID" $IWCONFIG_ARGS
68 fi
70 # Encrypted network
71 [ -n "$WIFI_KEY" ] && case "$WIFI_KEY_TYPE" in
72 wep|WEP)
73 # wpa_supplicant can also deal with wep encryption
74 # Tip: Use unquoted strings for hexadecimal key in wep_key0
75 echo "Creating: /etc/wpa/wpa.conf"
76 cat /etc/wpa/wpa_empty.conf > /etc/wpa/wpa.conf
77 cat >> /etc/wpa/wpa.conf << EOT
78 network={
79 ssid="$WIFI_ESSID"
80 scan_ssid=1
81 key_mgmt=NONE
82 wep_key0=$WIFI_KEY
83 wep_tx_keyidx=0
84 priority=5
85 }
86 EOT
87 echo "Starting wpa_supplicant for NONE/WEP..."
88 wpa_supplicant -B -W -c/etc/wpa/wpa.conf -D$WPA_DRIVER \
89 -i$WIFI_INTERFACE ;;
91 wpa|WPA)
92 # load pre-configured multiple profiles
93 echo "Creating: /etc/wpa/wpa.conf"
94 cat /etc/wpa/wpa_empty.conf > /etc/wpa/wpa.conf
95 if [ "$WIFI_IDENTITY" ]; then
96 cat >> /etc/wpa/wpa.conf << EOT
97 network={
98 ssid="$WIFI_ESSID"
99 key_mgmt=WPA-EAP
100 scan_ssid=1
101 identity="$WIFI_IDENTITY"
102 password="$WIFI_PASSWORD"
103 }
104 EOT
105 else
106 cat >> /etc/wpa/wpa.conf << EOT
107 network={
108 ssid="$WIFI_ESSID"
109 scan_ssid=1
110 proto=WPA RSN
111 key_mgmt=WPA-PSK WPA-EAP
112 psk="$WIFI_KEY"
113 priority=5
114 }
115 EOT
116 fi
117 echo "Starting wpa_supplicant for WPA-PSK..."
118 wpa_supplicant -B -W -c/etc/wpa/wpa.conf \
119 -D$WPA_DRIVER -i$WIFI_INTERFACE ;;
121 any|ANY)
122 echo "Creating: /etc/wpa/wpa.conf"
123 cat /etc/wpa/wpa_empty.conf > /etc/wpa/wpa.conf
124 cat >> /etc/wpa/wpa.conf << EOT
125 network={
126 ssid="$WIFI_ESSID"
127 scan_ssid=1
128 key_mgmt=WPA-EAP WPA-PSK IEEE8021X NONE
129 group=CCMP TKIP WEP104 WEP40
130 pairwise=CCMP TKIP
131 psk="$WIFI_KEY"
132 priority=5
133 }
134 EOT
135 echo "Starting wpa_supplicant for any key type..."
136 wpa_supplicant -B -W -c/etc/wpa/wpa.conf \
137 -D$WPA_DRIVER -i$WIFI_INTERFACE ;;
138 esac
139 INTERFACE=$WIFI_INTERFACE
140 fi
141 }
143 # WPA DHCP script
144 wpa() {
145 wpa_cli -a"/etc/init.d/wpa_action.sh" -B
146 }
148 # For a dynamic IP with DHCP.
149 dhcp() {
150 if [ "$DHCP" = "yes" ] ; then
151 echo "Starting udhcpc client on: $INTERFACE..."
152 # Is wpa wireless && wpa_ctrl_open interface up ?
153 if [ -d /var/run/wpa_supplicant ] && [ "$WIFI" = "yes" ]; then
154 wpa
155 else # fallback on udhcpc: wep, eth
156 /sbin/udhcpc -b -T 1 -A 12 -i $INTERFACE -p \
157 /var/run/udhcpc.$INTERFACE.pid
158 fi
159 fi
160 }
162 # For a static IP.
163 static_ip() {
164 if [ "$STATIC" = "yes" ] ; then
165 echo "Configuring static IP on $INTERFACE: $IP..."
166 if [ -n "$BROADCAST" ]; then
167 /sbin/ifconfig $INTERFACE $IP netmask $NETMASK broadcast $BROADCAST up
168 else
169 /sbin/ifconfig $INTERFACE $IP netmask $NETMASK up
170 fi
172 # Use ip to set gateways if iproute.conf exists
173 if [ -f /etc/iproute.conf ]; then
174 while read line
175 do
176 ip route add $line
177 done < /etc/iproute.conf
178 else
179 /sbin/route add default gateway $GATEWAY
180 fi
182 # wpa_supplicant waits for wpa_cli
183 [ -d /var/run/wpa_supplicant ] && wpa_cli -B
185 # Multi-DNS server in $DNS_SERVER.
186 /bin/mv /etc/resolv.conf /tmp/resolv.conf.$$
187 for NS in $DNS_SERVER
188 do
189 echo "nameserver $NS" >> /etc/resolv.conf
190 done
191 if [ ! -z $DOMAIN ];then
192 echo "search $DOMAIN" >> /etc/resolv.conf
193 fi
194 for HELPER in /etc/ipup.d/*; do
195 [ -x $HELPER ] && $HELPER $INTERFACE $DNS_SERVER
196 done
197 fi
198 }
200 # stopping everything
201 stop() {
202 echo "stopping all interfaces"
203 ifconfig $INTERFACE down
204 ifconfig $WIFI_INTERFACE down
206 echo "Killing all daemons"
207 killall udhcpc
208 killall wpa_supplicant 2>/dev/null
210 if iwconfig $WIFI_INTERFACE | fgrep -q "Tx-Power"; then
211 echo "Shutting down wifi card"
212 iwconfig $WIFI_INTERFACE txpower off
213 fi
214 }
216 start() {
217 eth
218 wifi
219 dhcp
220 static_ip
221 # change default lxpanel panel iface
222 if [ -f /etc/lxpanel/default/panels/panel ]; then
223 sed -i "s/iface=.*/iface=$INTERFACE/" \
224 /etc/lxpanel/default/panels/panel
225 fi
226 }
228 # looking for arguments:
229 if [ -z "$1" ]; then
230 boot
231 start
232 else
233 case $1 in
234 start)
235 start ;;
236 stop)
237 stop ;;
238 restart)
239 stop
240 start ;;
241 *)
242 echo ""
243 echo -e "\033[1mUsage:\033[0m /etc/init.d/`basename $0` [start|stop|restart]"
244 echo ""
245 echo -e " Default configuration file is \033[1m/etc/network.conf\033[0m"
246 echo -e " You can specify another configuration file in the second argument:"
247 echo -e " \033[1mUsage:\033[0m /etc/init.d/`basename $0` [start|stop|restart] file.conf"
248 echo "" ;;
249 esac
250 fi