autoconf (2321B)
1 #!/bin/rc 2 # Find any working ethernet/wlan interface and 3 # configure it, with priority given to ethernet. 4 5 if (!~ `{id -u} 0) { 6 printf 'You should probably run this script as root.\n' >[1=2] 7 exit 1 8 9 } 10 11 cfile = /etc/ip.rc 12 if (test -f $cfile) { 13 . $cfile 14 } 15 16 # Remove excess elements 17 ethip = $ethip(1) 18 wlanip = $wlanip(1) 19 20 gateway = 192.168.1.1 21 lanmask = 192.168.1.0/24 22 23 fn needeth { if (~ $ethip ()) { printf 'Ethernet ip: '; ethip = `$nl{head -n 1} } } 24 fn needwlan { if (~ $wlanip ()) { printf 'Wireless ip: '; wlanip = `$nl{head -n 1} } } 25 fn try { $* || exit $status(1) } 26 fn setup { 27 interface = $1 28 ip = $2 29 30 if (!~ `{ip addr show $interface | awk '$1 ~ /^inet/ {sub(/\/.*/, ""); print $2}'} $ip) { 31 printf 'Adding address %s to %s...\n' $ip $interface 32 try ip addr add $ip/24 brd + dev $interface || exit 1 33 } 34 35 printf 'Flushing default route...\n' 36 try ip route flush default 37 printf 'Configuring default route via %s\n' $gateway 38 try ip route add default dev $interface 39 try ip route change default via $gateway dev $interface 40 printf 'Flushing LAN route...\n' 41 try ip route flush $lanmask 42 printf 'Configuring LAN route from %s\n' $ip 43 try ip route add $lanmask dev $interface 44 try ip route change $lanmask src $ip dev $interface 45 46 printf 'Successfully configured %s with ip %s\n' $interface $ip 47 exit 0 48 } 49 50 printf 'Finding interfaces...' 51 interfaces = `$nl{ip a | awk '/^[0-9]/ {gsub(/:/, ""); print $2}'} 52 eth = () 53 wlan = () 54 55 for (i in $interfaces) { 56 if (~ $i en* || ~ $i eth*) { 57 eth = ($eth $i) 58 } else if (~ $i wl*) { 59 wlan = ($wlan $i) 60 } 61 } 62 63 printf 'Checking for ethernet interfaces...' 64 for (i in $eth) { 65 # Try to set the interface to up. 66 # If it doesn't work, `ip a` will say down. 67 ip link set dev $i up 68 69 if (~ `{ip addr show $i} UP) { 70 printf 'Found available ethernet interface: %s\n' $i 71 needeth 72 setup $i $ethip 73 } 74 } 75 76 printf 'Checking for wpa_supplicant...' 77 if (!sv check wpa_supplicant) { 78 if (!test -d /etc/sv/wpa_supplicant) { 79 exit 1 80 } 81 try ln -s /etc/sv/wpa_supplicant /var/service 82 try sv check wpa_supplicant 83 } 84 85 printf 'Checking for wireless interfaces...' 86 for (i in $wlan) { 87 ip link set dev $i up 88 89 if (~ `{ip addr show $i} UP) { 90 printf 'Found available wireless interface: %s\n' $i 91 needwlan 92 setup $i $wlanip 93 } 94 } 95 96 printf 'Could not find any interface to configure.\n' 97 exit 1