<?xml version="1.0" encoding="windows-1252"?>
<node id="85051" title="netswap.pl" created="2001-06-01 19:59:21" updated="2005-07-19 14:08:39">
<type id="1748">
sourcecode</type>
<author id="81246">
malloc</author>
<data>
<field name="doctext">
&lt;code&gt;
#! /usr/bin/perl -i

=head1 NAME 

netswap.pl

=head1 AUTHOR 

Daniel Kelemen
 
=head1 SYNOPSIS

netswap.pl [FILENAME]-

    Scan the configuration file and setup local 
networking parameters based upon contents.

nconf files should be of form:

  hostname:   foo
  domainname: bar.com
  ip:         192.168.16.42
  subnetmask: 255.255.0.0
  gateway:    192.168.1.1
  dns:        192.168.2.1

=head1 DESCRIPTION

Switch local network parameters

=cut

my $line = my $param_name = my $param_val = "";
my $filename = shift || die "Usage: netswap.pl CONFILENAME\n";
open(FH,"$filename") || die "Could not open network configuration file: [$filename] for reading.\n";
while(&lt;FH&gt;){
    $line = $_;
    next unless ($line !~ m/^\s*$/) or ($line !~ m/^\s*\#/);
    $line =~ m/^(.*):(.*)$/;
    $param_name = $1;
    $param_val = $2;
    $param_name =~ s/^\s*//g;
    $param_name =~ s/\s*$//g;
    $param_val =~ s/^\s*//g;
    $param_val =~ s/\s.*$//g;
    ${$param_name} = $param_val;
}
close FH;
print "new hostname = [$hostname]\nnew domainname = [$domainname]\nnew ip = [$ip]\nnew Subnet Mask = [$subnetmask]\nnew DNS = [$dns]\nnew gateway = [$gateway]\n";

# We do /etc/hosts:
my %pass = ("^.*$hostname.*" =&gt; "$ip\t$hostname.$domainname\t$hostname"); # Replace the whole line for this hostname.
file_edit("/etc/hosts",\%pass);

# We do /etc/sysconfig/network:
%pass = ("^GATEWAY.*" =&gt; "GATEWAY=\"$gateway\"","^HOSTNAME.*" =&gt; "HOSTNAME=\"$hostname\"","^DOMAINNAME.*" =&gt; "DOMAINNAME=\"$domainname\"");
file_edit("/etc/sysconfig/network",\%pass);

# We do /etc/sysconfig/network-scripts/ifcfg-eth0;
%pass =  ("^IPADDR.*" =&gt; "IPADDR=\"$ip\"","^NETMASK.*" =&gt; "NETMASK=\"$subnetmask\""); file_edit("/etc/sysconfig/network-scripts/ifcfg-eth0",\%pass);

# We do the /etc/resolv.conf:
%pass = ("^nameserver.*" =&gt; "nameserver $dns","^SEARCH.*" =&gt; "SEARCH $domainname");
file_edit("/etc/resolv.conf",\%pass);

`/etc/init.d/network restart`; # FIN

########################file_edit###################

=head1 file_edit(@)

accepts: list containing filename to edit as first item,
hash reference as second item.  Hash should contain search expressions
as keys, replacement expressions as values.  eg.

    file_edit("/etc/hosts",(foo =&gt; bar));

will replace all occurrences of 'foo' with 'bar' in /etc/hosts.

=cut

sub file_edit(@) {
    my $filename = shift;
    my $hash_ref = shift;
    my %lines = %$hash_ref;
    die "File [$filename] cannot be found!\n" unless (-e $filename);
    @ARGV = ($filename); # put the filename where perl can find it.
    while(&lt;&gt;){ 
 $line = $_;
 foreach $search (keys %lines){
     if ($line =~ m/$search/i) { # not sure about insensitivity here.
  $line =~ s/$search/$lines{$search}/ig;
     }
 }
 print $line;
    } # End while line read for file

} # End file_edit.

&lt;/CODE&gt;</field>
<field name="codedescription">
This little script swaps out all of your linux network parameters based upon the contents of a configuration file, the structure of which is shown in the pod. It is intended mostly for use by people who use linux on a laptop, and frequently hop between networks where they have static IP's, as I do often, so i just keep a config file for each network.  It works fine for me, and I hope some others will find it useful. Please feel free to comment on it, especially style/coding practices.  Thanks for lookin,

--malloc</field>
<field name="codecategory">
linux/networking</field>
<field name="codeauthor">
malloc
Daniel Kelemen
dan@ajvar.org</field>
</data>
</node>
