我的笔记本电脑需要在家中使用 DHCP,在工作时使用静态 IP。据我所知,没有办法快速在两者之间切换 - 每次移动笔记本电脑时我都必须手动配置网络连接。一种替代方法是创建一个小型 Bash 脚本来编辑/etc/network/interfaces
,但我宁愿使用更简单的方法,例如网络代理首选项 → 位置。
编辑:看起来这可能很简单使用/etc/network/interfaces
本身。
答案1
使用我正在开发的网络配置软件 NCD 可以轻松完成此操作:http://code.google.com/p/badvpn/wiki/NCD
以下 NCD 程序根据变量的值使用静态配置或 DHCP。要切换,您必须编辑 NCD 程序并重新启动 NCD 守护程序。(但请参阅下文了解自动检测)
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");
}
还可以通过检查 DHCP 服务器的 MAC 地址来自动进行选择。以下 NCD 程序始终执行 DHCP,但随后它会根据家庭路由器的地址检查 DHCP 服务器的 MAC 地址。如果它们匹配,它会忽略通过 DHCP 获得的地址并分配静态地址,否则它会分配 DHCP 地址。(注意:您需要 svn repo 中的最新 NCD 才能执行此操作,我刚刚实现了变量dhcp.server_mac
)
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");
}