2 # Filename: grml-network
3 # Purpose: configuration script for network
4 # Authors: Michael Prokop <mika@grml.org>, Marcel Wichern 2006, Klaus Knopper 2002, Niall Walsh + Stefan Lippers-Hollmann 2004-2007
5 # Bug-Reports: see http://grml.org/bugs/
6 # License: This file is licensed under the GPL v2.
7 # Latest change: Sam Jän 27 19:28:54 CET 2007 [mika]
8 ################################################################################
9 # Changes have been merged from Kanotix's and Sidux's netcardconfig:
10 # http://kanotix.com/files/debian/pool/main/n/netcardconfig-kanotix/
11 # http://svn.berlios.de/wsvn/fullstory/configure-networkcard/trunk/
12 ################################################################################
14 PATH="/bin:/sbin:/usr/bin:/usr/sbin"
16 export PATH LANGUAGE LANG
18 . /etc/grml/script-functions
28 # This function produces the IWOURLINE for interfaces
31 if [ -n "$NWID" ]; then
32 IWOURLINE="$IWOURLINE wireless-nwid $NWID\n"
35 if [ -n "$MODE" ]; then
36 IWOURLINE="$IWOURLINE wireless-mode $MODE\n"
39 if [ -n "$CHANNEL" ]; then
40 IWOURLINE="$IWOURLINE wireless-channel $CHANNEL\n"
43 if [ -n "$FREQ" ]; then
44 IWOURLINE="$IWOURLINE wireless-freq $FREQ\n"
47 if [ -n "$KEY" ]; then
48 if [ "$PUBKEY" -eq 1 ]; then
49 # Store the key in interfaces in wireless-key
50 IWOURLINE="$IWOURLINE wireless-key $KEY\n"
52 # Store the key in /etc/network/wep.$DV which is root readable only
53 # Use pre-up in interfaces to read and set it
54 echo "$KEY" > /etc/network/wep.$DV && chmod 600 /etc/network/wep.$DV && IWOURLINE="$IWOURLINE pre-up KEY=\$(cat /etc/network/wep.$DV) && iwconfig $DV key \$KEY\n"
58 [ -d /sys/module/rt2??0/ ] && IWPREUPLINE="$IWPREUPLINE pre-up /sbin/ifconfig $DV up\n"
60 if [ -n "$IWCONFIG" ]; then
61 IWPREUPLINE="$IWPREUPLINE iwconfig $IWCONFIG\n"
64 if [ -n "$IWSPY" ]; then
65 IWPREUPLINE="$IWPREUPLINE iwspy $IWSPY\n"
68 if [ -n "$IWPRIV" ]; then
69 IWPREUPLINE="$IWPREUPLINE iwpriv $IWPRIV\n"
72 # execute ESSID last, but make sure that it is written as first option
73 if [ -n "$ESSID" ]; then
74 IWOURLINE="$IWOURLINE wireless-essid $ESSID\n"
77 if [ "$WPAON" -gt 0 ]; then
78 IWOURLINE="$IWOURLINE wpa-ssid $ESSID\n wpa-psk $WPASECRET\n"
81 IWOURLINE="$IWOURLINE $IWPREUPLINE"
82 #echo "DEBUG: for interfaces $IWOURLINE"
92 for PART in $DEVICE; do
93 if [ $PARTCOUNT -eq 0 ]; then
96 echo $PART | grep -q A::1 && isauto=1
97 echo $PART | grep -q F::1 && isfirewire=1
98 echo $PART | grep -q W::1 && iswireless=1
99 [ -z "$driver" ] && driver=$(echo $PART|awk 'BEGIN {FS="::"} /^D:/{print $2}')
100 [ -z "$mac" ] && mac=$(echo $PART|awk 'BEGIN {FS="::"} /^M:/{print $2}')
108 [ $isfirewire -gt 0 ] && MY_DEVICE_NAME="$NET_DEVICE_NAME_FW"
109 [ -z "$MY_DEVICE_NAME" -a $iswireless -gt 0 ] && MY_DEVICE_NAME="$NET_DEVICE_NAME_W"
110 [ -z "$MY_DEVICE_NAME" ] && MY_DEVICE_NAME="$NET_DEVICE_NAME"
111 MY_DEVICE_NAME="$DEVICENAME $MY_DEVICE_NAME $mac $driver"
112 [ $isauto -gt 0 ] && MY_DEVICE_NAME="$MY_DEVICE_NAME $NET_DEVICE_NAME_AUTO"
113 MY_DEVICE_NAME=$(echo $MY_DEVICE_NAME | sed 's/\ /__/g')
117 if ! egrep -e "^auto[ ]+.*$DV" /etc/network/interfaces >/dev/null; then
118 awk '{if(/^auto/){if(done==0){print $0 " '"$DV"'";done=1}else{print}}else{print}}END{if(done==0){print "auto '$DV'"}}' "/etc/network/interfaces" > "$TMP"
119 cat "$TMP" > /etc/network/interfaces
124 if egrep -e "^auto[ ]+.*$DV" /etc/network/interfaces >/dev/null; then
125 perl -pi -e 's/^(auto.*)'$DV'(.*)$/$1$2/;' /etc/network/interfaces
130 [ ! -r /etc/network/interfaces ] && touch /etc/network/interfaces
131 DEVICE=${NETDEVICES[$DV]}
135 WLDEVICE="$(LANG=C LC_MESSAGEWS=C iwconfig $DV 2>/dev/null | awk '/802\.11|READY|ESSID/{print $1}')"
136 WLDEVICECOUNT="$(LANG=C LC_MESSAGEWS=C iwconfig $DV 2>/dev/null | wc -l)"
137 if [ $iswireless -gt 0 ] && $DIALOG --yesno "$MESSAGE13" 8 45; then
152 if [ -f /etc/network/interfaces ]; then
153 awk '/iface/{if(/'"$DV"'/){found=1}else{found=0}}
154 /essid/{if(found){for(i=NF;i>=2;i--)essid=$i "~" essid}}
155 /nwid/{if(found){nwid=$NF}}
156 /mode/{if(found){mode=$NF}}
157 /channel/{if(found){channel=$NF}}
158 /freq/{if(found){freq=$NF}}
159 /sens/{if(found){sens=$NF}}
160 /rate/{if(found){rate=$NF}}
161 /rts/{if(found){rts=$NF}}
162 /frag/{if(found){frag=$NF}}
163 /iwconfig/{if(!/KEY/){if(found){iwconfig=$NF}}}
164 /iwspy/{if(found){iwspy=$NF}}
165 /iwpriv/{if(found){iwpriv=$NF}}
166 /wireless[-_]key/{if(found){gsub(/^\W*wireless[-_]key\W*/,"");key=$0}}
168 if (!(length(essid))){essid="~~~"}
169 if (!(length(nwid))){nwid="~~~"}
170 if (!(length(mode))){mode="~~~"}
171 if (!(length(channel))){channel="~~~"}
172 if (!(length(freq))){freq="~~~"}
173 if (!(length(sens))){sens="~~~"}
174 if (!(length(rate))){rate="~~~"}
175 if (!(length(rts))){rts="~~~"}
176 if (!(length(frag))){frag="~~~"}
177 if (!(length(iwconfig))){iwconfig="~~~"}
178 if (!(length(iwspy))){iwspy="~~~"}
179 if (!(length(iwpriv))){iwpriv="~~~"}
180 if (!(length(key))){key="~~~"}
181 print essid" "nwid" "mode" "channel" "freq" "sens" "rate" "rts" "frag" "iwconfig" "iwspy" "iwpriv" "key
182 }' /etc/network/interfaces >"$TMP"
184 read ESSID NWID MODE CHANNEL FREQ SENS RATE RTS FRAG IWCONFIG IWSPY IWPRIV KEY<"$TMP"
186 [ "$ESSID" = "~~~" ] && ESSID=""
187 [ "$NWID" = "~~~" ] && NWID=""
188 [ "$MODE" = "~~~" ] && MODE=""
189 [ "$CHANNEL" = "~~~" ] && CHANNEL=""
190 [ "$FREQ" = "~~~" ] && FREQ=""
191 [ "$SENS" = "~~~" ] && SENS=""
192 [ "$RATE" = "~~~" ] && RATE=""
193 [ "$RTS" = "~~~" ] && RTS=""
194 [ "$FRAG" = "~~~" ] && FRAG=""
195 [ "$IWCONFIG" = "~~~" ] && IWCONFIG=""
196 [ "$IWSPY" = "~~~" ] && IWSPY=""
197 [ "$IWPRIV" = "~~~" ] && IWPRIV=""
198 [ "$KEY" = "~~~" ] && KEY=""
200 ESSID=$(echo $ESSID | tr "~" " " | sed 's/ *$//')
202 if [ -z "$KEY" ]; then
203 KEY=$(cat /etc/network/wep.$DV 2>/dev/null)
205 if [ -z "$KEY" ]; then
214 #echo "DEBUG:E:$ESSID N:$NWID M:$MODE C:$CHANNEL F:$FREQ S:$SENS R:$RATE K:$KEY R:$RTS F:$FRAG I:$IWCONFIG I:$IWSPY I:$IWPRIV"
218 $DIALOG --inputbox "$MESSAGEW4 $DEVICENAME $MESSAGEW5" 15 50 "$ESSID" 2>"$TMP" || bailout 1
219 read ESSID <"$TMP" ; rm -f "$TMP"
220 [ -z "$ESSID" ] && ESSID="any"
222 $DIALOG --inputbox "$MESSAGEW6 $DEVICENAME $MESSAGEW7" 15 50 "$NWID" 2>"$TMP" || bailout 1
223 read NWID <"$TMP" ; rm -f "$TMP"
225 $DIALOG --inputbox "$MESSAGEW8 $DEVICENAME $MESSAGEW9" 15 50 "$MODE" 2>"$TMP" || bailout 1
226 read MODE <"$TMP" ; rm -f "$TMP"
227 [ -z "$MODE" ] && MODE="Managed"
229 $DIALOG --inputbox "$MESSAGEW10 $DEVICENAME $MESSAGEW11" 15 50 "$CHANNEL" 2>"$TMP" || bailout 1
230 read CHANNEL <"$TMP" ; rm -f "$TMP"
232 if [ -z "$CHANNEL" ]; then
233 $DIALOG --inputbox "$MESSAGEW12 $DEVICENAME $MESSAGEW13" 15 50 "$FREQ" 2>"$TMP" || bailout 1
234 read FREQ <"$TMP" ; rm -f "$TMP"
244 ipw2200|ipw2100|ipw3945)
255 if [ -z "$WPA_DEV" ]; then
256 if [ -d /proc/net/ndiswrapper/$DV ]; then
258 elif [ -d /proc/net/hostap/$DV ]; then
260 elif [ $WLDEVICECOUNT -eq 1 ]; then
261 if [ -e /proc/driver/atmel ]; then
269 if [ -n "$WPA_DEV" ]; then
270 if $DIALOG --yesno "$MESSAGEW22" 15 50; then
273 # bssid 00:11:22:33:44:55
274 # priority [0]|Integer
275 # proto [WPA RSN] WPA|RSN
276 # key_mgmt [WPA-PSK WPA-EAP]|NONE|WPA-PSK|WPA-EAP|IEEE8021X
277 # pairwise [CCMP TKIP]|CCMP|TKIP|NONE
278 # group [CCMP TKIP WEP105 WEP40]|CCMP|TKIP|WEP105|WEP40
279 # eapol_flags [3]|1|2
295 if (gsub(/^\W*wpa-psk\W*/,"")){
302 ' /etc/network/interfaces)
303 $DIALOG --inputbox "$MESSAGEW23 $ESSID" 15 50 "$WPASECRET" 2>"$TMP" || bailout 1
305 # make sure backslashes inside passphrase are handled correct
306 WPASECRET=$(sed -e 's/\\/\\/g' "$TMP") && rm -r "$TMP"
320 # No need for a wep key if we are using wpa
321 if [ ! $WPAON -eq 1 ]; then
322 $DIALOG --inputbox "$MESSAGEW14 $DEVICENAME $MESSAGEW15" 15 50 "$KEY" 2>"$TMP" || bailout 1
323 read KEY <"$TMP" ; rm -f "$TMP"
325 if [ -n "$KEY" -a "$PUBKEY" -eq 0 ]; then
326 if ! $DIALOG --yesno "$MESSAGEW25 $DEVICENAME $MESSAGEW26" 15 50; then
332 $DIALOG --inputbox "$MESSAGEW16 $DEVICENAME $MESSAGEW17" 15 50 "$IWCONFIG" 2>"$TMP" || bailout 1
333 read IWCONFIG <"$TMP" ; rm -f "$TMP"
335 $DIALOG --inputbox "$MESSAGEW18 $DEVICENAME $MESSAGEW19" 15 50 "$IWSPY" 2>"$TMP" || bailout 1
336 read IWSPY <"$TMP" ; rm -f "$TMP"
338 $DIALOG --inputbox "$MESSAGEW20 $DEVICENAME $MESSAGEW21" 15 50 "$IWPRIV" 2>"$TMP" || bailout 1
339 read IWPRIV <"$TMP" ; rm -f "$TMP"
344 if $DIALOG --yesno "$MESSAGE2" 8 45; then
345 if [ -w /etc/network/interfaces ]; then
347 awk '/iface/{if(/'"$DV"'/){found=1}else{found=0}}
348 /^\W$/{if(blank==0){lastblank=1}else{lastblank=0}{blank=1}}
349 /\w/{blank=0;lastblank=0}
350 {if(!(found+lastblank)){print}}
351 END{print "iface '"$DV"' inet dhcp"}' \
352 /etc/network/interfaces >"$TMP"
353 echo -e "$IWOURLINE" >> $TMP
354 #echo -e "\n\n" >> $TMP
355 cat "$TMP" >/etc/network/interfaces
357 # Add an "auto" entry
361 if [ -f /etc/network/interfaces ]; then
362 awk '/iface/{if(/'"$DV"'/){found=1}else{found=0}}
363 /address/{if(found){address=$NF}}
364 /netmask/{if(found){netmask=$NF}}
365 /broadcast/{if(found){broadcast=$NF}}
366 /gateway/{if(found){gateway=$NF}}
367 /dns-nameservers/{if(found){dns-nameservers=$NF}}
368 END{print address" "netmask" "broadcast" "gateway" "dns-nameservers}' /etc/network/interfaces >"$TMP"
369 read IP NM BC DG <"$TMP"
373 $DIALOG --inputbox "$MESSAGE6 $DV" 10 45 "${IP:-192.168.0.1}" 2>"$TMP" || bailout 1
374 read IP <"$TMP" ; rm -f "$TMP"
376 $DIALOG --inputbox "$MESSAGE7 $DV" 10 45 "${NM:-255.255.255.0}" 2>"$TMP" || bailout 1
377 read NM <"$TMP" ; rm -f "$TMP"
379 $DIALOG --inputbox "$MESSAGE8 $DV" 10 45 "${BC:-${IP%.*}.255}" 2>"$TMP" || bailout 1
380 read BC <"$TMP" ; rm -f "$TMP"
382 $DIALOG --inputbox "$MESSAGE9" 10 45 "${DG:-${IP%.*}.254}" 2>"$TMP"
383 read DG <"$TMP" ; rm -f "$TMP"
385 if [ -f "/etc/resolv.conf" ]; then
386 NS="$(awk '/^nameserver/{printf "%s ",$2}' /etc/resolv.conf)"
389 $DIALOG --inputbox "$MESSAGE10" 10 45 "${NS:-${IP%.*}.254}" 2>"$TMP"
390 read NS <"$TMP" ; rm -f "$TMP"
392 if [ -w /etc/network/interfaces ]; then
393 awk '/iface/{if(/'"$DV"'/){found=1}else{found=0}}
395 END{print "\niface '"$DV"' inet static\n\taddress '"$IP"'\n\tnetmask '"$NM"'\n\tnetwork '"${IP%.*}.0"'";if("'"$BC"'"!=""){print "\tbroadcast '"$BC"'"};if("'"$DG"'"!=""){print "\tgateway '"$DG"'"};if("'"$NS"'"!=""){print "\tdns-nameservers '"$NS"'"};if("'"$IWOURLINE"'"!=""){print "'"$IWOURLINE"'"};print "\n"}' \
396 /etc/network/interfaces >"$TMP"
398 cat "$TMP" >/etc/network/interfaces
401 # Add an "auto" entry
411 NET_DEVICE_NAME="Network_device"
412 NET_DEVICE_NAME_W="Wireless_device"
413 NET_DEVICE_NAME_FW="Firewire_device"
414 NET_DEVICE_NAME_AUTO="Auto"
415 MESSAGE0="No supported network cards found."
416 MESSAGE1="Please select network device"
417 MESSAGE2="Use DHCP broadcast?"
418 MESSAGE3="Sending DHCP broadcast from device"
420 MESSAGE5="Hit return to exit."
421 MESSAGE6="Please enter IP Address for"
422 MESSAGE7="Please enter Network Mask for"
423 MESSAGE8="Please enter Broadcast Address for"
424 MESSAGE9="Please enter Default Gateway"
425 MESSAGE10="Please enter Nameserver(s)"
426 MESSAGE11="Setting Nameserver in /etc/resolv.conf to"
427 MESSAGE12="Adding Nameserver to /etc/resolv.conf:"
428 MESSAGE13="Setup wireless options?"
429 MESSAGE14="Failed to bring up the interface, would you like to reconfigure it?"
430 MESSAGE15="Interface enabled, do you want it auto enabled at boot?"
431 MESSAGEW0="No wireless network card found."
432 MESSAGEW1="Configuration of wireless parameters for"
433 MESSAGEW3="Please configure IP parameters of the interface first"
434 MESSAGEW4="Enter the ESSID for"
435 MESSAGEW5="\n\n\n(empty for 'any', not recommended !)\n"
436 MESSAGEW6="Enter the NWID (cell identifier)\nfor"
437 MESSAGEW7=", if needed\n\n\n"
438 MESSAGEW8="Enter the mode for"
439 MESSAGEW9="\n\n(Managed(=default), Ad-Hoc, Master,\nRepeater, Secondary, auto)\n"
440 MESSAGEW10="Enter channel number for"
441 MESSAGEW11="\n\n(0 bis 16, empty for auto or if you want to\n enter the frequency next)\n"
442 MESSAGEW12="Enter the frequency for"
443 MESSAGEW13="\n\n(e.g 2.412G, empty for auto)"
444 MESSAGEW14="Enter the encryption key\nfor"
445 MESSAGEW15="\n\n(empty for cleartext, not recommended !!)"
446 MESSAGEW16="Enter additional parameters for\n'iwconfig"
447 MESSAGEW17="' if needed, e.g.\n\n\nsens -80 rts 512 frag 512 rate 5.5M"
448 MESSAGEW18="Enter additional parameters for\n'iwspy"
449 MESSAGEW19="' if needed\n\n\n"
450 MESSAGEW20="Enter additional parameters for\n'iwpriv"
451 MESSAGEW21="' if needed\n\n\n"
452 MESSAGEW22="Enable WPA support?"
453 MESSAGEW23="Enter the WPA passphrase (passphrase must be 8..63 characters) for"
454 MESSAGEW25="Would you like to store your wep key in it's own private file ("
455 MESSAGEW26=")? If you say no, your wep key will be stored in /etc/network/interfaces and will be readable by any account on your system. You may want to 'chmod 600 /etc/network/interfaces' if you answer no to this question"
459 LAN=$(tail -n +3 /proc/net/dev|awk -F: '{print $1}'|sed "s/\s*//"|grep -v -e ^lo -e ^vmnet|sort)
460 [ -n "$WLAN" ] || WLAN=$(tail -n +3 /proc/net/wireless 2>/dev/null|awk -F: '{print $1}'|sort)
461 unset LAN_DEVICES WLAN_DEVICES FIREWIRE_DEVICES NETDEVICES
462 while read dev mac; do
463 #echo "Making NETDEVICES $NETDEVICESCOUNT $dev"
464 iswlan=$(echo $dev $WLAN|tr ' ' '\n'|sort|uniq -d)
466 grep auto /etc/network/interfaces | grep -q $dev && isauto="1"
467 driver=$(ethtool -i $dev 2>/dev/null|awk '/^driver:/{print $2}')
468 if [ "$driver" ]; then
469 if [ "$iswlan" ]; then
470 NETDEVICES[$NETDEVICESCOUNT]="$dev A::$isauto M::$mac D::$driver W::1 F::0"
472 NETDEVICES[$NETDEVICESCOUNT]="$dev A::$isauto M::$mac D::$driver W::0 F::0"
475 if [ "$iswlan" ]; then
476 NETDEVICES[$NETDEVICESCOUNT]="$dev A::$isauto M::$mac W::1 F::0"
478 NETDEVICES[$NETDEVICESCOUNT]="$dev A::$isauto M::$mac W::0 F::0"
481 #echo "Made to ${NETDEVICES[$NETDEVICESCOUNT]}"
482 ((NETDEVICESCOUNT++))
483 done < <(ifconfig -a|grep Ethernet|grep -v ^vmnet|awk '! /^\s/{print $1" "$5}')
485 if [ "$(ethtool -i $dev 2>/dev/null|awk '/^bus-info:/{print $2}')" == "ieee1394" ]; then
487 grep auto /etc/network/interfaces | grep -q $dev && isauto="1"
488 NETDEVICES[$NETDEVICESCOUNT]="$dev A::$isauto D::$(ethtool -i $dev 2>/dev/null|awk '/^driver:/{print $2}') W::0 F::1"
489 ((NETDEVICESCOUNT++))
493 #NETDEVICES="$(cat /proc/net/dev | awk -F: '/eth.:|lan.:|tr.:|wlan.:|ath.:|ra.:/{print $1}')"
495 if [ -z "$NETDEVICES" ]; then
496 $DIALOG --msgbox "$MESSAGE0" 15 45
500 count="$NETDEVICESCOUNT"
502 if [ "$count" -gt 1 ]; then
505 while [ $mycount -lt $count ]; do
506 DEVICE=${NETDEVICES[$mycount]}
507 #echo "$mycount is $DEVICE"
509 #echo "name: $DEVICENAME auto: $isauto fw: $isfirewire mac: $mac driver: $driver"
511 DEVICELIST="$DEVICELIST $mycount $MY_DEVICE_NAME"
518 EXITMENU="$EXITKEY Exit"
520 # main program loop until they bailout
522 # first get the device
523 if [ "$count" -gt 1 ]; then
525 $DIALOG --menu "$MESSAGE1" 18 60 12 $DEVICELIST $EXITMENU 2>"$TMP" || bailout
526 read DV <"$TMP" ; rm -f "$TMP"
527 [ "$DV" = "$EXITKEY" ] && bailout
531 # they have asked to stop configuring the interface so exit
532 [ -z "$IFACEDONE" ] || bailout
536 while [ -n "$DV" -a -z "$IFACEDONE" ]; do
541 $DIALOG --yesno "$MESSAGE14" 15 50 || IFACEDONE="DONE"
543 $DIALOG --yesno "$MESSAGE15" 15 50 && addauto || remauto
549 ## END OF FILE #################################################################