1

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.

2
  • 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? Commented Aug 14, 2013 at 21:47
  • yea. i mean. manage the network adapter . bring up or down the iface, also add or remove IPADDRESS Commented Aug 14, 2013 at 21:52

2 Answers 2

3

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)
}
Sign up to request clarification or add additional context in comments.

1 Comment

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.
2

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

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 !!!???
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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.