Bash如果在一堆文件的grep中找到某些字符串,则执行

时间:2019-05-29 06:05:24

标签: linux bash shell

我有一堆设备配置文件,想检查它们的版本是否与我的列表匹配。

首先,我需要检查他们的模型。

第二步:对于每种型号,仅批准特定版本。 示例:

Model 1841, Version 15.1(4)M7, 15.1(4)M10, or 15.2(1)T4 are approved.
Model 2800, Version 15.1(2)GC2, 15.1(4)M10, 15.1(4)M12a are approved.
Model C2960, Version 15.0(2)SE9 and 15.0(2)SE11 are approved.

这是配置文件的示例。

[user@linux]$ more Device*
::::::::::::::
DeviceA
::::::::::::::
DeviceA#sh ver
Cisco IOS Software, 1841 Software (C1841-ADVSECURITYK9-M), Version 15.1(4)M10, RELEASE SOFTWARE (fc2)
::::::::::::::
DeviceB
::::::::::::::
DeviceB#sh ver
Cisco IOS Software, 2800 Software (C2800NM-ADVSECURITYK9-M), Version 15.1(4)M10, RELEASE SOFTWARE (fc2)
::::::::::::::
DeviceC
::::::::::::::
DeviceC#sh ver
Cisco IOS Software, C2960 Software (C2960-LANBASEK9-M), Version 12.2(55)SE8, RELEASE SOFTWARE (fc2)
[user@linux]$

这是我的脚本,用于检查每个配置文件中的模型

#!/bin/bash
if grep --color=auto 'Cisco IOS Software' * > /dev/null
then
    echo found
else
    echo not found
fi

样本输出

[user@linux]$ ./script.sh
found
[user@linux]$

我能够grep第一步的模型。但是仍然不知道如何验证特定型号是否已批准版本。

所需的输出

如果已批准版本,则结果应为OK。 如果不是,则表明设备使用的是未经批准的版本。

示例

DeviceA - Model 1841 - Version 15.1(4)M10 = OK
DeviceB - Model 2800 - Version 15.1(4)M10 = OK
DeviceC - Model C2960, Version 12.2(55)SE8 = NOT OK

我希望这个问题很清楚,否则请告诉我。

2 个答案:

答案 0 :(得分:1)

以下带有注释的脚本:

# our devices input file
cat <<EOF >devices
::::::::::::::
DeviceA
::::::::::::::
DeviceA#sh ver
Cisco IOS Software, 1841 Software (C1841-ADVSECURITYK9-M), Version 15.1(4)M10, RELEASE SOFTWARE (fc2)
::::::::::::::
DeviceB
::::::::::::::
DeviceB#sh ver
Cisco IOS Software, 2800 Software (C2800NM-ADVSECURITYK9-M), Version 15.1(4)M10, RELEASE SOFTWARE (fc2)
::::::::::::::
DeviceC
::::::::::::::
DeviceC#sh ver
Cisco IOS Software, C2960 Software (C2960-LANBASEK9-M), Version 12.2(55)SE8, RELEASE SOFTWARE (fc2)
EOF

# approved list
cat <<EOF >appr
Model 1841, Version 15.1(4)M7, 15.1(4)M10, or 15.2(1)T4 are approved.
Model 2800, Version 15.1(2)GC2, 15.1(4)M10, 15.1(4)M12a are approved.
Model C2960, Version 15.0(2)SE9 and 15.0(2)SE11 are approved.
EOF

# parse that appr list into "<mode> <version>\n" list
# get rid of all that "are approved." and "," and "Version" and "and" and "or"
# it's just lucky it works
<appr sed -e 's/Model \([^,]*\), Version \(.*\) are.*/\1 \2/' \
    -e 's/and//g' -e 's/or//' -e 's/, / /g' -e 's/[[:space:]]\+/ /g' |
# puff, I'm out of ideas, for each model and versions reformat the list
while IFS=", " read -r model versions; do
    while IFS=" " read -d' ' -r version; do
        printf "%s %s\n" "$model" "$version"
    done <<<"$versions"
done > ./appr2
# OK, after we have that, we can move on

#extract "<device>\n<model> <version>\n" from the list of devices
<file sed -n '/sh ver/s/\([^#]*\).*/\1/p; /Cisco/s/[^,]*, \([^ ]*\).*Version \([^,]*\).*/\1 \2/p' |
while 
    # simple read liine and line and model
    IFS= read -r device &&
    IFS=' ' read -r model version
do
    # because of it is formatted, that's pretty and simple
    if grep -q -f ./appr2 <<<"$model $version"; then
        approved_str=""
    else
        approved_str="NOT "
    fi

    # output
    printf "%s - Model %s - Version %s = %sOK\n" "$device" "$model" "$version" "$approved_str"
done        

将输出:

DeviceA - Model 1841 - Version 15.1(4)M10 = OK
DeviceB - Model 2800 - Version 15.1(4)M10 = OK
DeviceC - Model C2960 - Version 12.2(55)SE8 = NOT OK

Model C2960之后没有逗号,我认为这是一个错字。

脚本中最糟糕的部分是所有正则表达式解析。它很容易中断,我不知道它将用于哪种输入。如果您正确格式化了输入文件的格式,则整个脚本将变为:

grep -q -f ./approved_list ./devices_list

./approved_list格式为<mode> <version>\n./devices_list的换行符分隔列表,格式为<device> <mode> <version>\n

答案 1 :(得分:0)

您可以像这样制作自定义的roved.cfg

$ cat approved.cfg
1841.*Version 15.1\(4\)M7
2800.*Version 15.1\(2\)GC2
C2960.*Version 15.0\(2\)SE9
1841.*Version 15.1\(4\)M10
2800.*Version 15.1\(4\)M10
C2960.*Version 15.0\(2\)SE11
1841.*Version 15.2\(1\)T4
2800.*Version 15.1\(4\\)M12a

现在您可以将其用于从设备进行grepping:

echo Hits
grep -Ef approved.cfg Device*
echo Noks
grep -vEf approved.cfg Device* | grep "Cisco IOS Software"

您可以调整输出以符合要求。
更改您的approved.cfg

1841 - Version 15.1(4)M7
2800 - Version 15.1(2)GC2
C2960 - Version 15.0(2)SE9
1841 - Version 15.1(4)M10
2800 - Version 15.1(4)M10
C2960 - Version 15.0(2)SE11
1841 - Version 15.2(1)T4
2800 - Version 15.1(4)M12a

首先寻找有效的sed命令:

grep -H "Cisco IOS" Device* | 
   sed -r 's/([^:]*):[^,]*, ([^ ]*).*Version ([^,]*).*/\1 - Model \2 - Version \3/'

现在您可以将其用于

grep -H "Cisco IOS" Device* |
   sed -r 's/([^:]*):[^,]*, ([^ ]*).*Version ([^,]*).*/\1 - Model \2 - Version \3/' |
   grep -Fvf approved.cfg |
   sed 's/$/ = NOT OK/'

grep -H "Cisco IOS" Device* |
   sed -r 's/([^:]*):[^,]*, ([^ ]*).*Version ([^,]*).*/\1 - Model \2 - Version \3/' |
   grep -Ff approved.cfg |
   sed 's/$/ = OK/'