[K12OSN] How to change X-terminales networks

Arturo Limón arturo at susetic.com
Sun Oct 26 22:07:29 UTC 2008


After trying all advices you gave me in all posts, I finally decided to code
a perl script to change involved files. I am quite far from being a perl
expert (I use it two or three times a year), but I have done something than
seems to work (at least, it works for me).

I have put variable names and comments in English, so that interested ones
have it easier to follow it than in spanish (my mother tongue). My English,
as my perl, is not perfect either, sorry about that.

The scrip is considering all interfaces are "C" class, and it has been coded
and tested in an k12ltsp-el5 system. The only thing to care about is the new
dhcp-range, that after conversion may yield abnormal IP ranges (I haven't
cared about correcting that, I find it simpler to change just that in the
config file than making more code).

The script is as follows. Al comments and improuvements are welcome.

# ---------------- SCRIPT BEGINS -------------------
#!/usr/bin/perl

# Files involved in lts
@FILES=qw( /etc/dhcpd-k12ltsp.conf
           /etc/exports
           /etc/hosts
           /etc/hosts.allow
           /opt/ltsp/i386/etc/lts.conf
           /opt/ltsp/ppc/etc/lts.conf
         );

# function to calculate "C" class subnets.
sub calc_subnet {
   my $IP=$_[0];
   return substr($IP,0,rindex($IP,".")+1);
}

# checking all available interfaces.
open (IFCONFIG, "/sbin/ifconfig |");
$neth=0;
while (<IFCONFIG>) {
   @linea = split;
   if ($_ =~ /^[a-z].*/) {
      $ETHS[$neth]=$linea[0];
   }
   if ($_ =~ /inet addr.*/) {
      $IPS[$neth] = substr($linea[1],5);
      $neth++;
   }
}
close (IFCONFIG);

# checking active ltsp network
$FICHERO="/opt/ltsp/i386/etc/lts.conf";
open (LTSCONF, $FICHERO) || die( "\nERROR: File $FICHERO not found\n");
while (<LTSCONF>) {
   @linea = split;
   if ($linea[0] eq "SERVER") {
      $ACTUALIP=$linea[2];
   }
}
close(LTSCONF);

# showing available interfaces, and actual lts interface.
print "\nAvailable interfaces:\n\n";
for ($i=0;$i<$neth;$i++) {
   print $IPS[$i]," \t ",$ETHS[$i];
   if ($ACTUALIP eq $IPS[$i]) {
      print " (actual lts interface)";
      $OLDETH=$ETHS[$i];
      $OLDIP=$IPS[$i];
      $OLDSUBNET=calc_subnet($OLDIP);
#      $OLDNET=$OLDSUBNET."0";
#      $OLDBCAST=$OLDSUBNET."255";
   }
   print "\n";
}

# Asking for new lts interface
print "\nNew lts interface (ethx): ";
$NEWETH=<stdin>;

# looking for ip associated to the new lts interface.
chomp($NEWETH);
for ($i=0;$i<$neth;$i++) {
   if ($NEWETH eq $ETHS[$i]) {
      $NEWIP=$IPS[$i];
      $NEWSUBNET=calc_subnet($NEWIP);
#      $NEWNET=$NEWSUBNET."0";
#      $NEWBCAST=$NEWSUBNET."255";
      last;
   }
}

# If introduced interface is no one of the existents.
if ( $i == $neth ) {
   die ("Sorry, unknown interface $NEWETH. Process cancelled.");
}

print "The following files will be changed to new subnet $NEWSUBNET\n";
print "and .bak backup copies will be generated:\n\n";
print "";
foreach $file (@FILES) {
   print $file."\n";
}

print "\nDo you agree (y/n)? ";
$res=<STDIN>;
chomp($res);
if ( lc($res) ne "y" && lc($res) ne "yes" ) {
   print "\nBye ..!!! \n";
}

# check all files do exists; in case ALL are not there, cancel process.
foreach $file (@FILES) {
   if (! -e $file) {
      die("\nFile $file not found. No file changed. Process canceled");
   }
}

# Go ahead ....!!!
print "\n";
foreach $file (@FILES) {
   print "changing file ".$file."\n";
   system( "/bin/sed -i.bak 's/$OLDIP/$NEWIP/g;s/$OLDSUBNET/$NEWSUBNET/g'
$file" );
}

print "\nProcess finished. Do not forget to check 'range dynamic-bootp'";
print "\nin /etc/dhcpd-k12ltsp.conf, and restart dhcp and nfs services.\n";
print "\nCheck too /etc/hosts for the new name of the server\n\n";

# ---------------- SCRIPT ENDS ----------------------

Regards,
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://listman.redhat.com/archives/k12osn/attachments/20081026/ee4e6fba/attachment.htm>


More information about the K12OSN mailing list