perl code to change ip

  |   Source

I write this script for a real project. It changes ip of every node in a local network. All the information related to our customer is deleted.

if you see the test() function and you will notice "assert" lines. Yes, I believe in TDD (Test Drivern Development).


#!/usr/bin/perl
require "assert.pl";
if(0){
open(F,"/etc/hosts");
@lines=;
close(F);
foreach (@lines){
    print $_;
}
}#if(0)

############################
# main
############################
if(1){
if(&is_interactive()){
    print "Usage: echo 127.0.0.1 ekcms| ./chgip\n";
    exit 1;
}

$pipein=;
chomp($pipein);
my $h=&who_will_change($pipein);
foreach (keys %$h){
    print "key=$_;val=$h->{$_}\n"; #debug
}
&change_etc_hosts("/etc/hosts",$h);
#distribute /etc/hosts 
foreach my $k (keys %$h){
    system("scp /etc/hosts root@$k:/etc/hosts") || assert(0);
}
&change_ip("/etc/sysconfig/network-scripts",$h);

}#if(0)

############################
# test
############################
if(0){
    &test();
}#if(0)

############################
# subroutines
############################
sub test
{
if(0){
    print &unique_id();
}#if(0)

if(1){
    my $p="127.0.0.1 ekcp1\n0.0.0.0 ekcp2";
    $p.="\n99.99.99.99 ".`hostname`;
    my $h=&who_will_change($p);
    foreach (keys %$h){
        print "key=$_;val=$h->{$_}\n"; #debug
    }
    &change_etc_hosts("hosts",$h);
    #&change_ip("./",$h);
}#if(0)
}

sub is_interactive {
    return -t STDIN && -t STDOUT;
}


# Usage: my $p="127.0.0.1 ekcp1\n0.0.0.0 ekcp2";$h=&chgip($p);print $h->{'ekcp1'};
sub who_will_change
{
    my $p=shift;
    my %h=();
    my @ls=split(/\n/,$p);
    foreach (@ls){
        chomp($_);
        if($_ eq ""){next;}
        my @a=split(/ /);
        if($a[1] eq ""){next;}
        $h{$a[1]}=$a[0];
    }
    return \%h;
}


# Usage: my $p="127.0.0.1 ekcp1\n0.0.0.0 ekcp2";$h=&chgip($p);&change_etc_hosts("/etc/hosts",$h);
sub change_etc_hosts
{
    my $f=shift;
    my $h=shift;
    open(F,$f);
    @lines=;
    close(F);
    foreach my $k (keys %$h){
        #print "k=$k\n"; #debug
        my $replaced=0;
        for(my $i=0;$i<=$#lines;$i++){
            if($lines[$i]!~/^[\d\.]+[\s]+$k\W/){
                next;
            }
            $lines[$i]=~s/^[\d\.]+/$h->{$k}/g;
            $replaced=1;
            #print "line=".$lines[$i]; #debug
        }
        if(!$replaced){
            push(@lines,"$h->{$k} $k\n");
        }
    }
    foreach (@lines){print "line=$_";}; #debug
    open (F,">$f");
    print F (@lines);
    close(F); 
}

#Usage: my $p="127.0.0.1 ekcp1\n0.0.0.0 ekcp2";$h=&chgip($p);&change_ip("/etc/sysconfig/network-scripts",$h);
#Return: 0 - everything ok; 1 - several hosts unreachable
sub change_ip
{
    my $dir=shift;
    my $h=shift;
    my $ret=0;
    #print "dir=$dir\n"; #debug
    my $pingcmd="ping";
    my $s=`uname`;chomp($s);
    if($s eq "Linux"){ $pingcmd="ping -c 2"};
    $s=`hostname`;chomp($s);
    foreach my $k (keys %$h){
        #if(system("$pingcmd $k")!=0){
        #   $ret=1;
        #   next;
        #}
        if( $s eq $k){ #local
            print "s=$s;k=$k\n"; #debug
            system("perl -i.bak -pe 's/IPADDR=.*/IPADDR=$h->{$k}/g' $dir/ifcfg-eth0") || assert(0);
        }
        else{ #remote
if(0){
            my $u=&unique_id();
            system("scp root@$k:$dir/ifcfg-eth0 /tmp/$u.ifcfg-eth0") || assert(0);
            system("perl -i.bak -pe 's/IPADDR=.*/IPADDR=$h->{$k}/g' /tmp/$u.ifcfg-eth0")|| assert(0);
            system("scp /tmp/$u.ifcfg-eth0 root@$k:$dir/ifcfg-eth0 ")|| assert(0) ;
            system("/etc/init.d/network restart")|| assert(0);
}#if(0)
        }
    }
    return $ret;
}

sub unique_id()
{
    my $s=`date +%s`;
    return crypt($s,chr(int(rand(127))));
}


############################
# test data
############################

Comments powered by Disqus