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 check_for_blocked_devices() {
28 if ! command -v rfkill &>/dev/null ; then
29 echo "Warning: rfkill executable not present, skipping check for blocked devices."
33 # if we detect any blocked devices (SOFT or HARD) then report
34 if rfkill | grep -w 'blocked' ; then
41 # This function produces the IWOURLINE for interfaces
45 if [ -n "$KEY" ]; then
46 if [ "$PUBKEY" -eq 1 ]; then
47 # Store the key in interfaces in wireless-key
48 IWOURLINE="$IWOURLINE\twireless-key $KEY\n"
50 # Store the key in /etc/network/wep.$DV which is root readable only
51 # Use pre-up in interfaces to read and set it
52 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"
56 for mod in /sys/module/rt2??0/ ; do
57 if [ -d "$mod" ]; then
58 IWPREUPLINE="$IWPREUPLINE\tpre-up /sbin/ip link set $DV up\n"
63 # execute ESSID last, but make sure that it is written as first option
64 if [ -n "$ESSID" ]; then
65 IWOURLINE="$IWOURLINE\twireless-essid $ESSID\n"
68 if [ "$WPAON" -gt 0 ]; then
69 IWOURLINE="$IWOURLINE\twpa-ssid $ESSID\n\twpa-psk $WPASECRET\n"
72 IWOURLINE="$IWOURLINE $IWPREUPLINE"
73 #echo "DEBUG: for interfaces $IWOURLINE"
76 generate_udev_entry() {
78 mkdir -p /etc/udev/scripts
79 echo "# Auto generated script from netcardconfig on $(date)
80 # Executing this script generates an entry in /etc/udev/rules.d/z25_persistent-net.rules
81 # for you, please check z25_persistent-net.rules for existing entries before
82 # running this script (once more)." > /etc/udev/scripts/netcardconfig
83 for interface in $(ip -oneline link | awk '!/ lo: / {print $2}' | sed 's/:$//; s/@.*//') ; do
84 echo -n "INTERFACE=$interface /lib/udev/write_net_rules " >> /etc/udev/scripts/netcardconfig && \
85 if which udevadm >/dev/null 2>&1; then
86 udevadm info -a -p "/sys/class/net/$interface" | awk -F'==' '/address/ {print $2}' >> /etc/udev/scripts/netcardconfig
88 udevinfo -a -p "/sys/class/net/$interface" | awk -F'==' '/address/ {print $2}' >> /etc/udev/scripts/netcardconfig
91 # send errors to /dev/null as well because the sed line inside the /lib/udev/write_net_rules
92 # script outputs a sed warning which might unsettle users
93 chmod +x /etc/udev/scripts/netcardconfig && /etc/udev/scripts/netcardconfig 1>/dev/null 2>&1
96 disable_config_dhcp() {
97 if grep -q CONFIG_DHCP /etc/grml/autoconfig ; then
98 sed -i "s|^CONFIG_DHCP.*|CONFIG_DHCP='no'|" /etc/grml/autoconfig
109 for PART in $DEVICE; do
110 if [ $PARTCOUNT -eq 0 ]; then
113 echo "$PART" | grep -q A::1 && isauto=1
114 echo "$PART" | grep -q F::1 && isfirewire=1
115 echo "$PART" | grep -q W::1 && iswireless=1
116 [ -z "$driver" ] && driver=$(echo "$PART"|awk 'BEGIN {FS="::"} /^D:/{print $2}')
117 [ -z "$mac" ] && mac=$(echo "$PART"|awk 'BEGIN {FS="::"} /^M:/{print $2}')
125 [ $isfirewire -gt 0 ] && MY_DEVICE_NAME="$NET_DEVICE_NAME_FW"
126 [ -z "$MY_DEVICE_NAME" ] && [ $iswireless -gt 0 ] && MY_DEVICE_NAME="$NET_DEVICE_NAME_W"
127 [ -z "$MY_DEVICE_NAME" ] && MY_DEVICE_NAME="$NET_DEVICE_NAME"
128 MY_DEVICE_NAME="$DEVICENAME $MY_DEVICE_NAME $mac $driver"
129 [ $isauto -gt 0 ] && MY_DEVICE_NAME="$MY_DEVICE_NAME $NET_DEVICE_NAME_AUTO"
130 MY_DEVICE_NAME="${MY_DEVICE_NAME// /__}"
134 if ! grep -E "^auto .*$DV" /etc/network/interfaces >/dev/null; then
135 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"
136 cat "$TMP" > /etc/network/interfaces
141 if grep -e "^auto .*$DV" /etc/network/interfaces >/dev/null; then
142 perl -pi -e 's/^(auto.*)'"$DV"'(.*)$/$1$2/;' /etc/network/interfaces
149 iwlist "$DV" scanning | grep "ESSID\|Quality" | sed -e "s/^.*ESSID:\"\|\"$//g" | tac > "$TMP"
161 if [[ -n "${METHOD}" ]] && [[ "${METHOD}" != 'dhcp' ]]; then
174 [ ! -r /etc/network/interfaces ] && touch /etc/network/interfaces
175 DEVICE=${NETDEVICES[$DV]}
178 ifdown "${DV}" --force
181 if "${INTERACTIVE}" ; then
182 # Setup wireless options?
183 if [ $iswireless -gt 0 ] && $DIALOG --yesno "$MESSAGE13" 8 45; then
184 WLDEVICECOUNT="$(LANG=C LC_MESSAGEWS=C iwconfig "$DV" 2>/dev/null | wc -l)"
188 if [ -f /etc/network/interfaces ]; then
189 awk '/iface/{if(/'"$DV"'/){found=1}else{found=0}}
190 /essid/{if(found){for(i=NF;i>=2;i--)essid=$i "~" essid}}
191 /wireless[-_]key/{if(found){gsub(/^\W*wireless[-_]key\W*/,"");key=$0}}
193 if (!(length(essid))){essid="~~~"}
194 if (!(length(key))){key="~~~"}
196 }' /etc/network/interfaces >"$TMP"
198 read -r ESSID KEY<"$TMP"
200 [[ "$ESSID" =~ ^~~~$ ]] && ESSID=""
201 [[ "$KEY" =~ ^~~~$ ]] && KEY=""
203 # shellcheck disable=SC2088
204 ESSID=$(echo $ESSID | tr '~' " " | sed 's/ *$//')
206 if [ -z "$KEY" ]; then
207 KEY=$(cat "/etc/network/wep.$DV" 2>/dev/null)
209 if [ -z "$KEY" ]; then
218 #echo "DEBUG:E:$ESSID K:$KEY "
222 # ask to scan or manually set wireless network
223 if $DIALOG --yesno "$MESSAGEWSCAN1" 8 45 ; then
226 while [ $RESCAN -eq 1 ]
229 $DIALOG --extra-button --extra-label "Rescan" --menu "$MESSAGEWSCAN2" 20 70 15 "${WARRAY[@]}" 2>"$TMP"
242 read -r ESSID <"$TMP" ; rm -f "$TMP"
245 $DIALOG --inputbox "$MESSAGEW4 $DEVICENAME $MESSAGEW5" 15 50 "$ESSID" 2>"$TMP" || bailout 1
246 read -r ESSID <"$TMP" ; rm -f "$TMP"
247 [ -z "$ESSID" ] && ESSID="any"
257 ipw2200|ipw2100|ipw3945)
268 if [ -z "$WPA_DEV" ]; then
269 if [ -d "/proc/net/ndiswrapper/$DV" ]; then
271 elif [ -d "/proc/net/hostap/$DV" ]; then
273 elif [ "$WLDEVICECOUNT" -eq 1 ]; then
274 if [ -e /proc/driver/atmel ]; then
282 if [ -n "$WPA_DEV" ]; then
283 if $DIALOG --yesno "$MESSAGEW22" 15 50; then
286 # bssid 00:11:22:33:44:55
287 # priority [0]|Integer
288 # proto [WPA RSN] WPA|RSN
289 # key_mgmt [WPA-PSK WPA-EAP]|NONE|WPA-PSK|WPA-EAP|IEEE8021X
290 # pairwise [CCMP TKIP]|CCMP|TKIP|NONE
291 # group [CCMP TKIP WEP105 WEP40]|CCMP|TKIP|WEP105|WEP40
292 # eapol_flags [3]|1|2
308 if (gsub(/^\W*wpa-psk\W*/,"")){
315 ' /etc/network/interfaces)
316 FIRST_RUN=0 # show the wpasecret input box at least once
317 while ( [ -z "$WPASECRET" ] || [ "$FIRST_RUN" ] ) ; do
318 $DIALOG --inputbox "$MESSAGEW23 $ESSID" 15 50 "$WPASECRET" 2>"$TMP" || bailout 1
319 read -r WPASECRET <"$TMP"
320 if [ -z "$WPASECRET" ] ; then
321 $DIALOG --msgbox "Sorry, empty password not allowed, please retry." 0 0 || bailout 1
334 # No need for a wep key if we are using wpa
335 if [ ! $WPAON -eq 1 ]; then
336 $DIALOG --inputbox "$MESSAGEW14 $DEVICENAME $MESSAGEW15" 15 50 "$KEY" 2>"$TMP" || bailout 1
337 read -r KEY <"$TMP" ; rm -f "$TMP"
339 if [ -n "$KEY" ] && [ "$PUBKEY" -eq 0 ]; then
340 if ! $DIALOG --yesno "$MESSAGEW25 $DEVICENAME $MESSAGEW26" 15 50; then
347 if [ "$iswireless" = "1" ] ; then
351 fi # Setup wireless options?
353 # Configure VLAN on this interface?
354 if $DIALOG --defaultno --yesno "$MESSAGE16" 8 45; then
355 $DIALOG --inputbox "$MESSAGE17 $DV" 10 45 2>"$TMP" || bailout 1
356 read -r VLAN <"$TMP" ; rm -f "$TMP"
357 if [ -n "$VLAN" ]; then
358 modprobe 8021q # avoid warning that VLAN support isn't present yet
363 fi # INTERACTIVE=true
365 # Use DHCP broadcast?
366 if ("${INTERACTIVE}" && "${DIALOG}" --yesno "${MESSAGE2}" 8 45) || \
367 (! "${INTERACTIVE}" && [[ "${METHOD}" == 'dhcp' ]]) ; then
368 if [ -w /etc/network/interfaces ]; then
370 awk '/iface/{if(/'"$DV"'/){found=1}else{found=0}}
371 /^\W$/{if(blank==0){lastblank=1}else{lastblank=0}{blank=1}}
372 /\w/{blank=0;lastblank=0}
373 {if(!(found+lastblank)){print}}
374 END{print "iface '"$DV"' inet dhcp";if("'"$PDV"'"!=""){print "\tvlan-raw-device '"$PDV"'"}}' \
375 /etc/network/interfaces >"$TMP"
378 # write wireless configuration only when we are dealing with a wireless device,
379 # it might be set from a previous configuration run of a wireless device
380 # while the currently configured network device isn't a wireless device
381 echo -e "$IWOURLINE" >> "$TMP"
384 #echo -e "\n\n" >> $TMP
385 cat "$TMP" >/etc/network/interfaces
387 # Add an "auto" entry
391 if "${INTERACTIVE}" ; then
392 if [ -f /etc/network/interfaces ]; then
393 awk '/iface/{if(/'"$DV"'/){found=1}else{found=0}}
394 /address/{if(found){address=$NF}}
395 /netmask/{if(found){netmask=$NF}}
396 /broadcast/{if(found){broadcast=$NF}}
397 /gateway/{if(found){gateway=$NF}}
398 /dns-nameservers/{if(found){dnsnameservers=$NF}}
399 END{print address" "netmask" "broadcast" "gateway" "dnsnameservers}' /etc/network/interfaces >"$TMP"
400 read -r IP NM BC DG NS <"$TMP"
404 $DIALOG --inputbox "$MESSAGE6 $DV" 10 45 "${IP:-192.168.0.1}" 2>"$TMP" || bailout 1
405 read -r IP <"$TMP" ; rm -f "$TMP"
407 $DIALOG --inputbox "$MESSAGE7 $DV" 10 45 "${NM:-255.255.255.0}" 2>"$TMP" || bailout 1
408 read -r NM <"$TMP" ; rm -f "$TMP"
410 $DIALOG --inputbox "$MESSAGE8 $DV" 10 45 "${BC:-${IP%.*}.255}" 2>"$TMP" || bailout 1
411 read -r BC <"$TMP" ; rm -f "$TMP"
413 # if we already have a default route, don't suggest to add another one as that might fail
415 if ! route -en | grep -q '^0.0.0.0' ; then
416 __default_gw="${DG:-${IP%.*}.1}"
418 $DIALOG --inputbox "$MESSAGE9" 10 45 "${__default_gw}" 2>"$TMP"
419 read -r DG <"$TMP" ; rm -f "$TMP"
421 if [ -f "/etc/resolv.conf" ]; then
422 NS="$(awk '/^nameserver/{printf "%s ",$2}' /etc/resolv.conf)"
425 $DIALOG --inputbox "$MESSAGE10" 10 45 "${NS:-$DG}" 2>"$TMP"
426 read -r NS <"$TMP" ; rm -f "$TMP"
429 if [ -w /etc/network/interfaces ]; then
434 # ensure we don't leak the IWOURLINE into the
435 # following awk command line, if we are configuring
436 # a device without wireless capabilities
441 awk '/iface/{if(/'"$DV"'/){found=1}else{found=0}}
443 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"}' \
444 /etc/network/interfaces >"$TMP"
446 cat "$TMP" >/etc/network/interfaces
449 # Add an "auto" entry
453 fi # Use DHCP broadcast?
459 # get persistent interface names across reboots
461 # make sure we don't run the dhcp stuff when using /etc/network/interfaces
467 NET_DEVICE_NAME="Network_device"
468 NET_DEVICE_NAME_W="Wireless_device"
469 NET_DEVICE_NAME_FW="Firewire_device"
470 NET_DEVICE_NAME_AUTO="Auto"
471 MESSAGE0="No supported network cards found."
472 MESSAGE1="Please select network device"
473 MESSAGE2="Use DHCP broadcast?"
474 MESSAGE6="Please enter IP Address for"
475 MESSAGE7="Please enter Network Mask for"
476 MESSAGE8="Please enter Broadcast Address for"
477 MESSAGE9="Please enter Default Gateway"
478 MESSAGE10="Please enter Nameserver(s)"
479 MESSAGE13="Setup wireless options?"
480 MESSAGE14="Failed to bring up the interface, would you like to reconfigure it?"
481 MESSAGE15="Interface enabled, do you want it auto enabled at boot?"
482 MESSAGE16="Configure VLAN on this interface?"
483 MESSAGE17="Please enter VLAN id for"
484 MESSAGEW4="Enter the ESSID for"
485 MESSAGEW5="\n(empty for 'any', not recommended!)\n"
486 MESSAGEW14="Enter the encryption key\nfor"
487 MESSAGEW15="\n\n(empty for cleartext, not recommended !!)"
488 MESSAGEW22="Enable WPA support?"
489 MESSAGEW23="Enter the WPA passphrase (passphrase must be 8..63 characters) for"
490 MESSAGEW25="Would you like to store your wep key in it's own private file ("
491 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"
492 MESSAGEWSCAN1="Scan for available wireless networks?"
493 MESSAGEWSCAN2="Please choose a wireless network from below:"
495 NET_DEV="${NET_DEV:-}"
496 METHOD="${METHOD,,:-}"
498 NETMASK="${NETMASK:-}"
499 GATEWAY="${GATEWAY:-}"
504 if [ -r /proc/net/dev ] ; then
505 LAN=$(tail -n +3 /proc/net/dev | awk -F: '{print $1}'| sed "s/\s*//" | grep -v -e ^lo -e ^vmnet | sort)
510 if [[ -n "${NET_DEV}" ]] && [[ -n "${METHOD}" ]]; then
511 if [[ "${METHOD}" != 'dhcp' ]]; then
513 if [[ -z "${IPADDR}" ]]; then
514 echo "Mandatory parameter IPADDR for method ${METHOD} missing" >&2
517 if [[ -z "${NETMASK}" ]]; then
518 echo "Mandatory parameter NETMASK for method ${METHOD} missing" >&2
521 if [[ "${missed}" -gt 0 ]]; then
522 echo "Can not proceed, exiting..." >&2
531 if [ -z "${WLAN[*]}" ] ; then
532 WLAN_OLD=( $(tail -n +3 /proc/net/wireless 2>/dev/null|awk -F: '{print $1}') )
533 # /proc/net/wireless does not work e.g. with iwl3945 on kernel 2.6.33 anymore
534 WLAN_NEW=( $(for i in /sys/class/net/* ; do ( [ -d "$i/wireless" ] || [ -d "$i/phy80211" ] ) && basename "$i" ; done) )
537 iwconfig "${DEV}" 2>/dev/null 1>&2 && WLAN_IWCONFIG+=("${DEV}")
539 WLAN=( $(echo "${WLAN_OLD[@]}" "${WLAN_NEW[@]}" "${WLAN_IWCONFIG[@]}" | tr ' ' '\n' | sort -u) )
542 unset LAN_DEVICES WLAN_DEVICES FIREWIRE_DEVICES NETDEVICES WLAN_IWCONFIG
544 while read -r dev mac; do
545 #echo "Making NETDEVICES $NETDEVICESCOUNT $dev"
546 iswlan=$(echo "$dev" "${WLAN[@]}"|tr ' ' '\n'|sort|uniq -d)
548 grep auto /etc/network/interfaces | grep -q "$dev" && isauto="1"
549 driver=$(ethtool -i "$dev" 2>/dev/null|awk '/^driver:/{print $2}')
550 if [ "$driver" ]; then
551 if [ "$iswlan" ]; then
552 NETDEVICES[$NETDEVICESCOUNT]="$dev A::$isauto M::$mac D::$driver W::1 F::0"
554 NETDEVICES[$NETDEVICESCOUNT]="$dev A::$isauto M::$mac D::$driver W::0 F::0"
557 if [ "$iswlan" ]; then
558 NETDEVICES[$NETDEVICESCOUNT]="$dev A::$isauto M::$mac W::1 F::0"
560 NETDEVICES[$NETDEVICESCOUNT]="$dev A::$isauto M::$mac W::0 F::0"
563 #echo "Made to ${NETDEVICES[$NETDEVICESCOUNT]}"
564 ((NETDEVICESCOUNT++))
565 done < <(ip link show | awk ' !/^\s/{sub(/:/,"",$2);IFACE=$2} /link.ether/{if (IFACE !~/^vmnet/) print IFACE" "$2}')
567 if [ "$(ethtool -i "${dev}" 2>/dev/null|awk '/^bus-info:/{print $2}')" == "ieee1394" ]; then
569 grep auto /etc/network/interfaces | grep -q "${dev}" && isauto="1"
570 NETDEVICES[$NETDEVICESCOUNT]="${dev} A::${isauto} D::$(ethtool -i "${dev}" 2>/dev/null|awk '/^driver:/{print $2}') W::0 F::1"
571 ((NETDEVICESCOUNT++))
575 #NETDEVICES="$(cat /proc/net/dev | awk -F: '/eth.:|lan.:|tr.:|wlan.:|ath.:|ra.:/{print $1}')"
577 if [ -z "${NETDEVICES[*]}" ]; then
578 $DIALOG --msgbox "$MESSAGE0" 15 45
582 count="$NETDEVICESCOUNT"
584 if [ "$count" -gt 1 ]; then
587 while [ $mycount -lt $count ]; do
588 DEVICE=${NETDEVICES[$mycount]}
589 #echo "$mycount is $DEVICE"
591 #echo "name: $DEVICENAME auto: $isauto fw: $isfirewire mac: $mac driver: $driver"
593 DEVICELIST=( "${DEVICELIST[@]}" "$mycount" "$MY_DEVICE_NAME" )
600 EXITMENU=( $EXITKEY Exit )
602 # main program loop until they bailout
604 # first get the device
605 if [ "$count" -gt 1 ]; then
606 if "${INTERACTIVE}" ; then
608 # Unblock possibly locked devices?
609 if [ $iswireless -gt 0 ] && check_for_blocked_devices; then
610 RF_INFO=$(rfkill | sed 's/$/\\n/') # ensure we have a proper newline layout within dialog
611 if $DIALOG --yesno "Disabled wireless devices detected:\n\n${RF_INFO}\n\nPlease ensure that any possibly existing hardware switches are disabled.\nDo you want to unblock all of them?" 0 0; then
617 $DIALOG --menu "$MESSAGE1" 18 60 12 "${DEVICELIST[@]}" "${EXITMENU[@]}" 2>"$TMP" || bailout
618 read -r DV <"$TMP" ; rm -f "$TMP"
620 # we have interface name so we need to find its number in NETDEVICES
623 for DV in "${!NETDEVICES[@]}"; do
624 if [[ "${NETDEVICES[$DV]}" =~ ^"${NET_DEV} " ]]; then
629 if ! "${found}"; then
630 echo "There is no interface ${NET_DEV} in the system" >&2
634 [[ -z "${IFACEDONE}" ]] || bailout
636 [ "$DV" = "$EXITKEY" ] && bailout
640 # they have asked to stop configuring the interface so exit
641 [ -z "$IFACEDONE" ] || bailout
645 while [ -n "$DV" ] && [ -z "$IFACEDONE" ]; do
646 configiface "${METHOD}" "${IPADDR}" "${NETMASK}" "${GATEWAY}" "${DNS}"
648 if "${INTERACTIVE}" ; then
649 $DIALOG --yesno "$MESSAGE14" 15 50 || IFACEDONE="DONE"
651 echo "${MESSAGE14}" >&2
655 if "${INTERACTIVE}" ; then
656 if $DIALOG --yesno "$MESSAGE15" 12 50 ; then
669 ## END OF FILE #################################################################