Perl Scripts
From The Wiki Guide
Contents |
Perl Scripts
Windows Scripts
Here are found scripts proven to work work under Windows of for task where the target is a Windows Server
Linux Scripts
Here are found scripts proven to work work under Linux of for task where the target is a Linux Server
Reconfigure IPs On a Server (Non-Destructive)
- Filename:
RepNotModify.pl
- Description:
This script takes a list of IP addresses (one per line) and uses it to generate new network scripts based on the new IPs. It is non-destructive. It places the new scripts on /etc/sysconfig/network-scripts with a *.bak extension; i.e. icfcg-eth0 is generated as ifcfcg-eth0.bak.
The first ip on the list will be assigned to the script corresponding to the first interface; usually ifcfg-eth0. From here on it will iterate through the IP list and the scripts at the same time. IP 2 will be assigned on ifcfg-eth0:0, 3 will be ifcfg-eth0:1 and so on.
- Code:
#!/usr/bin/perl
die "We need at least the list of IPs!\n" unless ($#ARGV != -1);
#Name of interface we are configuring.
$dev = 'eth0';
#path where the scripts are located.
$ifcfg = '/etc/sysconfig/network-scripts/';
open (IPs, $ARGV[0]);
@newIps = <IPs>;
$searchString = $ifcfg . "ifcfg-$dev" . '*';
#get all ifcfg-$dev* files that do not have bak on their name.
chomp(@confs = `ls $searchString | grep -v bak`);
die "We have more or less IPs on the list than what was previously configured" unless ($#confs == $#newIps);
$count=0;
foreach (@confs){
$newConf = $_ . ".bak";
$newIp = $newIps[$count];
$count++;
open (OLDCONF, "$_") or die "Couldn't open to read $_\n";
open (NEWCONF, "> $newConf") or die "Couldn't open to write $newConf";
while (<OLDCONF>){
/IPADDR/ ? print NEWCONF "IPADDR=$newIp" ? print NEWCONF $_;
}
close (OLDCONF);
close (NEWCONF);
}
Reconfigure IPs On a Server (Destructive)
- Filename:
RepModify.pl
- Description:
This script takes a list of IP addresses (one per line) and uses it to generate new network scripts based on the new IPs. It is destructive. It replaces scripts on /etc/sysconfig/network-scripts/ifcfg-*.
The first ip on the list will be assigned to the script corresponding to the first interface; usually ifcfg-eth0. From here on it will iterate through the IP list and the scripts at the same time. IP 2 will be assigned on ifcfg-eth0:0, 3 will be ifcfg-eth0:1 and so on.
- Code:
#!/usr/bin/perl
die "We need at least the list of IPs!\n" unless ($#ARGV != -1);
#Name of interface we are configuring.
$dev = 'eth0';
#path where the scripts are located.
$ifcfg = '/etc/sysconfig/network-scripts/';
open (IPs, $ARGV[0]);
@newIps = <IPs>;
$searchString = $ifcfg . "ifcfg-$dev" . '*';
#get all ifcfg-$dev* files that do not have bak on their name.
chomp(@confs = `ls $searchString | grep -v bak`);
die "We have more or less IPs on the list than what was previously configured" unless ($#confs == $#newIps);
$count=0;
foreach (@confs){
$newIp = $newIps[$count];
$count++;
$conFile = $_;
open (OLDCONF, "$conFile") or die "Couldn't open to read $conFile\n";
@oldFile = <OLDCONF>;
close (OLDCONF);
open (NEWCONF, "> $conFile") or die "Couldn't open to write $conFile";
foreach (@oldFile){
/IPADDR/ ? print NEWCONF "IPADDR=$newIp" : print NEWCONF $_;
}
close (NEWCONF);
}
Configure New IPs On a Server
- Filename:
ConfNewIp.pl
- Description:
This script takes a list of IP addresses (one per line) and uses it to generate new network scripts based on the new IPs. It is destructive. It replaces scripts on /etc/sysconfig/network-scripts/ifcfg-* and it also creates new scripts as necessary
The first ip on the list will be assigned to the script corresponding to the first interface; usually ifcfg-eth0. From here on it will iterate through the IP list and the scripts at the same time. IP 2 will be assigned on ifcfg-eth0:0, 3 will be ifcfg-eth0:1 and so on.
- Code:
#!/usr/bin/perl
die "We need at least the list of IPs!\n" unless ($#ARGV != -1);
#Name of interface we are configuring.
$dev = 'eth0';
$bootproto='static';
$netmask='255.255.255.0';
$onboot='yes';
#path where the scripts are located.
$ifcfg = '/etc/sysconfig/network-scripts/';
$path = $ifcfg . "ifcfg-$dev";
open (IPs, $ARGV[0]);
while (<IPs>) {
chomp;
/(\d{1,3}\.){3}\d{1,3}/ ? push (@newIps, $_) : next;
}
close(IPs);
#get all ifcfg-$dev* files that do not have bak on their name.
chomp(@confs = `ls $searchString | grep -v bak`);
print "WARNING: We have less IPs on the list than what was previously configured\n" unless ($#confs < $#newIps);
$count=0;
foreach (@newIps) {
#first eth0 has no ':'. Second one is eth0:0. So we have an exception.
$count == 0 ? $conFile = $path : ($conFile = $path . ":". ($count - 1));
-e $conFile ? print "." : print "$conFile not found. Generating a new one\n";
open (NEWCONF, "> $conFile") or die "Couldn't open to write $conFile";
$count == 0 ? print NEWCONF "DEVICE=".$dev."\n" : print NEWCONF "DEVICE=".$dev.":".($count - 1)."\n";
print NEWCONF "BOOTPROTO=".$bootproto."\n";
print NEWCONF "IPADDR=".$_."\n";
print NEWCONF "NETMASK=".$netmask."\n";
print NEWCONF "ONBOOT=".$onboot."\n";
close (NEWCONF);
$count++;
}
print "\n";
Mac Scripts
Here are found scripts proven to work work under Mac of for task where the target is a Mac Server
