########################################################################## # package HostTools.pm # This module contains functions which acquire simple information about # hosts # V Who When What # --------------------------------------------------------------------------- # 1.5.2 skendric 06-16-2008 Stylistic mods # 1.5.1 skendric 05-21-2008 streamline get_nodename # 1.5.0 skendric 03-12-2007 Stylistic mods # 1.4.9 skendric 07-11-2006 Employ Net::IPAddress to validate IP addresses # 1.4.8 skednric 10-13-2005 Clean-up get_nodename routine # 1.4.7 skendric 06-13-2005 Check subroutine parameters # 1.4.6 skendric 06-12-2005 Fix bug in get_ipaddr, return undef from # get_nodename if not found # 1.4.5 skendric 02-09-2005 Convert %hostname to %nodename # 1.4.4 skendric 02-06-2005 get_hostname populates %hostname now # 1.4.3 skendric 01-03-2005 Add get_ipaddr # 1.4.2 skendric 01-03-2005 Change return value of get_hostname # 1.4.1 skendric 09-02-2004 Yank guess_os_name # 1.4.0 skendric 08-28-2004 Add guess_os_name # 1.3.0 skendric 08-27-2004. Move ping_it to PingTools # 1.2.0 skendric 08-26-2004 Move complex functions to DataCollection.pm # 1.1.0 skendric 08-22-2004 Add get_host_details and get_netbios_name # 1.0.4 skendric 07-13-2004 Change EXPORT_OK to EXPORT (sigh) # 1.0.3 skendric 07-13-2004 Change EXPORT to EXPORT_OK # 1.0.2 skendric 06-19-2004 get_netbios_name fixes # 1.0.1 skendric 06-18-2004 Re-name 'is_alive' to 'ping_it' # 1.0.0 skendric 06-06-2004 First version # # # # Authors: Stuart Kendrick # # Source: http://www.skendric.com/device/soma # # This software is available under the GNU GENERAL PUBLIC LICENSE, see # http://www.fsf.org/licenses/gpl.html # package FHCRC::VDOPS::HostTools; #### Load modules #### use strict; use warnings; use Carp qw(carp cluck croak confess); use Data::Dumper; use Exporter; use Net::hostent; use Net::Ifconfig::Wrapper; use Net::IPAddress qw(validaddr); use Perl6::Say; use Socket; use lib '/home/soma/lib'; use FHCRC::VDOPS::SomaData; use FHCRC::VDOPS::Utilities; #### Set-up export stuff #### our @ISA = qw(Exporter); our @EXPORT = qw( get_ipaddr get_hostname get_nodename my_broadcast ); # Declare package local variables ##### Only subroutines below here #### ####################################################################### # Given a hostname, return its IP address ######################################################################## sub get_ipaddr { my $h; # Net::hostent object my $ipaddr; # The answer my $nodename = shift; # The hostname # Debug trace trace_location('begin') if $debug == 8; # Sanity check confess "No parameters!" unless defined $nodename; # Do the work $h = gethost($nodename); $ipaddr = inet_ntoa($h->addr) if defined $h; # Debug trace trace_location('end') if $debug == 8; return $ipaddr; } ####################################################################### # Given an IP address, return hostname ######################################################################## sub get_hostname { my $h; # Net::hostent object my $hostname; # The answer my $addr = shift; # The IP address # Debug trace trace_location('begin') if $debug == 8; # Sanity check confess "No parameters!" unless defined $addr; confess "Not a valid IP address!" unless validaddr($addr); # Do the work $h = gethost($addr); $hostname = $h->name if defined $h; # Debug trace trace_location('end') if $debug == 8; return $hostname; } ####################################################################### # Given an IP address, return the nodename ######################################################################## sub get_nodename { my $addr = shift; # The IP address my $h; # Net::hostent object my $nodename; # The answer # Debug trace trace_location('begin') if $debug == 8; # Sanity check confess 'No parameters!' unless defined $addr; confess 'Not a valid IP address!' unless validaddr($addr); # Do the work $h = gethost($addr); if (defined $h) { $nodename = $h->name; ($nodename) = ($nodename =~ /(.*?)\./) if $nodename =~ /\./; } # Debug info if ($debug == 8) { if (defined $nodename) { say "$addr = $nodename"; } else { say "$addr nodename is undefined"; } } # Debug trace trace_location('end') if $debug == 8; return $nodename; } ######################################################################## # Find my broadcast address(es), return a list containing unique # elements. I bet there exists a way cleaner way to do this ... I don't # grasp references to hashes of references to hashes yet ... ######################################################################## sub my_broadcast { my (%anotherhash, @bcast, %hash, $hashref, %nexthash, %seen, @uniq); # Debug trace trace_location('begin') if $debug; # Acquire ifconfig info $hashref = Net::Ifconfig::Wrapper::Ifconfig('list', '', '', ''); %hash = %$hashref; # Dig through these nested hashes to find broadcast addresses for my $key (keys %hash) { %nexthash = % { $hash{$key} }; for my $nextkey (keys %nexthash) { if ($nextkey eq "inet") { %anotherhash = % { $nexthash{$nextkey} }; for my $anotherkey (keys %anotherhash) { push @bcast, $anotherhash{$anotherkey}; } } } } # Look for unique broadcast addresses %seen = (); for my $item (@bcast) { push (@uniq, $item) unless $seen{$item}++; } @bcast = @uniq; # Debug trace trace_location('end') if $debug; return @bcast; } 1;