使用Shell脚本验证设备配置

时间:2019-05-04 09:13:34

标签: linux shell

假设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:~/$ 

是否有更好的方法来解决此问题?

1 个答案:

答案 0 :(得分: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