This can be easily done with NCD, the network configuration software I'm developing: http://code.google.com/p/badvpn/wiki/NCD
The following NCD program uses either static config or DHCP based on the value of a variable. To switch, you have to edit the NCD program and restart the NCD daemon. (but see below for automatic detection)
process lan {
# Network device name.
var("eth0") dev;
# Use static config or DHCP? Change to "true" for static config.
var("false") is_home;
# Wait for device and link.
net.backend.waitdevice(dev);
net.up(dev);
net.backend.waitlink(dev);
# Get configuration.
provide("lan_config");
depend("lan_config_done") config;
# Assign IP address.
net.ipv4.addr(dev, config.addr, config.addr_prefix);
# Add default route.
net.ipv4.route("0.0.0.0", "0", config.gateway, "20", dev);
# Configure DNS servers.
net.dns(config.dns_servers, "20");
}
process lan_config_home {
depend("lan_config") dep;
if(dep.is_home);
# Choose static configuration.
var("192.168.1.72") addr;
var("24") addr_prefix;
var("192.168.1.1") gateway;
list("192.168.1.1") dns_servers;
provide("lan_config_done");
}
process lan_config_other {
depend("lan_config") dep;
ifnot(dep.is_home);
# Do DHCP.
net.ipv4.dhcp(dep.dev) dhcp;
# Verify address.
ip_in_network(dhcp.addr, "127.0.0.0", "8") test_local;
ifnot(test_local);
# Alias configuration from DHCP.
var(dhcp.addr) addr;
var(dhcp.prefix) addr_prefix;
var(dhcp.gateway) gateway;
var(dhcp.dns_servers) dns_servers;
provide("lan_config_done");
}
The selection can also be automated by checking the MAC address of the DHCP server. The following NCD program always performs DHCP, but then it checks the DHCP server's MAC address against the address of the home router. If they match, it ignores the DHCP-obtained address and assigns a static address, else it assigns the DHCP address. (note: you need the latest NCD from the svn repo for this, I've just implemented the dhcp.server_mac
variable)
process lan {
# Network device name.
var("eth0") dev;
# MAC address of home router (6 two-digit caps hexadecimal values separated with colons).
var("00:AB:CD:EF:51:74") home_mac;
# Wait for device and link.
net.backend.waitdevice(dev);
net.up(dev);
net.backend.waitlink(dev);
# Do DHCP.
net.ipv4.dhcp(dev) dhcp;
# Are we home?
strcmp(dhcp.server_mac, home_mac) is_home;
# Choose configuration.
provide("lan_config");
depend("lan_config_done") config;
# Assign IP address.
net.ipv4.addr(dev, config.addr, config.addr_prefix);
# Add default route.
net.ipv4.route("0.0.0.0", "0", config.gateway, "20", dev);
# Configure DNS servers.
net.dns(config.dns_servers, "20");
}
process lan_config_home {
depend("lan_config") dep;
if(dep.is_home);
println("using config: static");
# Choose static configuration.
var("192.168.1.72") addr;
var("24") addr_prefix;
var("192.168.1.1") gateway;
list("192.168.1.1") dns_servers;
provide("lan_config_done");
}
process lan_config_other {
depend("lan_config") dep;
ifnot(dep.is_home);
println("using config: dhcp");
# Verify address.
ip_in_network(dep.dhcp.addr, "127.0.0.0", "8") test_local;
ifnot(test_local);
# Alias configuration from DHCP.
var(dep.dhcp.addr) addr;
var(dep.dhcp.prefix) addr_prefix;
var(dep.dhcp.gateway) gateway;
var(dep.dhcp.dns_servers) dns_servers;
provide("lan_config_done");
}