A C extension that provides hostname and IP address information about the current host.
require 'sys/host' include Sys puts Host.hostname puts Host.ip_addr # For a multi-nic system Host.ip_addr.each do |ip| puts ip end
VERSION
Returns the current version number of this package (as a string)
Host.hostname
Returns the hostname of the current host. This may or not return the FQDN, depending on your system.
Host.ip_addr
Returns a list of IP addresses for the current host (yes, it is possible to have more than one).
Host.VERSION
Returns the current version number as a string. Be sure to use parens for this method call or you'll end up getting the *Ruby* version number.
HostException < StandardError
This exception is raised if the gethostname() call returns a non-zero value. If you hit this error, you got big problems. This can be raised in either the hostname or ip_addr method.
HostAddressException < StandardError
This exception is raised if the gethostbyname() call returns a NULL hostent structure in the ip_addr method. That *probably* means the hostname hasn't been defined in /etc/hosts.
IPV6 is not supported (yet).
Add reentrant versions for other platforms. Add support for IPV6. This will be difficult unless I have access to a system on an IPV6 network. Any help on this front appreciated.
"This module doesn't do very much. What's the point?" - I thought that having to load the 'socket' module was overkill just to get the hostname. "Why an extension? Wouldn't it have been easier to just parse the output of a system call?" - What fun would that be? Seriously, I didn't want to have to spawn a subprocess for this. Besides, Perl has a Sys::Hostname module, so I thought Ruby should have one, too. Mine does more, though. :)
Returning multiple ip addresses for multi-homed systems doesn't seem to work properly. Help appreciated
Thanks go to Mike Hall, Guy Decoux, and Matz for their help with C extensions in general. :) Thanks also go to Richard Stevens for his *awesome* Unix programming books.
Copyright 2002-2003, Daniel J. Berger, djberg96@yahoo.com All Rights Reserved. This module is free software. It may be used, redistributed and/or modified under the same terms as Ruby itself.
Daniel J. Berger djberg96 at yahoo dot com rubyhacker1 on IRC
gethostbyname, gethostbyname_r, gethostname, hostname