Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW

by malloc (Pilgrim)
on Jun 01, 2001 at 23:59 UTC ( #85051=sourcecode: print w/ replies, xml ) Need Help??

Category: linux/networking
Author/Contact Info malloc Daniel Kelemen
Description: 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
#! /usr/bin/perl -i

=head1 NAME

=head1 AUTHOR 

Daniel Kelemen

    Scan the configuration file and setup local 
networking parameters based upon contents.

nconf files should be of form:

  hostname:   foo


Switch local network parameters


my $line = my $param_name = my $param_val = "";
my $filename = shift || die "Usage: CONFILENAME\n";
open(FH,"$filename") || die "Could not open network configuration file
+: [$filename] for reading.\n";
    $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 g
+ateway = [$gateway]\n";

# We do /etc/hosts:
my %pass = ("^.*$hostname.*" => "$ip\t$hostname.$domainname\t$hostname
+"); # Replace the whole line for this hostname.

# We do /etc/sysconfig/network:
%pass = ("^GATEWAY.*" => "GATEWAY=\"$gateway\"","^HOSTNAME.*" => "HOST
+NAME=\"$hostname\"","^DOMAINNAME.*" => "DOMAINNAME=\"$domainname\"");

# We do /etc/sysconfig/network-scripts/ifcfg-eth0;
%pass =  ("^IPADDR.*" => "IPADDR=\"$ip\"","^NETMASK.*" => "NETMASK=\"$
+subnetmask\""); file_edit("/etc/sysconfig/network-scripts/ifcfg-eth0"

# We do the /etc/resolv.conf:
%pass = ("^nameserver.*" => "nameserver $dns","^SEARCH.*" => "SEARCH $

`/etc/init.d/network restart`; # FIN


=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 => bar));

will replace all occurrences of 'foo' with 'bar' in /etc/hosts.


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.
 $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.

Comment on
Download Code
Replies are listed 'Best First'.
Re: (style/coding comments)
by OeufMayo (Curate) on Jun 04, 2001 at 02:16 UTC

    malloc asked for comments about his style/coding practices, and I saw his useful program as a good exercise for a code/style review (not golf!).
    I'm not claiming that the proposed code is perfect nor the original code is bad (in fact, I really like the use of the -i switch to edit files in a convenient way in a program), but I hope to give some hints on how can one use perlish features to improve code.

by Anonymous Monk on Jun 02, 2001 at 00:49 UTC
    Only comment i have is that you don't really need the if ($line =~ m/$search/i) { in file_edit(). I'm sure things will work out just fine if you just use s/$search/$lines{$search}/ig; without that if..

Back to Code Catacombs

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: sourcecode [id://85051]
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others romping around the Monastery: (4)
As of 2015-10-09 22:47 GMT
Find Nodes?
    Voting Booth?

    Does Humor Belong in Programming?

    Results (253 votes), past polls