使用我的这个软件进行网络配置,非常简单。
http://code.google.com/p/badvpn/wiki/NCD
以下NCD程序将起作用。它还显示了如何扩展配置,在这种情况下,通过在网络启动/关闭时运行某些程序,可以扩展配置。
process eth0 {
# Set device.
var("eth0") dev;
# Wait for device to appear, set it up, and wait for cable to be plugged in.
net.backend.waitdevice(dev);
net.up(dev);
net.backend.waitlink(dev);
# DHCP configuration.
# net.ipv4.dhcp() will block here until it obtaines an IP address.
# It doesn't check the obtained address in any way,
# so as a basic security measure, do not proceed if it is local.
net.ipv4.dhcp(dev) dhcp;
ip_in_network(dhcp.addr, "127.0.0.0", "8") test_local;
ifnot(test_local);
# Assign IP address to interface, as obtained by DHCP.
net.ipv4.addr(dev, dhcp.addr, dhcp.prefix);
# Add default route.
net.ipv4.route("0.0.0.0", "0", dhcp.gateway, "20", dev);
# Add DNS servers.
net.dns(dhcp.dns_servers, "20");
# Run an external program when network comes up or goes down.
list("/some/program/to/run/when/up", "argument") do;
list("/some/program/to/run/when/down", "argument") undo;
run(do, undo);
}
请注意,这不仅可以处理插入或拔出的网络电缆,还可以处理网络接口本身的出现和消失(如果是USB,则很有用)。
与诸如ifplugd之类的其他软件相比,使用NCD的优势在于其设计具有更大的灵活性。基本上,您可以编程自己的网络配置,而不必局限于一组有限的硬编码功能。
更新:我已经为BadVPN和NCD制作了Ubuntu软件包。我也用Ubuntu的安装和使用说明更新了Wiki:http : //code.google.com/p/badvpn/wiki/NCD#Running_it
ifplugd
并没有得到netplugd
维持。但是,使用udev规则,这个类似的问题看起来像是一个很好的(简单)答案-不需要其他软件包。serverfault.com/a/312296/275255