# How can I get the local machine's netmask programmatically?

**URL:** <https://www.ruby-forum.com/t/how-can-i-get-the-local-machines-netmask-programmatically/106765>\
**Category:** Ruby\
**Created:** [June 13, 2007, 10:31pm UTC](https://www.ruby-forum.com/t/how-can-i-get-the-local-machines-netmask-programmatically/106765 "2007-06-13T22:31:32Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![wolfram](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/e68b1a/32.png) [@wolfram](https://www.ruby-forum.com/u/wolfram)\
**Post date:** [June 13, 2007, 10:31pm UTC](https://www.ruby-forum.com/t/how-can-i-get-the-local-machines-netmask-programmatically/106765/1 "2007-06-13T22:31:32Z")

</div>

I’m working on a Rails app that needs to restrict certain privileged  
operations/requests to only those requests that originate from within  
the local subnet.

How can I find out the machine’s netmask programmatically?

Thanks,

Wolf

---

<div class="post-metadata">

**Author:** ![wolfram](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/e68b1a/32.png) [@wolfram](https://www.ruby-forum.com/u/wolfram)\
**Post date:** [June 13, 2007, 11:13pm UTC](https://www.ruby-forum.com/t/how-can-i-get-the-local-machines-netmask-programmatically/106765/2 "2007-06-13T23:13:38Z")

</div>

On 6/13/07, wolfram [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> I’m working on a Rails app that needs to restrict certain privileged  
> operations/requests to only those requests that originate from within  
> the local subnet.
> 
> How can I find out the machine’s netmask programmatically?

Hi,

what operating system? windows/linux(distribution?)/osx?  
how many network adapters are in the machine?

on windows use `ipconfig /all` and parse output or WMI through Win32OLE  
on unix `/sbin/ifconfig -a`

If there are more adapters, you have to choose the right one (or allow  
all local nets).

J.

---

<div class="post-metadata">

**Author:** ![wolfram](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/e68b1a/32.png) [@wolfram](https://www.ruby-forum.com/u/wolfram)\
**Post date:** [June 14, 2007, 12:45am UTC](https://www.ruby-forum.com/t/how-can-i-get-the-local-machines-netmask-programmatically/106765/3 "2007-06-14T00:45:52Z")

</div>

> what operating system? windows/linux(distribution?)/osx?  
> how many network adapters are in the machine?

Linux.

> on windows use `ipconfig /all` and parse output or WMI through Win32OLE  
> on unix `/sbin/ifconfig -a`

I know about the system commands. Is there a library/API to get these  
programmatically without having to parse output from a sytem command?

Thanks,  
W.

---

<div class="post-metadata">

**Author:** ![wolfram](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/e68b1a/32.png) [@wolfram](https://www.ruby-forum.com/u/wolfram)\
**Post date:** [June 14, 2007, 1:30am UTC](https://www.ruby-forum.com/t/how-can-i-get-the-local-machines-netmask-programmatically/106765/4 "2007-06-14T01:30:58Z")

</div>

On Thu, 2007-06-14 at 07:45 +0900, wolfram wrote:

> Thanks,  
> W.

Create a socket and pull it from the socket structure.

---

<div class="post-metadata">

**Author:** ![wolfram](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/e68b1a/32.png) [@wolfram](https://www.ruby-forum.com/u/wolfram)\
**Post date:** [June 14, 2007, 6:49pm UTC](https://www.ruby-forum.com/t/how-can-i-get-the-local-machines-netmask-programmatically/106765/5 "2007-06-14T18:49:48Z")

</div>

I spent quite a bit of time last night trying to figure this out to no  
avail.

Reid, how long did it take you to come up with this? Could anyone  
without C experience figured this out?

---

<div class="post-metadata">

**Author:** ![wolfram](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/e68b1a/32.png) [@wolfram](https://www.ruby-forum.com/u/wolfram)\
**Post date:** [June 14, 2007, 3:19am UTC](https://www.ruby-forum.com/t/how-can-i-get-the-local-machines-netmask-programmatically/106765/6 "2007-06-14T03:19:09Z")

</div>

On Thu, 2007-06-14 at 08:30 +0900, Reid T. wrote:

> > programmatically without having to parse output from a sytem command?
> > 
> > Thanks,  
> > W.
> 
> Create a socket and pull it from the socket structure.

see if this works…???

rthompso@shienar ~ $ cat getnetmask.rb  
require ‘rubygems’  
require “inline”

class NetMask  
inline do |builder|  
builder.include ‘\<sys/types.h\>’  
builder.include ‘\<sys/socket.h\>’  
builder.include ‘\<sys/ioctl.h\>’  
builder.include ‘\<netinet/in.h\>’  
builder.include ‘\<net/if.h\>’  
builder.c "

```
char * nmask() {
    int fd;
    struct ifreq ifr;

    fd = socket(AF_INET, SOCK_DGRAM, 0);
    ifr.ifr_addr.sa_family = AF_INET;
    strncpy(ifr.ifr_name, \"eth0\", IFNAMSIZ-1);
    ioctl(fd, SIOCGIFNETMASK, &ifr);
    close(fd);

    return (char *)inet_ntoa(((struct sockaddr_in 

```

\*)&ifr.ifr\_addr)-\>sin\_addr);  
}"  
end  
end

nm = NetMask.new()  
nmaa = nm.nmask()  
puts nmaa

rthompso@shienar ~ $ ruby getnetmask.rb  
255.255.255.0

---

<div class="post-metadata">

**Author:** ![wolfram](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/e68b1a/32.png) [@wolfram](https://www.ruby-forum.com/u/wolfram)\
**Post date:** [June 14, 2007, 7:44pm UTC](https://www.ruby-forum.com/t/how-can-i-get-the-local-machines-netmask-programmatically/106765/7 "2007-06-14T19:44:23Z")

</div>

On Fri, 2007-06-15 at 01:48 +0900, removed\_email\_address@domain.invalid wrote:

> > > > > how many network adapters are in the machine?  
> > > > > W.  
> > > > > inline do |builder|
> > 
> > nm = NetMask.new()  
> > nmaa = nm.nmask()  
> > puts nmaa
> > 
> > rthompso@shienar ~ $ ruby getnetmask.rb  
> > 255.255.255.0

[http://www.hashcode.eti.br/?p=46](http://www.hashcode.eti.br/?p=46) makes an interesting read also

---

<div class="post-metadata">

**Author:** ![wolfram](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/e68b1a/32.png) [@wolfram](https://www.ruby-forum.com/u/wolfram)\
**Post date:** [June 14, 2007, 7:22pm UTC](https://www.ruby-forum.com/t/how-can-i-get-the-local-machines-netmask-programmatically/106765/8 "2007-06-14T19:22:53Z")

</div>

On Fri, 2007-06-15 at 01:48 +0900, removed\_email\_address@domain.invalid wrote:

> > require ‘rubygems’
> > 
> > ```
> > return (char *)inet_ntoa(((struct sockaddr_in *)&ifr.ifr_addr)->sin_addr);
> > 
> > ```

1. I was previously aware of RubyInline – so didn’t have to  
‘find/research’ it

2. I knew that getting the info in C code was doable

3. I googled for examples of getting socket info

4. it took me about 15 minutes probably

4a) I think a non-C programmer could have figured it out in time  
I basically copied the C RubyInline example ( the factorial one )  
and pasted in the socket code.

The gotcha’s would probably have been  
a)figuring out to use the builder.include, described in the C++  
example, to get the header files included  
b)getting rid of a warning message due to the original C code not  
casting to (char \*) the return value of inet\_ntoa()

1. Ruby’s various socket classes probably provide access to the same  
info - but I couldn’t find out how to get at it ( like you, I spent  
waaaay more time trying to find a pure ruby way of getting the info than  
coding the example ) i.e. Ruby’s Socket class has a getsockname which  
returns the struct sockaddr packed into a string – It may have all the  
info needed, but I’m not familiar enough with unpacking a sockaddr  
structure to try to figure it out ( or unpacking anything in Ruby for  
that matter 🙂 ).
