Does Go offer a way to configure network interfaces? I found a very easy to use net.Interfaces method to get the information, but i want to modify the network configuration.
-
I tried to rephrase some parts of your question and I hope I got it right. What do you want to modify? The IP address? Something else?tux21b– tux21b2013-08-14 21:47:48 +00:00Commented Aug 14, 2013 at 21:47
-
yea. i mean. manage the network adapter . bring up or down the iface, also add or remove IPADDRESSFreaktor– Freaktor2013-08-14 21:52:15 +00:00Commented Aug 14, 2013 at 21:52
Add a comment
|
2 Answers
You can accomplish this using netlink:
lnk, err := netlink.LinkByName("eth0")
if err != nil {
log.Fatal(err)
}
ipConfig := &netlink.Addr{IPNet: &net.IPNet{
IP: net.ParseIP("192.168.0.2"),
Mask: net.CIDRMask(24, 32),
}}
if err = netlink.AddrAdd(lnk, ipConfig); err != nil {
log.Fatal(err)
}
1 Comment
MarSoft
Please be warned that at the time of writing this library does not support other OS then Linux. So if you need a cross-platform solution then you will have to either use
ip/ipconfig/netsh directly with os/exec, or look for some other libraries.In order to modify your network config, the best way would be to call external tools like ip, iptables, ifconfig, brctl, etc..
This is the way we do in within docker (https://github.com/dotcloud/docker/blob/master/network.go#L72)
2 Comments
d9ngle
This answer is pretty much useless, he's asking about doing it in
Go and your response is to use ip, iptables, ifconfig, brctl, docker !!!???Freaktor
at the time (2013) this was the best answer for my problem, but now the library you suggest is better for the job, btw he wasnt suggesting me to use docker, he was saying that was the way docker internally used it