Commit 603b93fb authored by Mac Newbold's avatar Mac Newbold

Combination of ifc_boot and ifc_all. Sets up network configurations with

ifconfig and in the boot configuration files. Works completely with Linux
and FreeBSD.
parent 88d69d3c
#!/usr/bin/perl -w
my $rsh = "ssh";
my $rcp = "scp";
$| = 1; #Turn off line buffering on output
if ( $#ARGV < 0) {
die("Usage: ifc_boot <ifc_file>\n".
"Sets network interface configurations, both with ifconfig, and\n".
"in the boot configuration files.\n");
}
open(WHO,"/usr/bin/whoami 2>&1 |");
$_ = <WHO>;
chop;
if ($_ ne "root") { die("This won't work unless you're root.\n"); }
close(WHO);
if (-e "$ARGV[0]" ) {
open(IN,$ARGV[0]) || die("Couldn't open $ARGV[0]\n");
} elsif (-e "/usr/testbed/etc/$ARGV[0]") {
open(IN,"/usr/testbed/etc/$ARGV[0]") ||
die("Couldn't open /usr/testbed/etc/$ARGV[0]\n");
} else {
die ("Couldn't locate $ARGV[0]\n");
}
my $last = "";
my $OS = "Unknown";
my $IP = "";
my $IF = 0;
my $ifstr = "";
my $newIP = "";
print "Reading input file...\n";
while (<IN>) {
chop;
s/\s*\#(.*)//;
s/^\s+$//;
s/\s+/ /g;
if ($_ eq "") {next;}
#print "Got $_\n";
($IP,$IF,$newIP)= split(" ",$_);
#print "Config $IP, interface $IF, to IP $newIP\n";
#if its not the same as the last one, get the OS
if ( $IP ne $last ) {
$last = $IP;
print "Pinging $IP...";
if (-e "/bin/ping") {
open(PING,"/bin/ping -c 1 $IP 2>&1 |");
} else {
open(PING,"/sbin/ping -c 1 $IP 2>&1 |");
}
do { } until ( <PING> =~ /transmitted, (\d*) packets received/ );
print "Got $1 ping packet(s) back from $IP.\n";
if ( $1 > 0 ) {
open(UNAME,"$rsh $IP uname -a 2>&1 |");
$_ = <UNAME>;
#print $_;
if ($_ ) { @temp = split(" ",$_); $OS = $temp[0]; }
} else {
print STDERR "Can't reach $IP with ping. Skipping interface $IF.\n";
$OS = "_NO_PING_";
next;
}
print "OS for $IP is $OS \t";
if ($OS eq "Linux") { $ifstr="eth"; }
elsif ($OS eq "FreeBSD") { $ifstr="fxp"; }
else { $ifstr = ""; }
if ($ifstr) { print "Using interface string '$ifstr'\n"; }
} else {
if ($OS eq "_NO_PING_") {
print STDERR "Can't reach $IP with ping. Skipping interface $IF.\n";
next;
}
}
#Set up the interface
if ( $ifstr eq "" ) {
print STDERR "Can't configure OS '$OS' on $IP. Skipping interface $IF.\n";
next;
} elsif ( $OS eq "Linux" ) {
@_ = split(/\./,$newIP);
my $network = join(".",@_[0..2]);
print "Setting $IP $ifstr$IF to $newIP in boot configurations...\n";
open(TEMP,">/tmp/ifc_boot_file");
print TEMP "DEVICE=$ifstr$IF\nIPADDR=$newIP\nNETMASK=255.255.255.0\n".
"NETWORK=$network.0\nBROADCAST=$network.255\nONBOOT=yes\n".
"BOOTPROTO=bootp\nUSERCTL=no\n";
close(TEMP);
open(IFC, "$rcp /tmp/ifc_boot_file ".
"$IP:/etc/sysconfig/network-scripts/ifcfg-$ifstr$IF 2>&1 |");
} elsif ($OS eq "FreeBSD") {
my $exists = 0;
print "Setting $IP $ifstr$IF to $newIP in boot configurations...\n";
open(EXISTS, "$rsh $IP ls /etc/rc.conf.local 2>&1 |");
while ( <EXISTS> ) { if ( ! /No such file/ ) { $exists = 1; } }
close(EXISTS);
open(TEMP,">/tmp/ifc_boot_file");
if ($exists) {
open(COPY, "$rcp $IP:/etc/rc.conf.local /etc/ifc_boot_input 2>&1 |");
while ( <COPY> ) { print "COPY>> $_"; }
close(COPY);
open(CONF,"/etc/ifc_boot_input");
while ( <CONF> ) {
# print "CONF>>$_";
if ( ! /ifconfig_fxp$IF=/ ) {
if ( /network_interfaces\s*=\s*\"([A-Za-z0-9_ ]*)\"/ ) {
$_ = $1;
s/\s+/ /g;
s/^\s+//g;
s/\s+$//g;
my @ifcs = split(" ",$_);
print TEMP "network_interfaces=\"";
foreach my $ifc (@ifcs) {
if ($ifc ne "fxp$IF") { print TEMP "$ifc "; }
}
print TEMP "fxp$IF\"\n";
} else {
print TEMP $_;
}
# print "KEPT\n";
}
}
close(CONF);
} else { print TEMP "network_interfaces=\"fxp$IF lo0\"\n"; }
print TEMP "ifconfig_fxp$IF=\"inet $newIP netmask 255.255.255.0\"\n";
close(TEMP);
open(IFC,"$rcp /tmp/ifc_boot_file $IP:/etc/rc.conf.local 2>&1 |");
} else {
print STDERR "Can't configure OS '$OS' on $IP. Skipping interface $IF.\n";
next;
}
while ( <IFC> ) {
print ">> $_";
}
close(IFC);
print "Setting $IP $ifstr$IF to $newIP with ifconfig...\n";
open(IFC,
"$rsh $IP /sbin/ifconfig $ifstr$IF inet $newIP ".
"netmask 255.255.255.0 2>&1 |"
);
while ( <IFC> ) {
print ">> $_";
}
close(IFC);
}
close(IN);
print "End of input file.\nDone.\n";
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment