############################ Accessing 'HOSTFILE' SERVER: [xxxxxx.black.lab.com] Operating System is: [Linux] System Platform is: [RedHat] Running on system: [xxxxxxx] >>> IP6 Device: eth0 is using: xx::219:xx:xxxx:xx/64 address >>> IP4 Device: eth1 is using: xx.xxx.xx.xxx address Mask: xxx.xxx.xxx.xxx Broadcast: xxx.xx.xx.xxx >>> IP6 Device: eth1 is using: xx:80c0:xx:xx:xx:xxxx:feee:xxxx/64 address /etc-test/sysconfig/network-scripts/ifcfg-eth1 /etc-test/sysconfig/network-scripts/ifcfg-eth0 /etc-test/resolv.conf ############################ Accessing 'HOSTFILE' SERVER: [xxxx-xxxx.xxx.xxx.xxxx.com ] #### open (FILE, '<', 'hostfile2') or die "Could not open 'hostfile' : $!"; my @SERVERS = ; #print "AJAY <@SERVERS>\n"; foreach $server (@SERVERS) { #print "[$server]\n"; chomp($server); print "Accessing 'HOSTFILE' SERVER: [$server]\n"; my $opsys = getOS($server, $user, $password); print "Operating System is: [$opsys]\n"; my $pform = getPlatform($server, $user, $password, $opsys); print "System Platform is: [$pform]\n"; my $host = getHost($server, $user, $password); print "Running on system: [$host]\n"; my $nic; my @files; for $nic (getNetworkInfo($server, $user, $password, $pform)) { if (defined $nic->{ip}) { print ">>> IP4 Device: $nic->{device} is using: $nic->{ip} address\n" . "\tMask: $nic->{mask}\n" . "\tBroadcast: $nic->{bcast}\n"; } if (defined $nic->{ip6}) { print ">>> IP6 Device: $nic->{device} is using: $nic->{ip6} address\n"; } push(@files, getPlatformFiles($pform, $nic)); } my %hash = map { $_ => 1 } @files; @files = keys %hash; foreach my $file (@files) { print "$file\n"; } } #### sub getOS { my ($servername_in, $user_in, $password_in) = @_; #print "DEBUG [$servername_in][$user_in][$password_in]\n"; open(INPUTA,"ssh -q $user_in\@$servername_in $cmd1; echo $password_in |") || die "command $cmd1 failed\n"; my $os = ; close INPUTA; chomp($os); #print "DEBUG [$os]\n"; return $os; } sub getPlatform { my ($servername_in, $user_in, $password_in, $opsys_in) = @_; my $platform; if ($opsys_in =~ /Linux/) { { my ($file, $var1, $var2); local $/ = undef; #undef the input rec seperator (newline), slurp file into a scalar, read entire file at once open(OUTPUTA, "ssh -q $user_in\@$servername_in $cmd3; echo $password_in |") || die "command $cmd3 failed\n"; $file = ; close OUTPUTA; chomp($file); #print ">>>>>>>>>>>> FileContent is: [$file]\n"; if ( $file =~ /^(Red) (Hat)/ ) { ($var1, $var2) = ($1, $2) or next; $platform = "$var1$var2"; #print "$var1$var2\n"; last; } elsif ( $file =~ /\bWelcome to ([\w.]+)/ ) { ($var1) = ($1) or next; $platform = "$var1"; #print "$var1\n"; last; } elsif ( $file =~ /^(Ubuntu)/ ) { ($var1) = ($1) or next; $platform = "$var1"; #print "$var1\n"; last; } else { print "Operating System not supported in this script\n"; } } } elsif ($opsys_in =~ /SunOS/) { $platform = "Solaris"; #print "DEBUG [$pform]\n"; } chomp($platform); return $platform } sub getHost { my ($servername_in, $user_in, $password_in) = @_; open(INPUTB,"ssh -q $user_in\@$servername_in $cmd2; echo $password_in |") || die "command $cmd2 failed\n"; my $hn = ; close INPUTB; chomp($hn); #print "DEBUG [$hn]\n"; return $hn; } sub getNetworkInfo { my ($servername_in, $user_in, $password_in, $pform_in) = @_; my @nics; my $ifconfig; if ($pform_in eq "RedHat") { undef $/; open(OUTPUTB, "ssh -q $user_in\@$servername_in $cmd4; echo $password_in |") || die "command $cmd4 failed\n"; $ifconfig = ; close OUTPUTB; chomp($ifconfig); for (split /(?<=\n)(?=\w)/, $ifconfig) { my %nic; ($nic{device}) = $_ =~ /^(eth\d)\s/ or next; if (/\binet addr:([\d.]+)\s.+?:([\d.]+)\s.+?:([\d.]+)/) { $nic{ip} = $1; $nic{bcast} = $2; $nic{mask} = $3; #print "Device: $nic{device} has the IP Address of $nic{ip}\n\tMask: $nic{mask}\n\tBroadcast: $nic{bcast}\n"; } if (/^\s+ inet6 addr:\s*(\S+)/m) { $nic{ip6} = $1; #print "Device: $nic{device} also has IPv6 address of $nic{ip6}\n"; } push @nics, \%nic; } }elsif($pform_in eq "Solaris") { undef $/; open(OUTPUTC, "ssh -q $user_in\@$servername_in \"$cmd5\"; echo $password_in |") || die "command \"$cmd5\" failed\n"; $ifconfig = ; close OUTPUTC; chomp($ifconfig); for (split /(?<=\n)(?=\w)/, $ifconfig) { my %nic; ($nic{device}) = $_ =~ /^(fjgi\d):\s/ or next; if (/\binet ([\d.]+)\s\w+\s([\w]+)\s\w+\s([\d.]+)/) { $nic{ip} = $1; $nic{mask} = $2; $nic{bcast} = $3; #print "Device: $nic{device} has the IP Address of $nic{ip}\n\tMask: $nic{mask}\n\tBroadcast: $nic{bcast}\n"; } if (/^\s+ inet6\s*(\S+)/m) { $nic{ip6} = $1; #print "Device: $nic{device} also has IPv6 address of $nic{ip6}\n"; } push @nics, \%nic; } }elsif ($pform_in eq "SUSE") { undef $/; open(OUTPUTD, "ssh -q $user_in\@$servername_in $cmd4; echo $password_in |") || die "command $cmd4 failed\n"; $ifconfig = ; close OUTPUTD; chomp($ifconfig); for (split /(?<=\n)(?=\w)/, $ifconfig) { my %nic; ($nic{device}) = $_ =~ /^(eth\d)\s/ or next; if (/\binet addr:([\d.]+)\s.+?:([\d.]+)\s.+?:([\d.]+)/) { $nic{ip} = $1; $nic{bcast} = $2; $nic{mask} = $3; #print "Device: $nic{device} has the IP Address of $nic{ip}\n\tMask: $nic{mask}\n\tBroadcast: $nic{bcast}\n"; } if (/^\s+ inet6 addr:\s*(\S+)/m) { $nic{ip6} = $1; #print "Device: $nic{device} also has IPv6 address of $nic{ip6}\n"; } push @nics, \%nic; #Returning an list of hashrefs } }elsif($pform_in eq "Ubuntu"){ undef $/; open(OUTPUTE, "ssh -q $user_in\@$servername_in $cmd4; echo $password_in |") || die "command $cmd4 failed\n"; $ifconfig = ; close OUTPUTE; chomp($ifconfig); for (split /(?<=\n)(?=\w)/, $ifconfig) { my %nic; ($nic{device}) = $_ =~ /^(eth\d)\s/ or next; if (/\binet addr:([\d.]+)\s.+?:([\d.]+)\s.+?:([\d.]+)/) { $nic{ip} = $1; $nic{bcast} = $2; $nic{mask} = $3; #print "Device: $nic{device} has the IP Address of $nic{ip}\n\tMask: $nic{mask}\n\tBroadcast: $nic{bcast}\n"; } if (/^\s+ inet6 addr:\s*(\S+)/m) { $nic{ip6} = $1; #print "Device: $nic{device} also has IPv6 address of $nic{ip6}\n"; } push @nics, \%nic; #Returning an list of hashrefs } } #use Data::Dumper; #print Dumper(\$nics); #print Dumper(\@nics); #print Dumper(\%nics); return @nics; } sub getPlatformFiles { my ($plat_in, $nic_in) = @_; my @list; if ($plat_in =~ /RedHat/) { push (@list, "/etc-test/resolv.conf"); push (@list, "/etc-test/sysconfig/network-scripts/ifcfg-$nic_in->{device}" ); } return @list; }