■Cisco CLIで使うsectionコマンドっぽいのをbash+awkで書いてみた。
bashの変数展開のためにスペースが含まれる場合は
ダブルクォーテーションで囲む必要があるが、
十分使えると思う。
$ cat myscripts/section
if [ $# -ne 1 ];then
echo "Usege: cat [text-file] | $0 [keyword|\"key word\"]"
exit 1
fi
awk -v key="$1" 'BEGIN{f=0}/^$|^\041$/{next}{if($0 ~ key){f=1;print $0}else{if(f==1 && $0 ~ /^ /){print $0}else{f=0}}}'
■引数なしの単独実行。
$ ./myscripts/section
Usege: cat [text-file] | ./myscripts/section [keyword|"key word"]
■「cat [text-file]」を与えたときに、
段落としてスペースではじまる行をセクションで表示するべき範囲と定義して、
キーワードに一致するか、段落に該当すれば表示するだけだけど、
一応解説
キーワードをシェル変数をawkの変数に代入。
ダブルクォーテーションで囲うので引数は「$1」
key="$1"
初期値としてフラグ「f」を0にする。
BEGIN{f=0}
空行と先頭が「!」だけの行はスキップ
/^$|^\041$/{next}
一行づつ読み込んだ際にキーワードに一致するか比較、フラグ「f」を1にしてその行を表示。
if($0 ~ key){f=1;print $0}
キーワードに一致しない行でフラグ「f」が1で先頭がスペースで始まっていればその行を表示。そうでなければフラグをクリア。
else{if(f==1 && $0 ~ /^ /){print $0}else{f=0}
■「section line」
$ find GNS3/projects/ -type f -name "startup-config.cfg" | tail -1 | xargs cat | ./myscripts/section line
line con 0
exec-timeout 0 0
privilege level 15
logging synchronous
line aux 0
exec-timeout 0 0
privilege level 15
logging synchronous
line vty 0 4
login
transport input none
■「section con」
$ find GNS3/projects/ -type f -name "startup-config.cfg" | tail -1 | xargs cat | ./myscripts/section con
! Last configuration change at 21:36:08 JST Sun Aug 19 2018
no mmi auto-configure
control-plane
line con 0
exec-timeout 0 0
privilege level 15
logging synchronous
■「section line con」
$ find GNS3/projects/ -type f -name "startup-config.cfg" | tail -1 | xargs cat | ./myscripts/section "line con"
line con 0
exec-timeout 0 0
privilege level 15
logging synchronous
■「section Ether」
$ find GNS3/projects/ -type f -name "startup-config.cfg" | tail -1 | xargs cat | ./myscripts/section Ether
interface Ethernet0/0
no ip address
shutdown
interface Ethernet0/1
no ip address
interface Ethernet0/2
no ip address
shutdown
interface Ethernet0/3
no ip address
shutdown
■「section 0/0」
$ find GNS3/projects/ -type f -name "startup-config.cfg" | tail -1 | xargs cat | ./myscripts/section 0/0
interface Ethernet0/0
no ip address
shutdown
■Linux上に保存されたconfigを探すのにはかどる。
$ find GNS3/projects/ -type f -name "startup-config.cfg" | awk '{print "cat "$0}'| sh | ./myscripts/section "hostname|line con" | tail -10
hostname RP2
line con 0
exec-timeout 0 0
privilege level 15
logging synchronous
hostname OSPFv3-3
line con 0
exec-timeout 0 0
privilege level 15
logging synchronous
■「.」で検索した結果とcatの結果が一致するように、
「!」のみの行と空行をスキップしないように修正。
$ cat myscripts/lsec
if [ $# -ne 1 ];then
echo "Usege: cat [text-file] | $0 [keyword|\"key word\"]"
exit 1
fi
awk -v key="$1" 'BEGIN{f=0} \
{if($0 ~ key) \
{f=1;print $0} \
else{if(f==1 && $0 ~ /^ /) \
{print $0} \
else{f=0} \
} \
}'
$ find GNS3/ -type f -name "startup-config.cfg" | xargs cat | lsec "eigrp| 8 |hostname"
...
hostname Ov3R3
hostname CL-L2
hostname R3
router eigrp 65000
network 10.0.0.0
network 172.16.0.0
passive-interface default
no passive-interface Serial2/0
eigrp stub connected summary
hostname RR6
router eigrp 65002
distribute-list 8 out Ethernet0/0
distribute-list 8 out Ethernet0/1
network 10.6.6.0 0.0.0.255
network 192.168.0.0 0.0.255.255
network 192.168.0.0
passive-interface Ethernet1/0
...
■fortigateでも。
$ cat fgt.cfg | ./myscripts/lsec "^config router rip$"
config router rip
config redistribute "connected"
end
config redistribute "static"
end
config redistribute "ospf"
end
config redistribute "bgp"
end
config redistribute "isis"
end