【问题标题】:Validating device configuration with shell script使用 shell 脚本验证设备配置
【发布时间】:2019-05-04 09:13:34
【问题描述】:

假设standard.txt是标准配置

config.txt 是我要验证的配置

两个配置都包含类似的设置,但是配置排列或格式可能略有不同。

user@pc:~/$ more standard.txt config.txt 
::::::::::::::
standard.txt
::::::::::::::
ASA-A# show run dns
dns domain-lookup outside
DNS server-group DefaultDNS
    name-server 172.16.51.30 inside
    name-server 8.8.8.8 outside
    name-server 172.16.54.30
    domain-name domain.com
::::::::::::::
config.txt
::::::::::::::
ASA-A# show run dns
dns domain-lookup outside
DNS server-group DefaultDNS
    name-server 172.16.51.30 inside
name-server 172.16.54.30
name-server 8.8.8.8 outside
domain-name domain.com
user@pc:~/$ 

diff 将无法验证它,因为它也会检查格式。

user@pc:~/$ diff standard.txt config.txt 
5,7c5,7
<     name-server 8.8.8.8 outside
<     name-server 172.16.54.30
<     domain-name domain.com
---
> name-server 172.16.54.30
> name-server 8.8.8.8 outside
> domain-name domain.com
user@pc:~/$ 

有没有更好的方法来解决这个问题?

【问题讨论】:

  • 去除空格,对行进行排序,然后进行比较(注意有一个-w 选项)。

标签: linux shell


【解决方案1】:

可能是这样的。它很简单,根据您的需要进行更改:

$ awk '               # using awk
{ $1=$1 }             # rebuild records for space control
NR==FNR {             # process file1
    a[$0]             # hash record to a (add counting if multiple identical records)
    next
}
{                     # process file2
    if($0 in a)       # if matching record in file1
        delete a[$0]  # remove from a
    else              # if not found in a
        print"2:",$0  # output record
}
END {                 # in the END
    for(i in a)       # output all records from file1 which had no match
        print "1:",i  # ... in file2
}' file1 file2

稍微改变一下file1后的输出(嗯,字节:):

2: ASA-A# show run dns
1: ASA-A# show run dnsp

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-04-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多