From 41c1938b8c0f2eae0218913c1a8d7848999b0861 Mon Sep 17 00:00:00 2001 From: Casandra Qiu Date: Wed, 1 Jun 2016 17:00:25 -0400 Subject: [PATCH 1/3] Use arp command to get switch mac address --- perl-xCAT/xCAT/Utils.pm | 32 +++++++++++++++++++ .../lib/xcat/plugins/switchdiscover.pm | 2 +- 2 files changed, 33 insertions(+), 1 deletion(-) diff --git a/perl-xCAT/xCAT/Utils.pm b/perl-xCAT/xCAT/Utils.pm index f49fb3253..f92a9f1c2 100644 --- a/perl-xCAT/xCAT/Utils.pm +++ b/perl-xCAT/xCAT/Utils.pm @@ -4711,5 +4711,37 @@ sub get_nmapversion { return $nmap_version; } +#-------------------------------------------------------------------------------- +=head3 get_macbyarp + Get Mac address by arp -n + Returns: + mac: Mac address +=cut +#-------------------------------------------------------------------------------- + +sub get_macbyarp { + my $arptable; + my $mac; + my $ip = shift; + if ($ip =~ /xCAT::Utils/) + { + $ip = shift; + } + if ( -x "/usr/sbin/arp") { + $arptable = `/usr/sbin/arp -n`; + } + else{ + $arptable = `/sbin/arp -n`; + } + my @arpents = split /\n/,$arptable; + foreach (@arpents) { + if (m/^($ip)\s+\S+\s+(\S+)\s/) { + $mac=$2; + last; + } + } + + return $mac; +} 1; diff --git a/xCAT-server/lib/xcat/plugins/switchdiscover.pm b/xCAT-server/lib/xcat/plugins/switchdiscover.pm index 344997d2b..ae7a70827 100644 --- a/xCAT-server/lib/xcat/plugins/switchdiscover.pm +++ b/xCAT-server/lib/xcat/plugins/switchdiscover.pm @@ -867,7 +867,7 @@ sub snmp_scan { my $vendor = get_snmpvendorinfo($request, $ip); if ($vendor) { - my $mac = get_snmpmac($request, $ip); + my $mac = xCAT::Utils->get_macbyarp($ip); if (!$mac) { $mac="nomac_nmap_$counter"; $counter++; From bbf151bf629dd5ee71b095948c20a6c2b35216fa Mon Sep 17 00:00:00 2001 From: Casandra Qiu Date: Tue, 7 Jun 2016 22:17:54 -0400 Subject: [PATCH 2/3] Use get_mac_by_arp subroutine from SvrUtil.pm --- perl-xCAT/xCAT/Utils.pm | 33 --------------- xCAT-server/lib/perl/xCAT/SvrUtils.pm | 25 ++++++----- .../lib/xcat/plugins/switchdiscover.pm | 41 ++++++++++++++----- 3 files changed, 45 insertions(+), 54 deletions(-) diff --git a/perl-xCAT/xCAT/Utils.pm b/perl-xCAT/xCAT/Utils.pm index f92a9f1c2..831bae20a 100644 --- a/perl-xCAT/xCAT/Utils.pm +++ b/perl-xCAT/xCAT/Utils.pm @@ -4711,37 +4711,4 @@ sub get_nmapversion { return $nmap_version; } -#-------------------------------------------------------------------------------- -=head3 get_macbyarp - Get Mac address by arp -n - Returns: - mac: Mac address -=cut -#-------------------------------------------------------------------------------- - -sub get_macbyarp { - my $arptable; - my $mac; - my $ip = shift; - if ($ip =~ /xCAT::Utils/) - { - $ip = shift; - } - if ( -x "/usr/sbin/arp") { - $arptable = `/usr/sbin/arp -n`; - } - else{ - $arptable = `/sbin/arp -n`; - } - my @arpents = split /\n/,$arptable; - foreach (@arpents) { - if (m/^($ip)\s+\S+\s+(\S+)\s/) { - $mac=$2; - last; - } - } - - return $mac; -} - 1; diff --git a/xCAT-server/lib/perl/xCAT/SvrUtils.pm b/xCAT-server/lib/perl/xCAT/SvrUtils.pm index 4d0a99052..a02ca4d1c 100755 --- a/xCAT-server/lib/perl/xCAT/SvrUtils.pm +++ b/xCAT-server/lib/perl/xCAT/SvrUtils.pm @@ -1163,6 +1163,7 @@ sub update_tables_with_diskless_image nodes: a reference to nodes array display: whether just display the result, if not 'yes', the result will be written to the mac table. + nopping: will not call pping subroutine if it set as "nopping" Returns: Return a hash with node name as key Globals: @@ -1170,7 +1171,7 @@ sub update_tables_with_diskless_image Error: none Example: - xCAT::Utils->get_mac_by_arp($nodes, $display); + xCAT::Utils->get_mac_by_arp($nodes, $display, $nopping); Comments: =cut @@ -1178,26 +1179,28 @@ sub update_tables_with_diskless_image #------------------------------------------------------------------------------- sub get_mac_by_arp () { - my ($class, $nodes, $display) = @_; + my ($class, $nodes, $display, $nopping) = @_; my $node; my $data; my %ret = (); my $unreachable_nodes = ""; my $noderange = join (',', @$nodes); - my @output = xCAT::Utils->runcmd("/opt/xcat/bin/pping $noderange", -1); - - foreach my $line (@output) { - my ($hostname, $result) = split ':', $line; - my ($token, $status) = split ' ', $result; - chomp($token); - if ($token eq 'ping') { - $node->{$hostname}->{reachable} = 1; + + if ( $nopping ne "nopping" ) { + my @output = xCAT::Utils->runcmd("/opt/xcat/bin/pping $noderange", -1); + foreach my $line (@output) { + my ($hostname, $result) = split ':', $line; + my ($token, $status) = split ' ', $result; + chomp($token); + if ($token eq 'ping') { + $node->{$hostname}->{reachable} = 1; + } } } foreach my $n ( @$nodes ) { - if ( $node->{$n}->{reachable} ) { + if ( ( $node->{$n}->{reachable} ) || ( $nopping eq "nopping" ) ){ my $output; my $IP = xCAT::NetworkUtils::toIP( $n ); if ( xCAT::Utils->isAIX() ) { diff --git a/xCAT-server/lib/xcat/plugins/switchdiscover.pm b/xCAT-server/lib/xcat/plugins/switchdiscover.pm index ae7a70827..94c194168 100644 --- a/xCAT-server/lib/xcat/plugins/switchdiscover.pm +++ b/xCAT-server/lib/xcat/plugins/switchdiscover.pm @@ -13,6 +13,7 @@ use xCAT::Usage; use xCAT::NodeRange; use xCAT::NetworkUtils; use xCAT::Utils; +use xCAT::SvrUtils; use XML::Simple; no strict; use Data::Dumper; @@ -826,13 +827,17 @@ sub snmp_scan { my $ranges = get_ip_ranges($request); - #use nmap to find if snmp port is enabled - # only open port will be scan + ########################################################## + #use nmap to parse the ip range and possible output from the command: + # Nmap scan report for switch-10-5-22-1 (10.5.22.1) 161/udp open snmp + # Nmap scan report for 10.5.23.1 161/udp open snmp + # Nmap scan report for 10.5.24.1 161/udp closed snmp + ########################################################## my $nmap_version = xCAT::Utils->get_nmapversion(); if (xCAT::Utils->version_cmp($nmap_version,"5.10") < 0) { - $ccmd = "/usr/bin/nmap -P0 -v -sU -p 161 -oA snmp_scan @$ranges | grep up | grep good "; + $ccmd = "/usr/bin/nmap -P0 -v -sU -p 161 -oA snmp_scan @$ranges | grep -E 'appears to be up|^161' | perl -pe 's/\\n/ / if \$. % 2'"; } else { - $ccmd = "/usr/bin/nmap -P0 -v -sU -p 161 -oA snmp_scan @$ranges | grep 'open port 161' "; + $ccmd = "/usr/bin/nmap -P0 -v -sU -p 161 -oA snmp_scan @$ranges | grep -v 'host down' | grep -E 'Nmap scan report|^161' | perl -pe 's/\\n/ / if \$. % 2'"; } if (exists($globalopt{verbose})) { send_msg($request, 0, "Process command: $ccmd\n"); @@ -855,19 +860,35 @@ sub snmp_scan { foreach my $line (@lines) { my @array = split / /, $line; - my $ip; - if (xCAT::Utils->version_cmp($nmap_version,"5.10") < 0) { - $ip = $array[1]; - } else { - $ip = $array[5]; + if ($line =~ /\b(\d{1,3}(?:\.\d{1,3}){3})\b/) + { + $ip = $1; } if (exists($globalopt{verbose})) { send_msg($request, 0, "Run snmpwalk command to get information for $ip"); } + if ($line =~ /close/) { + send_msg($request, 0, "*** snmp port is disabled for $ip ***"); + next; + } + my $vendor = get_snmpvendorinfo($request, $ip); if ($vendor) { - my $mac = xCAT::Utils->get_macbyarp($ip); + my $display = ""; + my $nopping = "nopping"; + my $output = xCAT::SvrUtils->get_mac_by_arp([$ip], $display, $nopping); + my $mac; + foreach my $node (keys %{$output}) { + if ($node eq $ip) { + my ($desc,$mac_arp) = split /: /, $output->{$node}; + $mac = $mac_arp; + last; + } + } + if (exists($globalopt{verbose})) { + send_msg($request, 0, "node: $ip, mac: $mac"); + } if (!$mac) { $mac="nomac_nmap_$counter"; $counter++; From fec8d6addcbed9ffd0acc1d3a9de37d8f883baf6 Mon Sep 17 00:00:00 2001 From: Casandra Qiu Date: Wed, 8 Jun 2016 10:09:22 -0400 Subject: [PATCH 3/3] more modification --- xCAT-server/lib/xcat/plugins/switchdiscover.pm | 11 +++-------- 1 file changed, 3 insertions(+), 8 deletions(-) diff --git a/xCAT-server/lib/xcat/plugins/switchdiscover.pm b/xCAT-server/lib/xcat/plugins/switchdiscover.pm index 94c194168..38c7e3546 100644 --- a/xCAT-server/lib/xcat/plugins/switchdiscover.pm +++ b/xCAT-server/lib/xcat/plugins/switchdiscover.pm @@ -832,6 +832,8 @@ sub snmp_scan { # Nmap scan report for switch-10-5-22-1 (10.5.22.1) 161/udp open snmp # Nmap scan report for 10.5.23.1 161/udp open snmp # Nmap scan report for 10.5.24.1 161/udp closed snmp + # Host 10.5.23.1 appears to be up ... good. 161/udp open|filtered snmp + # Host 10.5.24.1 appears to be up ... good. 161/udp closed snmp ########################################################## my $nmap_version = xCAT::Utils->get_nmapversion(); if (xCAT::Utils->version_cmp($nmap_version,"5.10") < 0) { @@ -878,14 +880,7 @@ sub snmp_scan { my $display = ""; my $nopping = "nopping"; my $output = xCAT::SvrUtils->get_mac_by_arp([$ip], $display, $nopping); - my $mac; - foreach my $node (keys %{$output}) { - if ($node eq $ip) { - my ($desc,$mac_arp) = split /: /, $output->{$node}; - $mac = $mac_arp; - last; - } - } + my ($desc,$mac) = split /: /, $output->{$ip}; if (exists($globalopt{verbose})) { send_msg($request, 0, "node: $ip, mac: $mac"); }