From ab27ea49e27cfea4f95ee5f7d6923cc7600365c0 Mon Sep 17 00:00:00 2001 From: Casandra Qiu Date: Thu, 28 Apr 2016 20:22:23 -0400 Subject: [PATCH 1/3] Support switchdiscover snmp_scan command for sles11.4 and ubuntu --- .../lib/xcat/plugins/switchdiscover.pm | 53 +++++++++++++++---- 1 file changed, 43 insertions(+), 10 deletions(-) diff --git a/xCAT-server/lib/xcat/plugins/switchdiscover.pm b/xCAT-server/lib/xcat/plugins/switchdiscover.pm index cde703e9d..b33b4f713 100644 --- a/xCAT-server/lib/xcat/plugins/switchdiscover.pm +++ b/xCAT-server/lib/xcat/plugins/switchdiscover.pm @@ -37,10 +37,11 @@ my %global_switch_type = ( mellanox => "Mellanox", MLNX => "Mellanox", MELLAN => "Mellanox", - IBM => "BNT", + IBM => "BNT" ); + #------------------------------------------------------------------------------- =head1 xCAT_plugin:switchdiscover =head2 Package Description @@ -423,7 +424,7 @@ sub process_request { if (!$display_done) { #display header - $format = "%-12s\t%-15s\t%-40.50s\t%-12s"; + $format = "%-12s\t%-20s\t%-50s\t%-12s"; $header = sprintf $format, "ip", "name","vendor", "mac"; send_msg(\%request, 0, $header); my $sep = "------------"; @@ -801,8 +802,10 @@ sub nmap_scan { sub snmp_scan { my $request = shift; my $ccmd; + my $result; my $switches; my $counter = 0; + my $sles11_sp4=0; # snmpwalk command has to be available for snmp_scan if (-x "/usr/bin/snmpwalk" ){ @@ -819,13 +822,29 @@ sub snmp_scan { ################################################## my $ranges = get_ip_ranges($request); + #sles11 SP4 has different output than other OS from nmap command + if (-e "/etc/os-release" ) { + $ccmd = "cat /etc/os-release | grep VERSION | grep 11.4"; + $result = xCAT::Utils->runcmd($ccmd, 0); + if ($::RUNCMD_RC == 0) { + $sles11_sp4=1; + } + } + + # for sles11.4, the line as :"Host 10.4.25.1 appears to be up ... good." + # other OS has line like this: "Discovered open port 161/udp on 10.4.25.1" + # only open port will be scan #use nmap to find if snmp port is enabled - $ccmd = "/usr/bin/nmap -P0 -v -sU -p 161 -oA snmp_scan @$ranges | grep 'open port 161' "; + if ($sles11_sp4 == 1) { + $ccmd = "/usr/bin/nmap -P0 -v -sU -p 161 -oA snmp_scan @$ranges | grep up | grep good "; + } else { + $ccmd = "/usr/bin/nmap -P0 -v -sU -p 161 -oA snmp_scan @$ranges | grep 'open port 161' "; + } if (exists($globalopt{verbose})) { send_msg($request, 0, "Process command: $ccmd\n"); } - my $result = xCAT::Utils->runcmd($ccmd, 0); + $result = xCAT::Utils->runcmd($ccmd, 0); if ($::RUNCMD_RC != 0) { send_msg($request, 1, "Could not process this command: $ccmd" ); @@ -840,11 +859,14 @@ sub snmp_scan { } my @lines = split /\n/, $result; - # each line like this: "Discovered open port 161/udp on 10.4.25.1" - # only open port will be scan foreach my $line (@lines) { my @array = split / /, $line; - my $ip = $array[5]; + my $ip; + if ($sles11_sp4 == 1) { + $ip = $array[1]; + } else { + $ip = $array[5]; + } if (exists($globalopt{verbose})) { send_msg($request, 0, "Run snmpwalk command to get information for $ip"); } @@ -885,7 +907,9 @@ sub get_snmpvendorinfo { my $snmpwalk_vendor; - my $ccmd = "snmpwalk -Os -v1 -c public $ip sysDescr.0"; + #Ubuntu only takes OID + #my $ccmd = "snmpwalk -Os -v1 -c public $ip sysDescr.0"; + my $ccmd = "snmpwalk -Os -v1 -c public $ip 1.3.6.1.2.1.1.1"; if (exists($globalopt{verbose})) { send_msg($request, 0, "Process command: $ccmd\n"); } @@ -922,7 +946,10 @@ sub get_snmpmac { my $ip = shift; my $mac; - my $ccmd = "snmpwalk -Os -v1 -c public $ip ipNetToMediaPhysAddress | grep $ip"; + #Ubuntu only takes OID + #my $ccmd = "snmpwalk -Os -v1 -c public $ip ipNetToMediaPhysAddress | grep $ip"; + my $ccmd = "snmpwalk -Os -v1 -c public $ip 1.3.6.1.2.1.4.22.1.2 | grep $ip"; + if (exists($globalopt{verbose})) { send_msg($request, 0, "Process command: $ccmd\n"); } @@ -937,6 +964,10 @@ sub get_snmpmac { } my ($desc,$mac) = split /: /, $result; + #trim the white space at begin and end of mac + $mac =~ s/^\s+|\s+$//g; + #replace space to : + $mac =~ tr/ /:/; if (exists($globalopt{verbose})) { send_msg($request, 0, "switch mac = $mac\n" ); @@ -959,7 +990,9 @@ sub get_snmphostname { my $ip = shift; my $hostname; - my $ccmd = "snmpwalk -Os -v1 -c public $ip sysName"; + #Ubuntu only takes OID + #my $ccmd = "snmpwalk -Os -v1 -c public $ip sysName"; + my $ccmd = "snmpwalk -Os -v1 -c public $ip 1.3.6.1.2.1.1.5"; if (exists($globalopt{verbose})) { send_msg($request, 0, "Process command: $ccmd\n"); } From 8bad779296a798b770c6cab35ffda399b0019607 Mon Sep 17 00:00:00 2001 From: Casandra Qiu Date: Fri, 29 Apr 2016 10:39:43 -0400 Subject: [PATCH 2/3] Use nmap version number instead of checking OS --- .../lib/xcat/plugins/switchdiscover.pm | 27 ++++++++++--------- 1 file changed, 14 insertions(+), 13 deletions(-) diff --git a/xCAT-server/lib/xcat/plugins/switchdiscover.pm b/xCAT-server/lib/xcat/plugins/switchdiscover.pm index b33b4f713..80fa858d2 100644 --- a/xCAT-server/lib/xcat/plugins/switchdiscover.pm +++ b/xCAT-server/lib/xcat/plugins/switchdiscover.pm @@ -805,7 +805,7 @@ sub snmp_scan { my $result; my $switches; my $counter = 0; - my $sles11_sp4=0; + my $nmap_version; # snmpwalk command has to be available for snmp_scan if (-x "/usr/bin/snmpwalk" ){ @@ -822,20 +822,21 @@ sub snmp_scan { ################################################## my $ranges = get_ip_ranges($request); - #sles11 SP4 has different output than other OS from nmap command - if (-e "/etc/os-release" ) { - $ccmd = "cat /etc/os-release | grep VERSION | grep 11.4"; - $result = xCAT::Utils->runcmd($ccmd, 0); - if ($::RUNCMD_RC == 0) { - $sles11_sp4=1; - } + $ccmd = "nmap -V | grep version"; + $result = xCAT::Utils->runcmd($ccmd, 0); + my @version_array = split / /, $result; + $nmap_version = $version_array[2]; + if (exists($globalopt{verbose})) { + send_msg($request, 0, "version of nmap: $nmap_version\n"); } - # for sles11.4, the line as :"Host 10.4.25.1 appears to be up ... good." - # other OS has line like this: "Discovered open port 161/udp on 10.4.25.1" - # only open port will be scan #use nmap to find if snmp port is enabled - if ($sles11_sp4 == 1) { + # only open port will be scan + # currently, we know nmap version 4.75 has different output for snmp_scan + # command. + # for version 4.75, the line as :"Host 10.4.25.1 appears to be up ... good." + # other higher version has line like this: "Discovered open port 161/udp on 10.4.25.1" + if ($nmap_version <= 4.75) { $ccmd = "/usr/bin/nmap -P0 -v -sU -p 161 -oA snmp_scan @$ranges | grep up | grep good "; } else { $ccmd = "/usr/bin/nmap -P0 -v -sU -p 161 -oA snmp_scan @$ranges | grep 'open port 161' "; @@ -862,7 +863,7 @@ sub snmp_scan { foreach my $line (@lines) { my @array = split / /, $line; my $ip; - if ($sles11_sp4 == 1) { + if ($nmap_version <= 4.75) { $ip = $array[1]; } else { $ip = $array[5]; From 4fed6d751ae4f3189b9ed974ace51a10f8f957fd Mon Sep 17 00:00:00 2001 From: Casandra Qiu Date: Wed, 4 May 2016 11:24:27 -0400 Subject: [PATCH 3/3] Use version_cmp() to compare the version number --- xCAT-server/lib/xcat/plugins/switchdiscover.pm | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/xCAT-server/lib/xcat/plugins/switchdiscover.pm b/xCAT-server/lib/xcat/plugins/switchdiscover.pm index 80fa858d2..1e1a70375 100644 --- a/xCAT-server/lib/xcat/plugins/switchdiscover.pm +++ b/xCAT-server/lib/xcat/plugins/switchdiscover.pm @@ -836,7 +836,7 @@ sub snmp_scan { # command. # for version 4.75, the line as :"Host 10.4.25.1 appears to be up ... good." # other higher version has line like this: "Discovered open port 161/udp on 10.4.25.1" - if ($nmap_version <= 4.75) { + if (xCAT::Utils->version_cmp($nmap_version,"4.75") <= 0) { $ccmd = "/usr/bin/nmap -P0 -v -sU -p 161 -oA snmp_scan @$ranges | grep up | grep good "; } else { $ccmd = "/usr/bin/nmap -P0 -v -sU -p 161 -oA snmp_scan @$ranges | grep 'open port 161' ";