Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical

Comment on

( #3333=superdoc: print w/replies, xml ) Need Help??
#! /usr/bin/perl -i =head1 NAME =head1 AUTHOR Daniel Kelemen =head1 SYNOPSIS [FILENAME]- Scan the configuration file and setup local networking parameters based upon contents. nconf files should be of form: hostname: foo domainname: ip: subnetmask: gateway: dns: =head1 DESCRIPTION Switch local network parameters =cut 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"; while(<FH>){ $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. file_edit("/etc/hosts",\%pass); # We do /etc/sysconfig/network: %pass = ("^GATEWAY.*" => "GATEWAY=\"$gateway\"","^HOSTNAME.*" => "HOST +NAME=\"$hostname\"","^DOMAINNAME.*" => "DOMAINNAME=\"$domainname\""); file_edit("/etc/sysconfig/network",\%pass); # We do /etc/sysconfig/network-scripts/ifcfg-eth0; %pass = ("^IPADDR.*" => "IPADDR=\"$ip\"","^NETMASK.*" => "NETMASK=\"$ +subnetmask\""); file_edit("/etc/sysconfig/network-scripts/ifcfg-eth0" +,\%pass); # We do the /etc/resolv.conf: %pass = ("^nameserver.*" => "nameserver $dns","^SEARCH.*" => "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 => 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(<>){ $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.

In reply to by malloc

Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":

  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?

    What's my password?
    Create A New User
    and all is quiet...

    How do I use this? | Other CB clients
    Other Users?
    Others scrutinizing the Monastery: (4)
    As of 2017-06-23 02:59 GMT
    Find Nodes?
      Voting Booth?
      How many monitors do you use while coding?

      Results (533 votes). Check out past polls.