Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7907615
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T11:32:04+00:00 2026-06-03T11:32:04+00:00

I’m attempting to write a menu driven modular perl script that will capture user

  • 0

I’m attempting to write a menu driven modular perl script that will capture user input and automate the network configuration process. This script has to be able to install required Arch packages, configure AP mode, configure either DHCP or a static address for the user selected interface and give an option to enable bridging. (EDIT: The script also needs to be able to enable and configure the dhcpd service)

The part I’m stuck on right now is creating a backup of the rc.conf file, reading the file and editing the lines that need to be modified if a network interface has already been statically configured. This script is for use in ArchLinux, I did some searching around and didn’t find anything that met my needs specifically.

Using generic input for
$ip = 1.1.1.1; $Bcast = 2.2.2.2; $netmask = 3.3.3.3; $GW = 4.4.4.4;

I’ve spent about two hours reading about file I/O and tried several things that didn’t work including scrapping the multiple file IO method and using something similar to: while(<IS>){s/^interface.?=(.*)$/"interface=@if[0] \n"/;} with inputs for each of the values that need to be replaced and couldn’t get it to actually do anything.

   if (system ("cat","/etc/rc.conf","|","grep","interface")){   
    use File::Copy "cp";
    $filename = "/etc/rc.conf"; 
    $tempfile = "/etc/rc.tmp";  
    $bak = "/etc/rc.bak";
    cp($filename,$bak);
    open(IS, $filename); 
    open(OS, ">$tempfile"); 
    while(<IS>){ 
        if($_ =~ /^interface.?=(.*)$/){ print OS"interface=@if[0] \n";}
        if($_ =~ /^address.?=(.*)$/){ print OS "address=$ip\n";}
        if($_ =~/^netmask.?=(.*)$/){ print OS "netmask=$netmask\n";}
        if($_ =~/^broadcast.?=(.*)$/){ print OS "broadcast=$Bcast\n";}
        if($_ =~/^gateway.?=(.*)$/){ print OS "gateway=$GW\n"; }
        else {print OS $_;} 
    }
    close(IS); close(OS); 
    unlink($filename); rename($tempfile, $filename);
}

rc.conf before

#
# /etc/rc.conf - Main Configuration for Arch Linux

. /etc/archiso/functions

LOCALE_DEFAULT="en_US.UTF-8"
DAEMON_LOCALE_DEFAULT="no"
CLOCK_DEFAULT="UTC"
TIMEZONE_DEFAULT="Canada/Pacific"
KEYMAP_DEFAULT="us"
CONSOLEFONT_DEFAULT=
CONSOLEMAP_DEFAULT=
USECOLOR_DEFAULT="yes"

LOCALE="$(kernel_cmdline locale ${LOCALE_DEFAULT})"
DAEMON_LOCALE="$(kernel_cmdline daemon_locale ${DAEMON_LOCALE_DEFAULT})"
HARDWARECLOCK="$(kernel_cmdline clock ${CLOCK_DEFAULT})"
TIMEZONE="$(kernel_cmdline timezone ${TIMEZONE_DEFAULT})"
KEYMAP="$(kernel_cmdline keymap ${KEYMAP_DEFAULT})"
CONSOLEFONT="$(kernel_cmdline consolefont ${CONSOLEFONT_DEFAULT})"
CONSOLEMAP="$(kernel_cmdline consolemap ${CONSOLEMAP_DEFAULT})"
USECOLOR="$(kernel_cmdline usecolor ${USECOLOR_DEFAULT})"

MODULES=()

UDEV_TIMEOUT=30
USEDMRAID="no"
USEBTRFS="no"
USELVM="no"

HOSTNAME="archiso"

DAEMONS=(hwclock syslog-ng)

interface=eth0
address=192.168.0.99
netmask=255.255.255.0
broadcast=192.168.0.255
gateway=192.168.0.1

rc.conf after

#
# /etc/rc.conf - Main Configuration for Arch Linux

. /etc/archiso/functions

LOCALE_DEFAULT="en_US.UTF-8"
DAEMON_LOCALE_DEFAULT="no"
CLOCK_DEFAULT="UTC"
TIMEZONE_DEFAULT="Canada/Pacific"
KEYMAP_DEFAULT="us"
CONSOLEFONT_DEFAULT=
CONSOLEMAP_DEFAULT=
USECOLOR_DEFAULT="yes"

LOCALE="$(kernel_cmdline locale ${LOCALE_DEFAULT})"
DAEMON_LOCALE="$(kernel_cmdline daemon_locale ${DAEMON_LOCALE_DEFAULT})"
HARDWARECLOCK="$(kernel_cmdline clock ${CLOCK_DEFAULT})"
TIMEZONE="$(kernel_cmdline timezone ${TIMEZONE_DEFAULT})"
KEYMAP="$(kernel_cmdline keymap ${KEYMAP_DEFAULT})"
CONSOLEFONT="$(kernel_cmdline consolefont ${CONSOLEFONT_DEFAULT})"
CONSOLEMAP="$(kernel_cmdline consolemap ${CONSOLEMAP_DEFAULT})"
USECOLOR="$(kernel_cmdline usecolor ${USECOLOR_DEFAULT})"

MODULES=()

UDEV_TIMEOUT=30
USEDMRAID="no"
USEBTRFS="no"
USELVM="no"

HOSTNAME="archiso"

DAEMONS=(hwclock syslog-ng)

interface=eth0 
interface=eth0
address=1.1.1.1
address=192.168.0.99
netmask=3.3.3.3
netmask=255.255.255.0
broadcast=2.2.2.2
broadcast=192.168.0.255
gateway=4.4.4.4
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-03T11:32:07+00:00Added an answer on June 3, 2026 at 11:32 am

    I am not going to comment on the wisdom of the rest of your script, but you have:

    if (system ("cat","/etc/rc.conf","|","grep","interface")){ 
    

    system returns 0 on success.

    So, you’ll enter the block only if that system call fails.

    If fact, I am on a Windows system right now with no /etc/rc.conf (but cat and grep thanks to Cygwin. Running the following script:

    #!/usr/bin/env perl
    
    use strict; use warnings;
    
    if (system ("cat","/etc/rc.conf","|","grep","interface")){
        print "*** it worked! ***\n";
        if ($? == -1) {
            print "failed to execute: $!\n";
        }
        elsif ($? & 127) {
            printf "child died with signal %d, %s coredump\n",
                ($? & 127),  ($? & 128) ? 'with' : 'without';
        }
        else {
            printf "child exited with value %d\n", $? >> 8;
        }
    }
    

    produces the output:

    cat: /etc/rc.conf: No such file or directory
    cat: |: No such file or directory
    cat: grep: No such file or directory
    cat: interface: No such file or directory
    *** it worked! ***
    child exited with value 1

    That means system returned a failure code. Now, if you want to use shell piping and redirection, you should pass system a string, not a list, and check like this:

    if (system ('cat /etc/rc.conf | grep interface') == 0) {
    

    On the other hand, I would rather not trust shells propagating exit status.

    The following should point you in a better direction:

    #!/usr/bin/env perl
    
    use strict;use warnings;
    
    my %lookup = (
        eth0 => {
            address => '1.1.1.1',
            broadcast => '2.2.2.2',
            netmask => '3.3.3.3',
            gateway => '4.4.4.4',
        },
        wlan0 => {
            address => '5.5.5.5',
            broadcast => '6.6.6.6',
            netmask => '7.7.7.7',
            gateway => '8.8.8.8',
        },
    
    );
    
    while (my $line = <DATA>) {
        if (my ($interface) = ($line =~ /^interface=(\S+)/)) {
            print $line;
            if (exists $lookup{$interface}) {
                $line = process_interface(\*DATA, $lookup{$interface});
                redo;
            }
        }
        else {
            print $line;
        }
    }
    
    sub process_interface {
        my ($fh, $lookup) = @_;
        my $keys = join '|', sort keys %$lookup;
    
        while (my $line = <DATA>) {
            $line =~ s/\A($keys)=.+/$1=$lookup->{$1}/
                or return $line;
            print $line;
        }
    
        return;
    }
    
    __DATA__
    #
    # /etc/rc.conf - Main Configuration for Arch Linux
    
    . /etc/archiso/functions
    
    # stuff
    
    interface=eth0
    address=192.168.0.99
    netmask=255.255.255.0
    broadcast=192.168.0.255
    gateway=192.168.0.1
    interface=wlan0
    address=192.168.0.99
    netmask=255.255.255.0
    broadcast=192.168.0.255
    gateway=192.168.0.1
    

    Output:

    #
    # /etc/rc.conf - Main Configuration for Arch Linux
    
    . /etc/archiso/functions
    
    # stuff
    
    interface=eth0
    address=1.1.1.1
    netmask=3.3.3.3
    broadcast=2.2.2.2
    gateway=4.4.4.4
    interface=wlan0
    address=5.5.5.5
    netmask=7.7.7.7
    broadcast=6.6.6.6
    gateway=8.8.8.8
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I need to clean up various Word 'smart' characters in user input, including but
I need a function that will clean a strings' special characters. I do NOT
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I've got a string that has curly quotes in it. I'd like to replace
I have a French site that I want to parse, but am running into
I am doing a simple coin flipping experiment for class that involves flipping a
I'm trying to create an if statement in PHP that prevents a single post

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.