From 1eb3b60e500f08b71591b6b2fe8edadaa0234199 Mon Sep 17 00:00:00 2001 From: daniceexi Date: Fri, 5 Feb 2010 09:31:40 +0000 Subject: [PATCH] move the function get_mac_by_arp from Utils.pm to SvrUtils.pm git-svn-id: https://svn.code.sf.net/p/xcat/code/xcat-core/trunk@5160 8638fb3e-16cb-4fca-ae20-7b5d299a9bcd --- perl-xCAT/xCAT/Utils.pm | 104 -------------------------- xCAT-server/lib/perl/xCAT/PPC.pm | 2 +- xCAT-server/lib/perl/xCAT/SvrUtils.pm | 103 +++++++++++++++++++++++++ xCAT-server/lib/xcat/plugins/blade.pm | 2 +- 4 files changed, 105 insertions(+), 106 deletions(-) diff --git a/perl-xCAT/xCAT/Utils.pm b/perl-xCAT/xCAT/Utils.pm index 6df475b9a..a400e4c39 100644 --- a/perl-xCAT/xCAT/Utils.pm +++ b/perl-xCAT/xCAT/Utils.pm @@ -5594,109 +5594,5 @@ sub full_path return $fullpath; } -#------------------------------------------------------------------------------- - -=head3 get_mac_by_arp - Description: - Get the MAC address by arp protocol - - Arguments: - nodes: a reference to nodes array - display: whether just display the result, if not 'yes', the result will - be written to the mac table. - Returns: - Return a hash with node name as key - Globals: - none - Error: - none - Example: - xCAT::Utils->get_mac_by_arp($nodes, $display); - Comments: - -=cut - -#------------------------------------------------------------------------------- -sub get_mac_by_arp () -{ - my ($class, $nodes, $display) = @_; - - 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; - } - } - - foreach my $n ( @$nodes ) { - if ( $node->{$n}->{reachable} ) { - my $output; - my $IP = xCAT::Utils::toIP( $n ); - if ( xCAT::Utils->isAIX() ) { - $output = `/usr/sbin/arp -a`; - } else { - $output = `/sbin/arp -n`; - } - - my ($ip, $mac); - my @lines = split /\n/, $output; - foreach my $line ( @lines ) { - if ( xCAT::Utils->isAIX() && $line =~ /\((\S+)\)\s+at\s+(\S+)/ ) { - ($ip, $mac) = ($1,$2); - ###################################################### - # Change mac format to be same as linux, but without ':' - # For example: '0:d:60:f4:f8:22' to '000d60f4f822' - ###################################################### - if ( $mac) - { - my @mac_sections = split /:/, $mac; - for my $m (@mac_sections) - { - $m = "0$m" if ( length($m) == 1); - } - $mac = join '', @mac_sections; - } - } elsif ( $line =~ /^(\S+)+\s+\S+\s+(\S+)\s/ ) { - ($ip, $mac) = ($1,$2); - } else { - ($ip, $mac) = (undef,undef); - } - if ( @$IP[1] !~ $ip ) { - ($ip, $mac) = (undef,undef); - } else { - last; - } - } - if ( $ip && $mac ) { - if ( $display ne "yes" ) { - ##################################### - # Write adapter mac to database - ##################################### - my $mactab = xCAT::Table->new( "mac", -create=>1, -autocommit=>1 ); - $mactab->setNodeAttribs( $n,{mac=>$mac} ); - $mactab->close(); - } - $ret{$n} = "MAC Address: $mac"; - } else { - $ret{$n} = "Cannot find MAC Address in arp table, please make sure target node and management node are in same network."; - } - } else { - $ret{$n} = "Unreachable."; - } - } - - return \%ret; -} - - 1; diff --git a/xCAT-server/lib/perl/xCAT/PPC.pm b/xCAT-server/lib/perl/xCAT/PPC.pm index 475a75188..4cc73b0cd 100644 --- a/xCAT-server/lib/perl/xCAT/PPC.pm +++ b/xCAT-server/lib/perl/xCAT/PPC.pm @@ -347,7 +347,7 @@ sub process_command { if (defined($request->{opt}->{d})) { $display = "yes"; } - my $output = xCAT::Utils->get_mac_by_arp($nodes, $display); + my $output = xCAT::SvrUtils->get_mac_by_arp($nodes, $display); my $rsp = (); foreach my $node (keys %{$output}) { diff --git a/xCAT-server/lib/perl/xCAT/SvrUtils.pm b/xCAT-server/lib/perl/xCAT/SvrUtils.pm index 3290eaf76..351ca2b33 100644 --- a/xCAT-server/lib/perl/xCAT/SvrUtils.pm +++ b/xCAT-server/lib/perl/xCAT/SvrUtils.pm @@ -764,5 +764,108 @@ sub update_tables_with_diskless_image } +#------------------------------------------------------------------------------- + +=head3 get_mac_by_arp + Description: + Get the MAC address by arp protocol + + Arguments: + nodes: a reference to nodes array + display: whether just display the result, if not 'yes', the result will + be written to the mac table. + Returns: + Return a hash with node name as key + Globals: + none + Error: + none + Example: + xCAT::Utils->get_mac_by_arp($nodes, $display); + Comments: + +=cut + +#------------------------------------------------------------------------------- +sub get_mac_by_arp () +{ + my ($class, $nodes, $display) = @_; + + 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; + } + } + + foreach my $n ( @$nodes ) { + if ( $node->{$n}->{reachable} ) { + my $output; + my $IP = xCAT::Utils::toIP( $n ); + if ( xCAT::Utils->isAIX() ) { + $output = `/usr/sbin/arp -a`; + } else { + $output = `/sbin/arp -n`; + } + + my ($ip, $mac); + my @lines = split /\n/, $output; + foreach my $line ( @lines ) { + if ( xCAT::Utils->isAIX() && $line =~ /\((\S+)\)\s+at\s+(\S+)/ ) { + ($ip, $mac) = ($1,$2); + ###################################################### + # Change mac format to be same as linux, but without ':' + # For example: '0:d:60:f4:f8:22' to '000d60f4f822' + ###################################################### + if ( $mac) + { + my @mac_sections = split /:/, $mac; + for my $m (@mac_sections) + { + $m = "0$m" if ( length($m) == 1); + } + $mac = join '', @mac_sections; + } + } elsif ( $line =~ /^(\S+)+\s+\S+\s+(\S+)\s/ ) { + ($ip, $mac) = ($1,$2); + } else { + ($ip, $mac) = (undef,undef); + } + if ( @$IP[1] !~ $ip ) { + ($ip, $mac) = (undef,undef); + } else { + last; + } + } + if ( $ip && $mac ) { + if ( $display ne "yes" ) { + ##################################### + # Write adapter mac to database + ##################################### + my $mactab = xCAT::Table->new( "mac", -create=>1, -autocommit=>1 ); + $mactab->setNodeAttribs( $n,{mac=>$mac} ); + $mactab->close(); + } + $ret{$n} = "MAC Address: $mac"; + } else { + $ret{$n} = "Cannot find MAC Address in arp table, please make sure target node and management node are in same network."; + } + } else { + $ret{$n} = "Unreachable."; + } + } + + return \%ret; +} + 1; diff --git a/xCAT-server/lib/xcat/plugins/blade.pm b/xCAT-server/lib/xcat/plugins/blade.pm index 7147efbf8..2ad2e728b 100644 --- a/xCAT-server/lib/xcat/plugins/blade.pm +++ b/xCAT-server/lib/xcat/plugins/blade.pm @@ -1364,7 +1364,7 @@ sub getmacs { } if ($byarp eq "yes") { - my $output = xCAT::Utils->get_mac_by_arp([$node], $display); + my $output = xCAT::SvrUtils->get_mac_by_arp([$node], $display); my @ret = (); foreach my $n (keys %$output) { if ($n ne $node) {