OK, so the Right Thing is ro run DHCP. However it is not always reasonable to demand that someone set up a whole new service when you want to plug in your laptop to swap some files. On the other hand you don't want to overwrite your current rc.conf settings.
The solution here is to try to ping a local machine (ie on the same network segment) whose presence will determine th network you are on.
Here is an extract from rc.conf. The pccard_ether_configs line lists places you might plug the machine in, then for each there is an address, a default router and a test machine (which defaults to the default router).
network_interfaces="lo0" pccard_ether_configs="home cstr l3g" ifconfig_home="inet 10.0.0.8" testmachine_home="10.0.0.2" dns_home="10.0.0.2" ifconfig_cstr="inet 129.215.91.97" defaultrouter_cstr="129.215.91.1" dns_cstr="129.215.91.1" ifconfig_3lg="inet 192.168.10.11" defaultrouter_3lg="192.168.10.1" dns_3lg="192.168.10.1" ifconfig_ep0="10.0.0.8" #defaultrouter_ep0="10.0.0.7" testmachine_ep0="10.0.0.2" ifconfig_ed0="dhcp" #ifconfig_ed0="10.0.0.8" #defaultrouter_ed0="10.0.0.7" #testmachine_ed0="10.0.0.2" ifconfig_lp0="inet 11.0.0.100 11.0.0.200" ifconfig_lo0="inet 127.0.0.1" ifconfig_default="dhcp" defaultrouter_default="10.0.0.2"
And here is my version of /etc/pccard_ether.
When a card is inserted it checks each config to see if the test machine is there. Just for the fun of it it prints stuff in colour to say what it is doing, the effect is somewhat lost at boot time as there are lots of other things printing at the same time, but good for testing.
When a card is removed it uses a file created on insertion to know what to undo.
#!/bin/sh - # # $FreeBSD: src/etc/pccard_ether,v 1.15.2.10 2001/09/14 17:28:11 imp Exp $ # # pccard_ether interfacename [start|stop] [ifconfig option] # # example: pccard_ether ep0 start -link0 # cecho_n() { clr=$1 shift echo -n "[${clr}m$*[m" >&2 } cecho() { cecho_n "$@" echo >&2 } select_config() { inf=$1 shift for c in "$@" do eval "ifc=\$ifconfig_$c" eval "dr=\$defaultrouter_$c" eval "tm=\$testmachine_$c" eval "dns=\$dns_$c" if [ "$ifc" = dhcp ] then cecho 32 DHCP always OK echo $c break; fi [ -n "$tm" ] || tm="$dr" [ -n "$tm" ] || continue cecho_n 1 "try config $c [$ifc] [$tm] [$dr] [$dns] " ifconfig $inf $ifc /sbin/ping -t 2 -c 1 $tm >/dev/null if /sbin/ping -t 2 -c 1 $tm >/dev/null then cecho 32 YES echo $c break else cecho 31 NO fi done } stop_dhcp() { if [ -s /var/run/dhclient.${interface}.pid ]; then pidfile="/var/run/dhclient.${interface}.pid" elif [ -s /var/run/dhcpc.${interface}.pid ]; then pidfile="/var/run/dhcpc.${interface}.pid" else return fi cecho 1 stop dhcp `cat ${pidfile}` kill `cat ${pidfile}` rm -f ${pidfile} } start_dhcp() { stop_dhcp if [ -x "${dhcp_program}" ]; then if [ `basename ${dhcp_program}` = "dhclient" ]; then pidfile="/var/run/dhclient.${interface}.pid" dhcp_flags="${dhcp_flags} -pf ${pidfile}" fi cecho 1 start $dhcp_program ${dhcp_program} ${dhcp_flags} ${interface} else cecho 31 "${dhcp_program}: DHCP client software not available" fi } # Suck in the configuration variables # if [ -r /etc/defaults/rc.conf ]; then . /etc/defaults/rc.conf source_rc_confs elif [ -r /etc/rc.conf ]; then . /etc/rc.conf fi exec >/dev/console 2>&1 interface=$1 shift startstop=$1 shift case ${pccard_ether_delay} in [Nn][Oo]) ;; [0-9]) sleep ${pccard_ether_delay} ;; *) # Default until it has had a chance to make it to /etc/defaults/rc.conf sleep 5 ;; esac case ${pccard_ifconfig} in [Nn][Oo] | '') # if expr "${removable_interfaces}" : ".*${interface}" > /dev/null # then # : # else # cecho 31 "Not a removable interface" # exit 0 # fi ;; *) ;; esac case $startstop in start ) config=`select_config $interface $interface $pccard_ether_configs pccard`;; stop ) config=`cat /var/run/pccard_ether_$interface`;; esac if [ -z "$config" ] then cecho 33 'NO CONFIG FOUND' eval "ifconfig_args=\$ifconfig_default" eval "defaultrouter=\$defaultrouter_default" eval "dns=\$dns_default" else [ $startstop = start ] && cecho 32 Configure for $config [ $startstop = stop ] && cecho 32 Shutdown for $config eval "ifconfig_args=\$ifconfig_$config" eval "defaultrouter=\$defaultrouter_$config" eval "dns=\$dns_$config" fi case ${startstop} in [Ss][Tt][Aa][Rr][Tt] | '') echo $config > /var/run/pccard_ether_$interface if [ -r /etc/start_if.${interface} ]; then . /etc/start_if.${interface} fi case ${ifconfig_args} in [Nn][Oo] | '') ;; [Dd][Hh][Cc][Pp]) # Start up the DHCP client program start_dhcp ;; *) # Do the primary ifconfig if specified ifconfig ${interface} ${ifconfig_args} $* # Check to see if aliases need to be added alias=0 while : do eval ifx_args=\$ifconfig_${interface}_alias${alias} if [ -n "${ifx_args}" ]; then ifconfig ${interface} ${ifx_args} alias alias=`expr ${alias} + 1` else break; fi done # Do ipx address if specified eval ifx_args=\$ifconfig_${interface}_ipx if [ -n "${ifx_args}" ]; then ifconfig ${interface} ${ifx_args} fi # Add default route into $static_routes case ${defaultrouter} in [Nn][Oo] | '') ;; *) static_routes="default ${static_routes}" route_default="default ${defaultrouter}" ;; esac # Add private route for this interface into $static_routes eval ifx_routes=\$static_routes_${interface} if [ -n "${ifx_routes}" ]; then static_routes="${ifx_routes} ${static_routes}" fi # Set up any static routes if specified if [ -n "${static_routes}" ]; then for i in ${static_routes}; do eval route_args=\$route_${i} route add ${route_args} done fi ;; esac # IPv6 setup case ${ipv6_enable} in [Yy][Ee][Ss]) if [ -r /etc/rc.network6 ]; then . /etc/rc.network6 network6_interface_setup ${interface} fi ;; esac ;; # Stop the interface *) if [ -r /etc/stop_if.${interface} ]; then . /etc/stop_if.${interface} fi eval ifconfig_args=\$ifconfig_${interface} case ${ifconfig_args} in [Nn][Oo] | '') ;; [Dd][Hh][Cc][Pp]) # Stop the DHCP client for this interface stop_dhcp ;; *) # Delelte static route if specified eval ifx_routes=\$static_routes_${interface} if [ -n "${ifx_routes}" ]; then for i in ${ifx_routes}; do eval route_args=\$route_${i} route delete ${route_args} done fi # Delete aliases if exist alias=0 while : do eval ifx_args=\$ifconfig_${interface}_alias${alias} if [ -n "${ifx_args}" ]; then ifconfig ${interface} ${ifx_args} alias delete alias=`expr ${alias} + 1` else break; fi done ;; esac # Remove the network interface and clean the ARP table ifconfig ${interface} delete arp -d -a ;; esac case "$dns" in [Nn][Oo] | '' ) : ;; * ) awk '$3 == "FORWARD" {$1= addr ";"} {print}' addr=$dns \ < /etc/namedb/named.conf > /etc/namedb/named.conf.x mv /etc/namedb/named.conf.x /etc/namedb/named.conf cecho 1 Restart named /usr/sbin/named.restart >/dev/null ;; esac case "$natd_enable" in [Yy][Ee][Ss] ) if [ "$interface" = "$natd_interface" ] then # cecho 1 Restart natd # kill -9 `cat /var/run/natd.pid` # natd -n $natd_interface $natd_flags : fi;; esac
Notes:
See Also: