Get notified on network interface changes

1,368 views
Skip to first unread message

Didrik Nordström

unread,
Apr 30, 2013, 11:06:38 PM4/30/13
to golan...@googlegroups.com
Hi, I want to write a program that is aware of changes in the network interfaces. net.Interfaces() seems like a great way to start, and as far as I understand by looking at the source code, go uses system calls to retrieve the current interfaces. I want my program to be notified if an interface is added/activated and removed/deactivated. Is my best shot to call net.Interfaces() at a regular time interval, and combine it with some channels? Any experiences?

Thanks,
Didrik

Jeff Mitchell

unread,
May 1, 2013, 11:11:33 AM5/1/13
to Didrik Nordström, golan...@googlegroups.com
Didrik Nordström wrote:
> Hi, I want to write a program that is aware of changes in the network
> interfaces. net.Interfaces() <http://golang.org/pkg/net/#Interfaces>
> seems like a great way to start, and as far as I understand by looking
> at the source code <http://golang.org/src/pkg/net/interface_linux.go>,
> go uses system calls to retrieve the current interfaces. I want my
> program to be notified if an interface is added/activated and
> removed/deactivated. Is my best shot to call net.Interfaces() at a
> regular time interval, and combine it with some channels? Any experiences?

One possibility would be to fork off a process running "ip monitor link". You could use http://labix.org/pipe to easily process the output and send a notification via a channel when there is a change.

--Jeff

Jeff R. Allen

unread,
May 2, 2013, 8:58:16 AM5/2/13
to golan...@googlegroups.com
If you are on Linux, you can use an AF_NETLINK socket to receive these notifications. There's a package for that:

  -jeff

Mikio Hara

unread,
May 8, 2013, 7:34:26 AM5/8/13
to Didrik Nordström, golang-nuts
It depends on what your application is, I mean such network facility information
API is designed for fetching network facility information at
application startup.
If your application requires more quickness and scalability like
routing, signaling
protocols, the best shot might be receiving deltas, in your case
interface arrival
and departure notifications, from the kernel by using routing, netlink sockets.
Reply all
Reply to author
Forward
0 new messages