mirror of
				https://github.com/xcat2/xcat-core.git
				synced 2025-11-03 21:02:34 +00:00 
			
		
		
		
	git-svn-id: https://svn.code.sf.net/p/xcat/code/xcat-core/trunk@104 8638fb3e-16cb-4fca-ae20-7b5d299a9bcd
		
			
				
	
	
		
			83 lines
		
	
	
		
			2.3 KiB
		
	
	
	
		
			Perl
		
	
	
	
	
	
			
		
		
	
	
			83 lines
		
	
	
		
			2.3 KiB
		
	
	
	
		
			Perl
		
	
	
	
	
	
# IBM(c) 2007 EPL license http://www.eclipse.org/legal/epl-v10.html
 | 
						|
package xCAT_plugin::networks;
 | 
						|
use xCAT::Table;
 | 
						|
use Data::Dumper;
 | 
						|
use Sys::Syslog;
 | 
						|
 | 
						|
 | 
						|
sub handled_commands {
 | 
						|
  return {
 | 
						|
    makenetworks => "networks",
 | 
						|
  }
 | 
						|
}
 | 
						|
  
 | 
						|
 | 
						|
sub process_request {
 | 
						|
  my $nettab = xCAT::Table->new('networks',-create=>1,-autocommit=>0);
 | 
						|
  my @rtable = split /\n/,`/bin/netstat -rn`;
 | 
						|
  open($rconf,"/etc/resolv.conf");
 | 
						|
  my @nameservers;
 | 
						|
  if ($rconf) {
 | 
						|
    my @rcont;
 | 
						|
    while (<$rconf>) {
 | 
						|
      push @rcont,$_;
 | 
						|
    }
 | 
						|
    close($rconf);
 | 
						|
    foreach (grep /nameserver/,@rcont) {
 | 
						|
      my $line = $_;
 | 
						|
      my @pair;
 | 
						|
      $line =~ s/#.*//;
 | 
						|
      @pair = split(/\s+/,$line);
 | 
						|
      push @nameservers,$pair[1];
 | 
						|
    }
 | 
						|
  }
 | 
						|
  splice @rtable,0,2;
 | 
						|
  foreach (@rtable) { #should be the lines to think about, do something with U, and something else with UG
 | 
						|
    my $net;
 | 
						|
    my $mask;
 | 
						|
    my $gw;
 | 
						|
    my @ent = split /\s+/,$_;
 | 
						|
    if ($ent[3] eq 'U') {
 | 
						|
      $net = $ent[0];
 | 
						|
      $mask = $ent[2];
 | 
						|
      $nettab->setAttribs({'net'=>$net},{'mask'=>$mask});
 | 
						|
      my $tent = $nettab->getAttribs({'net'=>$net},nameservers);
 | 
						|
      unless ($tent and $tent->{nameservers}) {
 | 
						|
        my $text = join ',',@nameservers;
 | 
						|
        $nettab->setAttribs({'net'=>$net},{nameservers=>$text});
 | 
						|
      }
 | 
						|
      unless ($tent and $tent->{tftpserver}) {
 | 
						|
        my $netdev=$ent[7];
 | 
						|
        my @netlines = split/\n/,`/sbin/ip addr show dev $netdev`;
 | 
						|
        foreach (grep /\s*inet\b/,@netlines) {
 | 
						|
            my @row = split(/\s+/,$_);
 | 
						|
            my $ipaddr = $row[2];
 | 
						|
            $ipaddr =~ s/\/.*//;
 | 
						|
            my @maska=split(/\./,$mask);
 | 
						|
            my @ipa=split(/\./,$ipaddr);
 | 
						|
            my @neta=split(/\./,$net);
 | 
						|
            my $isme=1;
 | 
						|
            foreach (0..3) {
 | 
						|
                my $oct = (0+$maska[$_]) & ($ipa[$_]+0);
 | 
						|
                unless ($oct == $neta[$_]) {
 | 
						|
                    $isme=0;
 | 
						|
                    last;
 | 
						|
                }
 | 
						|
            }
 | 
						|
            if ($isme) {
 | 
						|
                $nettab->setAttribs({'net'=>$net},{tftpserver=>$ipaddr});
 | 
						|
                last;
 | 
						|
            }
 | 
						|
        }
 | 
						|
      }
 | 
						|
      $nettab->commit;
 | 
						|
      #Nothing much sane to do for the other fields at the moment?
 | 
						|
    } elsif ($ent[3] eq 'UG') {
 | 
						|
    #TODO: networks through gateway. and how we might care..
 | 
						|
    } else {
 | 
						|
    #TODO: anything to do with such entries?
 | 
						|
    }
 | 
						|
  }
 | 
						|
}
 | 
						|
1;
 |