0

我正在尝试编写一个菜单驱动的模块化 perl 脚本,该脚本将捕获用户输入并自动化网络配置过程。该脚本必须能够安装所需的 Arch 软件包,配置 AP 模式,为用户选择的接口配置 DHCP 或静态地址,并提供启用桥接的选项。(编辑:脚本还需要能够启用和配置 dhcpd 服务)

我现在坚持的部分是创建 rc.conf 文件的备份,读取文件并编辑需要修改的行(如果已经静态配置了网络接口)。这个脚本是在 ArchLinux 中使用的,我做了一些搜索,没有找到任何满足我需求的东西。

使用通用输入 $ip = 1.1.1.1; $Bcast = 2.2.2.2; $netmask = 3.3.3.3; $GW = 4.4.4.4;

我花了大约两个小时阅读有关文件 I/O 的内容,并尝试了一些不起作用的方法,包括废弃多文件 IO 方法并使用类似的方法:while(<IS>){s/^interface.?=(.*)$/"interface=@if[0] \n"/;}为每个需要替换且不能的值输入输入不能让它真正做任何事情。

   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 之前

#
# /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 之后

#
# /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
4

2 回答 2

1

问题是你的if/////链,应该是/////链。if_ 每行不匹配的触发器,包括匹配的触发器,等等。ifififelseifelsifelsifelsifelsifelseelse { print OS $_ }gateway=interfaceaddress

于 2012-05-02T14:43:26.480 回答
1

我不会评论你剧本其余部分的智慧,但你有:

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

系统返回0成功。

system因此,只有当该调用失败时,您才会进入该块。

事实上,我现在在 Windows 系统上没有(/etc/rc.conf但感谢Cygwin。运行以下脚本:catgrep

#!/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;
    }
}

产生输出:

cat: /etc/rc.conf: 没有这样的文件或目录
cat: |: 没有这样的文件或目录
cat: grep: 没有这样的文件或目录
cat:接口:没有这样的文件或目录
*** 有效!***
孩子以值 1 退出

这意味着system返回了一个失败代码。现在,如果你想使用 shell 管道和重定向,你应该传递system一个字符串,而不是一个列表,然后像这样检查:

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

另一方面,我宁愿不信任传播退出状态的 shell。

以下内容应为您指明更好的方向:

#!/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

输出:

#
# /etc/rc.conf - Arch Linux 的主要配置

. /etc/archiso/functions

# 东西

接口=eth0
地址=1.1.1.1
网络掩码=3.3.3.3
广播=2.2.2.2
网关=4.4.4.4
接口=wlan0
地址=5.5.5.5
网络掩码=7.7.7.7
广播=6.6.6.6
网关=8.8.8.8
于 2012-05-02T14:44:29.377 回答