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 ################################################################################
8 # Changes have been merged from Kanotix's and Sidux's netcardconfig:
9 # http://kanotix.com/files/debian/pool/main/n/netcardconfig-kanotix/
10 # http://svn.berlios.de/wsvn/fullstory/configure-networkcard/trunk/
11 ################################################################################
13 PATH="/bin:/sbin:/usr/bin:/usr/sbin"
15 export PATH LANGUAGE LANG
17 . /etc/grml/script-functions
27 # This function produces the IWOURLINE for interfaces
31 if [ -n "$KEY" ]; then
32 if [ "$PUBKEY" -eq 1 ]; then
33 # Store the key in interfaces in wireless-key
34 IWOURLINE="$IWOURLINE\twireless-key $KEY\n"
36 # Store the key in /etc/network/wep.$DV which is root readable only
37 # Use pre-up in interfaces to read and set it
38 echo "$KEY" > "/etc/network/wep.$DV" && chmod 600 "/etc/network/wep.$DV" && IWOURLINE="$IWOURLINE\tpre-up KEY=\$(cat /etc/network/wep.$DV) && iwconfig $DV key \$KEY\n"
42 for mod in /sys/module/rt2??0/ ; do
43 if [ -d "$mod" ]; then
44 IWPREUPLINE="$IWPREUPLINE\tpre-up /sbin/ip link set $DV up\n"
49 # execute ESSID last, but make sure that it is written as first option
50 if [ -n "$ESSID" ]; then
51 IWOURLINE="$IWOURLINE\twireless-essid $ESSID\n"
54 if [ "$WPAON" -gt 0 ]; then
55 IWOURLINE="$IWOURLINE\twpa-ssid $ESSID\n\twpa-psk $WPASECRET\n"
58 IWOURLINE="$IWOURLINE $IWPREUPLINE"
59 #echo "DEBUG: for interfaces $IWOURLINE"
62 generate_udev_entry() {
64 mkdir -p /etc/udev/scripts
65 echo "# Auto generated script from netcardconfig on $(date)
66 # Executing this script generates an entry in /etc/udev/rules.d/z25_persistent-net.rules
67 # for you, please check z25_persistent-net.rules for existing entries before
68 # running this script (once more)." > /etc/udev/scripts/netcardconfig
69 for interface in $(ip -oneline link | awk '!/ lo: / {print $2}' | sed 's/:$//; s/@.*//') ; do
70 echo -n "INTERFACE=$interface /lib/udev/write_net_rules " >> /etc/udev/scripts/netcardconfig && \
71 if which udevadm >/dev/null 2>&1; then
72 udevadm info -a -p "/sys/class/net/$interface" | awk -F'==' '/address/ {print $2}' >> /etc/udev/scripts/netcardconfig
74 udevinfo -a -p "/sys/class/net/$interface" | awk -F'==' '/address/ {print $2}' >> /etc/udev/scripts/netcardconfig
77 # send errors to /dev/null as well because the sed line inside the /lib/udev/write_net_rules
78 # script outputs a sed warning which might unsettle users
79 chmod +x /etc/udev/scripts/netcardconfig && /etc/udev/scripts/netcardconfig 1>/dev/null 2>&1
82 disable_config_dhcp() {
83 if grep -q CONFIG_DHCP /etc/grml/autoconfig ; then
84 sed -i "s|^CONFIG_DHCP.*|CONFIG_DHCP='no'|" /etc/grml/autoconfig
95 for PART in $DEVICE; do
96 if [ $PARTCOUNT -eq 0 ]; then
99 echo "$PART" | grep -q A::1 && isauto=1
100 echo "$PART" | grep -q F::1 && isfirewire=1
101 echo "$PART" | grep -q W::1 && iswireless=1
102 [ -z "$driver" ] && driver=$(echo "$PART"|awk 'BEGIN {FS="::"} /^D:/{print $2}')
103 [ -z "$mac" ] && mac=$(echo "$PART"|awk 'BEGIN {FS="::"} /^M:/{print $2}')
111 [ $isfirewire -gt 0 ] && MY_DEVICE_NAME="$NET_DEVICE_NAME_FW"
112 [ -z "$MY_DEVICE_NAME" ] && [ $iswireless -gt 0 ] && MY_DEVICE_NAME="$NET_DEVICE_NAME_W"
113 [ -z "$MY_DEVICE_NAME" ] && MY_DEVICE_NAME="$NET_DEVICE_NAME"
114 MY_DEVICE_NAME="$DEVICENAME $MY_DEVICE_NAME $mac $driver"
115 [ $isauto -gt 0 ] && MY_DEVICE_NAME="$MY_DEVICE_NAME $NET_DEVICE_NAME_AUTO"
116 MY_DEVICE_NAME="${MY_DEVICE_NAME// /__}"
120 if ! grep -E "^auto .*$DV" /etc/network/interfaces >/dev/null; then
121 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"
122 cat "$TMP" > /etc/network/interfaces
127 if grep -e "^auto .*$DV" /etc/network/interfaces >/dev/null; then
128 perl -pi -e 's/^(auto.*)'"$DV"'(.*)$/$1$2/;' /etc/network/interfaces
135 iwlist "$DV" scanning | grep "ESSID\|Quality" | sed -e "s/^.*ESSID:\"\|\"$//g" | tac > "$TMP"
147 if [[ -n "${METHOD}" ]] && [[ "${METHOD}" != 'dhcp' ]]; then
160 [ ! -r /etc/network/interfaces ] && touch /etc/network/interfaces
161 DEVICE=${NETDEVICES[$DV]}
164 ifdown "${DV}" --force
167 if "${INTERACTIVE}" ; then
168 # Setup wireless options?
169 if [ $iswireless -gt 0 ] && $DIALOG --yesno "$MESSAGE13" 8 45; then
170 WLDEVICECOUNT="$(LANG=C LC_MESSAGEWS=C iwconfig "$DV" 2>/dev/null | wc -l)"
174 if [ -f /etc/network/interfaces ]; then
175 awk '/iface/{if(/'"$DV"'/){found=1}else{found=0}}
176 /essid/{if(found){for(i=NF;i>=2;i--)essid=$i "~" essid}}
177 /wireless[-_]key/{if(found){gsub(/^\W*wireless[-_]key\W*/,"");key=$0}}
179 if (!(length(essid))){essid="~~~"}
180 if (!(length(key))){key="~~~"}
182 }' /etc/network/interfaces >"$TMP"
184 read -r ESSID KEY<"$TMP"
186 [[ "$ESSID" =~ ^~~~$ ]] && ESSID=""
187 [[ "$KEY" =~ ^~~~$ ]] && KEY=""
189 # shellcheck disable=SC2088
190 ESSID=$(echo $ESSID | tr '~' " " | sed 's/ *$//')
192 if [ -z "$KEY" ]; then
193 KEY=$(cat "/etc/network/wep.$DV" 2>/dev/null)
195 if [ -z "$KEY" ]; then
204 #echo "DEBUG:E:$ESSID K:$KEY "
208 # ask to scan or manually set wireless network
209 if $DIALOG --yesno "$MESSAGEWSCAN1" 8 45 ; then
212 while [ $RESCAN -eq 1 ]
215 $DIALOG --extra-button --extra-label "Rescan" --menu "$MESSAGEWSCAN2" 20 70 15 "${WARRAY[@]}" 2>"$TMP"
228 read -r ESSID <"$TMP" ; rm -f "$TMP"
231 $DIALOG --inputbox "$MESSAGEW4 $DEVICENAME $MESSAGEW5" 15 50 "$ESSID" 2>"$TMP" || bailout 1
232 read -r ESSID <"$TMP" ; rm -f "$TMP"
233 [ -z "$ESSID" ] && ESSID="any"
243 ipw2200|ipw2100|ipw3945)
254 if [ -z "$WPA_DEV" ]; then
255 if [ -d "/proc/net/ndiswrapper/$DV" ]; then
257 elif [ -d "/proc/net/hostap/$DV" ]; then
259 elif [ "$WLDEVICECOUNT" -eq 1 ]; then
260 if [ -e /proc/driver/atmel ]; then
268 if [ -n "$WPA_DEV" ]; then
269 if $DIALOG --yesno "$MESSAGEW22" 15 50; then
272 # bssid 00:11:22:33:44:55
273 # priority [0]|Integer
274 # proto [WPA RSN] WPA|RSN
275 # key_mgmt [WPA-PSK WPA-EAP]|NONE|WPA-PSK|WPA-EAP|IEEE8021X
276 # pairwise [CCMP TKIP]|CCMP|TKIP|NONE
277 # group [CCMP TKIP WEP105 WEP40]|CCMP|TKIP|WEP105|WEP40
278 # eapol_flags [3]|1|2
294 if (gsub(/^\W*wpa-psk\W*/,"")){
301 ' /etc/network/interfaces)
302 FIRST_RUN=0 # show the wpasecret input box at least once
303 while ( [ -z "$WPASECRET" ] || [ "$FIRST_RUN" ] ) ; do
304 $DIALOG --inputbox "$MESSAGEW23 $ESSID" 15 50 "$WPASECRET" 2>"$TMP" || bailout 1
305 read -r WPASECRET <"$TMP"
306 if [ -z "$WPASECRET" ] ; then
307 $DIALOG --msgbox "Sorry, empty password not allowed, please retry." 0 0 || bailout 1
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 -r KEY <"$TMP" ; rm -f "$TMP"
325 if [ -n "$KEY" ] && [ "$PUBKEY" -eq 0 ]; then
326 if ! $DIALOG --yesno "$MESSAGEW25 $DEVICENAME $MESSAGEW26" 15 50; then
333 if [ "$iswireless" = "1" ] ; then
337 fi # Setup wireless options?
339 # Configure VLAN on this interface?
340 if $DIALOG --defaultno --yesno "$MESSAGE16" 8 45; then
341 $DIALOG --inputbox "$MESSAGE17 $DV" 10 45 2>"$TMP" || bailout 1
342 read -r VLAN <"$TMP" ; rm -f "$TMP"
343 if [ -n "$VLAN" ]; then
344 modprobe 8021q # avoid warning that VLAN support isn't present yet
349 fi # INTERACTIVE=true
351 # Use DHCP broadcast?
352 if ("${INTERACTIVE}" && "${DIALOG}" --yesno "${MESSAGE2}" 8 45) || \
353 (! "${INTERACTIVE}" && [[ "${METHOD}" == 'dhcp' ]]) ; then
354 if [ -w /etc/network/interfaces ]; then
356 awk '/iface/{if(/'"$DV"'/){found=1}else{found=0}}
357 /^\W$/{if(blank==0){lastblank=1}else{lastblank=0}{blank=1}}
358 /\w/{blank=0;lastblank=0}
359 {if(!(found+lastblank)){print}}
360 END{print "iface '"$DV"' inet dhcp";if("'"$PDV"'"!=""){print "\tvlan-raw-device '"$PDV"'"}}' \
361 /etc/network/interfaces >"$TMP"
364 # write wireless configuration only when we are dealing with a wireless device,
365 # it might be set from a previous configuration run of a wireless device
366 # while the currently configured network device isn't a wireless device
367 echo -e "$IWOURLINE" >> "$TMP"
370 #echo -e "\n\n" >> $TMP
371 cat "$TMP" >/etc/network/interfaces
373 # Add an "auto" entry
377 if "${INTERACTIVE}" ; then
378 if [ -f /etc/network/interfaces ]; then
379 awk '/iface/{if(/'"$DV"'/){found=1}else{found=0}}
380 /address/{if(found){address=$NF}}
381 /netmask/{if(found){netmask=$NF}}
382 /broadcast/{if(found){broadcast=$NF}}
383 /gateway/{if(found){gateway=$NF}}
384 /dns-nameservers/{if(found){dnsnameservers=$NF}}
385 END{print address" "netmask" "broadcast" "gateway" "dnsnameservers}' /etc/network/interfaces >"$TMP"
386 read -r IP NM BC DG NS <"$TMP"
390 $DIALOG --inputbox "$MESSAGE6 $DV" 10 45 "${IP:-192.168.0.1}" 2>"$TMP" || bailout 1
391 read -r IP <"$TMP" ; rm -f "$TMP"
393 $DIALOG --inputbox "$MESSAGE7 $DV" 10 45 "${NM:-255.255.255.0}" 2>"$TMP" || bailout 1
394 read -r NM <"$TMP" ; rm -f "$TMP"
396 $DIALOG --inputbox "$MESSAGE8 $DV" 10 45 "${BC:-${IP%.*}.255}" 2>"$TMP" || bailout 1
397 read -r BC <"$TMP" ; rm -f "$TMP"
399 $DIALOG --inputbox "$MESSAGE9" 10 45 "${DG:-${IP%.*}.1}" 2>"$TMP"
400 read -r DG <"$TMP" ; rm -f "$TMP"
402 if [ -f "/etc/resolv.conf" ]; then
403 NS="$(awk '/^nameserver/{printf "%s ",$2}' /etc/resolv.conf)"
406 $DIALOG --inputbox "$MESSAGE10" 10 45 "${NS:-$DG}" 2>"$TMP"
407 read -r NS <"$TMP" ; rm -f "$TMP"
410 if [ -w /etc/network/interfaces ]; then
415 # ensure we don't leak the IWOURLINE into the
416 # following awk command line, if we are configuring
417 # a device without wireless capabilities
422 awk '/iface/{if(/'"$DV"'/){found=1}else{found=0}}
424 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("'"$PDV"'"!=""){print "\tvlan-raw-device '"$PDV"'"};if("'"$IWOURLINE"'"!=""){print "'"$IWOURLINE"'"};print "\n"}' \
425 /etc/network/interfaces >"$TMP"
427 cat "$TMP" >/etc/network/interfaces
430 # Add an "auto" entry
434 fi # Use DHCP broadcast?
440 # get persistent interface names across reboots
442 # make sure we don't run the dhcp stuff when using /etc/network/interfaces
448 NET_DEVICE_NAME="Network_device"
449 NET_DEVICE_NAME_W="Wireless_device"
450 NET_DEVICE_NAME_FW="Firewire_device"
451 NET_DEVICE_NAME_AUTO="Auto"
452 MESSAGE0="No supported network cards found."
453 MESSAGE1="Please select network device"
454 MESSAGE2="Use DHCP broadcast?"
455 MESSAGE6="Please enter IP Address for"
456 MESSAGE7="Please enter Network Mask for"
457 MESSAGE8="Please enter Broadcast Address for"
458 MESSAGE9="Please enter Default Gateway"
459 MESSAGE10="Please enter Nameserver(s)"
460 MESSAGE13="Setup wireless options?"
461 MESSAGE14="Failed to bring up the interface, would you like to reconfigure it?"
462 MESSAGE15="Interface enabled, do you want it auto enabled at boot?"
463 MESSAGE16="Configure VLAN on this interface?"
464 MESSAGE17="Please enter VLAN id for"
465 MESSAGEW4="Enter the ESSID for"
466 MESSAGEW5="\n(empty for 'any', not recommended!)\n"
467 MESSAGEW14="Enter the encryption key\nfor"
468 MESSAGEW15="\n\n(empty for cleartext, not recommended !!)"
469 MESSAGEW22="Enable WPA support?"
470 MESSAGEW23="Enter the WPA passphrase (passphrase must be 8..63 characters) for"
471 MESSAGEW25="Would you like to store your wep key in it's own private file ("
472 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"
473 MESSAGEWSCAN1="Scan for available wireless networks?"
474 MESSAGEWSCAN2="Please choose a wireless network from below:"
476 NET_DEV="${NET_DEV:-}"
477 METHOD="${METHOD,,:-}"
479 NETMASK="${NETMASK:-}"
480 GATEWAY="${GATEWAY:-}"
485 if [ -r /proc/net/dev ] ; then
486 LAN=$(tail -n +3 /proc/net/dev | awk -F: '{print $1}'| sed "s/\s*//" | grep -v -e ^lo -e ^vmnet | sort)
491 if [[ -n "${NET_DEV}" ]] && [[ -n "${METHOD}" ]]; then
492 if [[ "${METHOD}" != 'dhcp' ]]; then
494 if [[ -z "${IPADDR}" ]]; then
495 echo "Mandatory parameter IPADDR for method ${METHOD} missing" >&2
498 if [[ -z "${NETMASK}" ]]; then
499 echo "Mandatory parameter NETMASK for method ${METHOD} missing" >&2
502 if [[ "${missed}" -gt 0 ]]; then
503 echo "Can not proceed, exiting..." >&2
512 if [ -z "${WLAN[*]}" ] ; then
513 WLAN_OLD=( $(tail -n +3 /proc/net/wireless 2>/dev/null|awk -F: '{print $1}') )
514 # /proc/net/wireless does not work e.g. with iwl3945 on kernel 2.6.33 anymore
515 WLAN_NEW=( $(for i in /sys/class/net/* ; do ( [ -d "$i/wireless" ] || [ -d "$i/phy80211" ] ) && basename "$i" ; done) )
518 iwconfig "${DEV}" 2>/dev/null 1>&2 && WLAN_IWCONFIG+=("${DEV}")
520 WLAN=( $(echo "${WLAN_OLD[@]}" "${WLAN_NEW[@]}" "${WLAN_IWCONFIG[@]}" | tr ' ' '\n' | sort -u) )
523 unset LAN_DEVICES WLAN_DEVICES FIREWIRE_DEVICES NETDEVICES WLAN_IWCONFIG
525 while read -r dev mac; do
526 #echo "Making NETDEVICES $NETDEVICESCOUNT $dev"
527 iswlan=$(echo "$dev" "${WLAN[@]}"|tr ' ' '\n'|sort|uniq -d)
529 grep auto /etc/network/interfaces | grep -q "$dev" && isauto="1"
530 driver=$(ethtool -i "$dev" 2>/dev/null|awk '/^driver:/{print $2}')
531 if [ "$driver" ]; then
532 if [ "$iswlan" ]; then
533 NETDEVICES[$NETDEVICESCOUNT]="$dev A::$isauto M::$mac D::$driver W::1 F::0"
535 NETDEVICES[$NETDEVICESCOUNT]="$dev A::$isauto M::$mac D::$driver W::0 F::0"
538 if [ "$iswlan" ]; then
539 NETDEVICES[$NETDEVICESCOUNT]="$dev A::$isauto M::$mac W::1 F::0"
541 NETDEVICES[$NETDEVICESCOUNT]="$dev A::$isauto M::$mac W::0 F::0"
544 #echo "Made to ${NETDEVICES[$NETDEVICESCOUNT]}"
545 ((NETDEVICESCOUNT++))
546 done < <(ip link show | awk ' !/^\s/{sub(/:/,"",$2);IFACE=$2} /link.ether/{if (IFACE !~/^vmnet/) print IFACE" "$2}')
548 if [ "$(ethtool -i "${dev}" 2>/dev/null|awk '/^bus-info:/{print $2}')" == "ieee1394" ]; then
550 grep auto /etc/network/interfaces | grep -q "${dev}" && isauto="1"
551 NETDEVICES[$NETDEVICESCOUNT]="${dev} A::${isauto} D::$(ethtool -i "${dev}" 2>/dev/null|awk '/^driver:/{print $2}') W::0 F::1"
552 ((NETDEVICESCOUNT++))
556 #NETDEVICES="$(cat /proc/net/dev | awk -F: '/eth.:|lan.:|tr.:|wlan.:|ath.:|ra.:/{print $1}')"
558 if [ -z "${NETDEVICES[*]}" ]; then
559 $DIALOG --msgbox "$MESSAGE0" 15 45
563 count="$NETDEVICESCOUNT"
565 if [ "$count" -gt 1 ]; then
568 while [ $mycount -lt $count ]; do
569 DEVICE=${NETDEVICES[$mycount]}
570 #echo "$mycount is $DEVICE"
572 #echo "name: $DEVICENAME auto: $isauto fw: $isfirewire mac: $mac driver: $driver"
574 DEVICELIST=( "${DEVICELIST[@]}" "$mycount" "$MY_DEVICE_NAME" )
581 EXITMENU=( $EXITKEY Exit )
583 # main program loop until they bailout
585 # first get the device
586 if [ "$count" -gt 1 ]; then
587 if "${INTERACTIVE}" ; then
589 $DIALOG --menu "$MESSAGE1" 18 60 12 "${DEVICELIST[@]}" "${EXITMENU[@]}" 2>"$TMP" || bailout
590 read -r DV <"$TMP" ; rm -f "$TMP"
592 # we have interface name so we need to find its number in NETDEVICES
595 for DV in "${!NETDEVICES[@]}"; do
596 if [[ "${NETDEVICES[$DV]}" =~ ^"${NET_DEV} " ]]; then
601 if ! "${found}"; then
602 echo "There is no interface ${NET_DEV} in the system" >&2
606 [[ -z "${IFACEDONE}" ]] || bailout
608 [ "$DV" = "$EXITKEY" ] && bailout
612 # they have asked to stop configuring the interface so exit
613 [ -z "$IFACEDONE" ] || bailout
617 while [ -n "$DV" ] && [ -z "$IFACEDONE" ]; do
618 configiface "${METHOD}" "${IPADDR}" "${NETMASK}" "${GATEWAY}" "${DNS}"
620 if "${INTERACTIVE}" ; then
621 $DIALOG --yesno "$MESSAGE14" 15 50 || IFACEDONE="DONE"
623 echo "${MESSAGE14}" >&2
627 if "${INTERACTIVE}" ; then
628 if $DIALOG --yesno "$MESSAGE15" 12 50 ; then
641 ## END OF FILE #################################################################