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 $DIALOG --inputbox "$MESSAGE9" 10 45 "${DG:-${IP%.*}.1}" 2>"$TMP"
414 read -r DG <"$TMP" ; rm -f "$TMP"
416 if [ -f "/etc/resolv.conf" ]; then
417 NS="$(awk '/^nameserver/{printf "%s ",$2}' /etc/resolv.conf)"
420 $DIALOG --inputbox "$MESSAGE10" 10 45 "${NS:-$DG}" 2>"$TMP"
421 read -r NS <"$TMP" ; rm -f "$TMP"
424 if [ -w /etc/network/interfaces ]; then
429 # ensure we don't leak the IWOURLINE into the
430 # following awk command line, if we are configuring
431 # a device without wireless capabilities
436 awk '/iface/{if(/'"$DV"'/){found=1}else{found=0}}
438 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"}' \
439 /etc/network/interfaces >"$TMP"
441 cat "$TMP" >/etc/network/interfaces
444 # Add an "auto" entry
448 fi # Use DHCP broadcast?
454 # get persistent interface names across reboots
456 # make sure we don't run the dhcp stuff when using /etc/network/interfaces
462 NET_DEVICE_NAME="Network_device"
463 NET_DEVICE_NAME_W="Wireless_device"
464 NET_DEVICE_NAME_FW="Firewire_device"
465 NET_DEVICE_NAME_AUTO="Auto"
466 MESSAGE0="No supported network cards found."
467 MESSAGE1="Please select network device"
468 MESSAGE2="Use DHCP broadcast?"
469 MESSAGE6="Please enter IP Address for"
470 MESSAGE7="Please enter Network Mask for"
471 MESSAGE8="Please enter Broadcast Address for"
472 MESSAGE9="Please enter Default Gateway"
473 MESSAGE10="Please enter Nameserver(s)"
474 MESSAGE13="Setup wireless options?"
475 MESSAGE14="Failed to bring up the interface, would you like to reconfigure it?"
476 MESSAGE15="Interface enabled, do you want it auto enabled at boot?"
477 MESSAGE16="Configure VLAN on this interface?"
478 MESSAGE17="Please enter VLAN id for"
479 MESSAGEW4="Enter the ESSID for"
480 MESSAGEW5="\n(empty for 'any', not recommended!)\n"
481 MESSAGEW14="Enter the encryption key\nfor"
482 MESSAGEW15="\n\n(empty for cleartext, not recommended !!)"
483 MESSAGEW22="Enable WPA support?"
484 MESSAGEW23="Enter the WPA passphrase (passphrase must be 8..63 characters) for"
485 MESSAGEW25="Would you like to store your wep key in it's own private file ("
486 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"
487 MESSAGEWSCAN1="Scan for available wireless networks?"
488 MESSAGEWSCAN2="Please choose a wireless network from below:"
490 NET_DEV="${NET_DEV:-}"
491 METHOD="${METHOD,,:-}"
493 NETMASK="${NETMASK:-}"
494 GATEWAY="${GATEWAY:-}"
499 if [ -r /proc/net/dev ] ; then
500 LAN=$(tail -n +3 /proc/net/dev | awk -F: '{print $1}'| sed "s/\s*//" | grep -v -e ^lo -e ^vmnet | sort)
505 if [[ -n "${NET_DEV}" ]] && [[ -n "${METHOD}" ]]; then
506 if [[ "${METHOD}" != 'dhcp' ]]; then
508 if [[ -z "${IPADDR}" ]]; then
509 echo "Mandatory parameter IPADDR for method ${METHOD} missing" >&2
512 if [[ -z "${NETMASK}" ]]; then
513 echo "Mandatory parameter NETMASK for method ${METHOD} missing" >&2
516 if [[ "${missed}" -gt 0 ]]; then
517 echo "Can not proceed, exiting..." >&2
526 if [ -z "${WLAN[*]}" ] ; then
527 WLAN_OLD=( $(tail -n +3 /proc/net/wireless 2>/dev/null|awk -F: '{print $1}') )
528 # /proc/net/wireless does not work e.g. with iwl3945 on kernel 2.6.33 anymore
529 WLAN_NEW=( $(for i in /sys/class/net/* ; do ( [ -d "$i/wireless" ] || [ -d "$i/phy80211" ] ) && basename "$i" ; done) )
532 iwconfig "${DEV}" 2>/dev/null 1>&2 && WLAN_IWCONFIG+=("${DEV}")
534 WLAN=( $(echo "${WLAN_OLD[@]}" "${WLAN_NEW[@]}" "${WLAN_IWCONFIG[@]}" | tr ' ' '\n' | sort -u) )
537 unset LAN_DEVICES WLAN_DEVICES FIREWIRE_DEVICES NETDEVICES WLAN_IWCONFIG
539 while read -r dev mac; do
540 #echo "Making NETDEVICES $NETDEVICESCOUNT $dev"
541 iswlan=$(echo "$dev" "${WLAN[@]}"|tr ' ' '\n'|sort|uniq -d)
543 grep auto /etc/network/interfaces | grep -q "$dev" && isauto="1"
544 driver=$(ethtool -i "$dev" 2>/dev/null|awk '/^driver:/{print $2}')
545 if [ "$driver" ]; then
546 if [ "$iswlan" ]; then
547 NETDEVICES[$NETDEVICESCOUNT]="$dev A::$isauto M::$mac D::$driver W::1 F::0"
549 NETDEVICES[$NETDEVICESCOUNT]="$dev A::$isauto M::$mac D::$driver W::0 F::0"
552 if [ "$iswlan" ]; then
553 NETDEVICES[$NETDEVICESCOUNT]="$dev A::$isauto M::$mac W::1 F::0"
555 NETDEVICES[$NETDEVICESCOUNT]="$dev A::$isauto M::$mac W::0 F::0"
558 #echo "Made to ${NETDEVICES[$NETDEVICESCOUNT]}"
559 ((NETDEVICESCOUNT++))
560 done < <(ip link show | awk ' !/^\s/{sub(/:/,"",$2);IFACE=$2} /link.ether/{if (IFACE !~/^vmnet/) print IFACE" "$2}')
562 if [ "$(ethtool -i "${dev}" 2>/dev/null|awk '/^bus-info:/{print $2}')" == "ieee1394" ]; then
564 grep auto /etc/network/interfaces | grep -q "${dev}" && isauto="1"
565 NETDEVICES[$NETDEVICESCOUNT]="${dev} A::${isauto} D::$(ethtool -i "${dev}" 2>/dev/null|awk '/^driver:/{print $2}') W::0 F::1"
566 ((NETDEVICESCOUNT++))
570 #NETDEVICES="$(cat /proc/net/dev | awk -F: '/eth.:|lan.:|tr.:|wlan.:|ath.:|ra.:/{print $1}')"
572 if [ -z "${NETDEVICES[*]}" ]; then
573 $DIALOG --msgbox "$MESSAGE0" 15 45
577 count="$NETDEVICESCOUNT"
579 if [ "$count" -gt 1 ]; then
582 while [ $mycount -lt $count ]; do
583 DEVICE=${NETDEVICES[$mycount]}
584 #echo "$mycount is $DEVICE"
586 #echo "name: $DEVICENAME auto: $isauto fw: $isfirewire mac: $mac driver: $driver"
588 DEVICELIST=( "${DEVICELIST[@]}" "$mycount" "$MY_DEVICE_NAME" )
595 EXITMENU=( $EXITKEY Exit )
597 # main program loop until they bailout
599 # first get the device
600 if [ "$count" -gt 1 ]; then
601 if "${INTERACTIVE}" ; then
603 # Unblock possibly locked devices?
604 if [ $iswireless -gt 0 ] && check_for_blocked_devices; then
605 RF_INFO=$(rfkill | sed 's/$/\\n/') # ensure we have a proper newline layout within dialog
606 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
612 $DIALOG --menu "$MESSAGE1" 18 60 12 "${DEVICELIST[@]}" "${EXITMENU[@]}" 2>"$TMP" || bailout
613 read -r DV <"$TMP" ; rm -f "$TMP"
615 # we have interface name so we need to find its number in NETDEVICES
618 for DV in "${!NETDEVICES[@]}"; do
619 if [[ "${NETDEVICES[$DV]}" =~ ^"${NET_DEV} " ]]; then
624 if ! "${found}"; then
625 echo "There is no interface ${NET_DEV} in the system" >&2
629 [[ -z "${IFACEDONE}" ]] || bailout
631 [ "$DV" = "$EXITKEY" ] && bailout
635 # they have asked to stop configuring the interface so exit
636 [ -z "$IFACEDONE" ] || bailout
640 while [ -n "$DV" ] && [ -z "$IFACEDONE" ]; do
641 configiface "${METHOD}" "${IPADDR}" "${NETMASK}" "${GATEWAY}" "${DNS}"
643 if "${INTERACTIVE}" ; then
644 $DIALOG --yesno "$MESSAGE14" 15 50 || IFACEDONE="DONE"
646 echo "${MESSAGE14}" >&2
650 if "${INTERACTIVE}" ; then
651 if $DIALOG --yesno "$MESSAGE15" 12 50 ; then
664 ## END OF FILE #################################################################