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
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
On 6/13/07, wolfram [email protected] 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.
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.
On Thu, 2007-06-14 at 07:45 +0900, wolfram wrote:
Thanks,
W.
Create a socket and pull it from the socket structure.
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?
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
On Fri, 2007-06-15 at 01:48 +0900, [email protected] wrote:
how many network adapters are in the machine?
W.
inline do |builder|nm = NetMask.new()
nmaa = nm.nmask()
puts nmaarthompso@shienar ~ $ ruby getnetmask.rb
255.255.255.0
http://www.hashcode.eti.br/?p=46 makes an interesting read also
On Fri, 2007-06-15 at 01:48 +0900, [email protected] wrote:
require ‘rubygems’
return (char *)inet_ntoa(((struct sockaddr_in *)&ifr.ifr_addr)->sin_addr);
I was previously aware of RubyInline – so didn’t have to
‘find/research’ it
I knew that getting the info in C code was doable
I googled for examples of getting socket info
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()
This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.
Sponsor our Newsletter | Privacy Policy | Terms of Service | Remote Ruby Jobs