views:

191

answers:

4

In ruby 1.8.5, how can I check for the existence of a named network interface like eth0 or wan0 under linux? Even checking the existence would be a start. I'm aware that I could wrap a shell command to use ifconfig or somesuch, but would rather have a pure ruby solution.

Another way of phrasing the question might be "If I was implementing ifconfig in ruby, what API would I use?"

A: 

This may be helpful:

Sockets Programming in Ruby

ennuikiller
+1  A: 

ifconfig opens /proc/net/dev and reads the interfaces out of there.

caf
+2  A: 

I think you should look into the sysfs tree, specifically in /sys/class/net/.

unwind
Thank you, both this and the /proc/net/dev suggestion look fine. Do you happen to know which of /proc and /sys is less likely to change in future kernels?
kdt
I think /sys is the better choice (obviously), a lot of things under /proc (or perhaps all of it, I'm not quite sure) are deprecated.
unwind
I don't imagine /sys will change in future kernel releases; it's part of a design introduced in 2.6 as being "the right way to go"
MarkR
A: 
%x[ifconfig eth0]

Check this out.

rubayeet