bash または python のスクリプトを使用して何かを自動化したいと考えています。
デバイスからの設定ファイルがあるとします。単純な config.txt ファイルです。内容は次のようになります (実際のファイルはもっと長く、テキストももっと多くあります)。
> cat config.txt
>
> ASA Version 9.1(5)
> !
> terminal width 511
> hostname confidential
> domain-name confidential
> enable password encrypted
> passwd encrypted
> names
> !
> interface GigabitEthernet0/0
> nameif interconnection
> security-level 50
> ip address 1.1.1.1 255.255.255.240 standby 1.1.1.1
> !
> interface GigabitEthernet0/1
> description Trunk
> no nameif
> no security-level
> no ip address
> !
> interface GigabitEthernet0/1.4
> description confidential
> vlan 4
> nameif vlan004_confidential
> security-level 50
> ip address 1.1.1.1 255.255.255.0
> !
> object network confidential
> host 2.2.2.2
> object network confidential2
> host 3.3.3.3
> object network confidential3
> host 4.4.4.4
>!
>access-list vlan65_access_in extended permit object-group confidential any object-group confidential
>access-list vlan65_access_in remark Allow ICMP OK-20131105
>access-list vlan65_access_in extended permit icmp any object vlan48-confidential
>access-list vlan65_access_in remark Allow NTP OK-20131105
>access-list vlan65_access_in extended permit udp any object-group confidential eq ntp
>access-list warehouse_access_in remark Access to confidential
>access-list warehouse_access_in extended permit object-group confidential any object-group confidential
>access-list warehouse_access_in remark Access to DNS srvrs
>access-list warehouse_access_in extended permit ip any object-group DNS_Servers
>access-list warehouse_access_in remark Allow acces to AD
>!
>no pager
>logging enable
>logging timestamp
>logging standby
>logging list SysLogs message 304001
>logging list connections message 302013-302304
>logging list NewConnection message 302303
>logging list NewConnection message 302015
>logging list NewConnection message 302013
>logging list NewConnection message 303002
>logging list Dropped message 106001-106103
>logging list ConfigChange message 111008
>logging list ConfigChange message 111001
>logging list ConfigChange message 111010
>logging buffer-size 1048576
>logging monitor debugging
>logging buffered warnings
>!
>access-group vlan4_access_in in interface vlan004_confidential1
>access-group vlan65_access_in in interface vlan065_confidential2
>access-group vlan66_access_in in interface vlan066_confidential3
>access-group vlan80_access_in in interface vlan080_confidential4
>!
>service-policy global_policy global
>service-policy test interface interconnection
>service-policy imec_intranet_traffic-policy interface vlan065_confidential5
>service-policy imec_intranet_traffic-policy interface vlan066_confidential6
>service-policy imec_intranet_traffic-policy interface vlan080_confidential7
>service-policy imec_intranet_traffic-policy interface vlan082_confidential8
>service-policy imec_intranet_traffic-policy interface vlan083_confidential9
>!
>: end
2 番目のファイルはリスト (list.txt) です。内容とレイアウトは次のようになります (notepad++ の場合)。
リスト.txt
>username full name employid group left comp on
>----------------------------------------------------------------------------------
>test16 confidential1 00014241 zzzz1 19-08-2017
>test38 confidential2 00014223 zzzz2 12-08-2017
>test47 confidential3 00013986 zzzz3 06-07-2017
>test85 confidential4 00013923 zzzz4 16-07-2017
list.txt ファイルの「ユーザー名」と「フルネーム」列のすべての単語を取得し、config.txt ファイルで一致するものがあるかどうかを確認するスクリプトを実行することは可能ですか? config.txt ファイルで見つかった単語 (ここではユーザー名またはフルネーム) とその場所を示す 3 番目のファイルにスクリプトの出力があると便利です。
たとえば、test38 が config.txt ファイルのどこかにあるかどうか知りたいとします。今は単純に grep できますが、list.txt ファイルには約 100 人のユーザーがいます。100 回 grep したくはありません。その上、将来的にはさらに多くのリストを取得する予定です。
答え1
以下は、この目的を果たす Perl スクリプトです (私の理解する限り)。
#!/usr/bin/perl
use strict;
use warnings;
my $config = 'config.txt'; # give the full path
my $list = 'list.txt'; # give the full path
my $outfile = 'outfile.txt'; # give the full path
# read list file and store in a hash
open my $fhl, '<', $list or die "unable to open '$list': $!";
my %users;
while(my $line = <$fhl>) {
next if $. < 3; # skip first 2 lines (header)
my ($user, $name) = split(/\s+/, $line);
$users{$user} = $name if $user and $name;
}
close $fhl;
open my $fhc, '<', $config or die "unable to open '$config': $!";
open my $out, '>', $outfile or die "unable to open '$outfile': $!";
# read config line by line
while(my $line = <$fhc>) {
# loop on all users
while( my ($u,$n) = each(%users)) {
# print outputfile if user found
print $out "$u:$n found line $.\n" if $line =~ /\b($u|$n)\b/;
}
}
与えられた例の出力ファイル
test38:confidential2 found line 30
test47:confidential3 found line 32
答え2
あなたのリクエストについての私の個人的な理解によれば、組み込みコマンドwhile
とコマンドを使用してawk
解決します。
直接出力
awk 'NR>2{print $1,$2}' list.txt | while IFS=" " read -r username fullname; do awk -v name="${username}" 'BEGIN{OFS="|"}match($0,/'"${fullname}"'/){gsub(/>/,"",name);print name,NR,$0}' config.txt; done
出力をファイルに書き込むoutput.txt
awk 'NR>2{print $1,$2}' list.txt | while IFS=" " read -r username fullname; do awk -v name="${username}" 'BEGIN{OFS="|"}match($0,/'"${fullname}"'/){gsub(/>/,"",name);print name,NR,$0>>"output.txt"}' config.txt; done
出力形式は
user name|line no.|match content
結果出力
test16|64|>access-group vlan4_access_in in interface vlan004_confidential1
test38|30|> object network confidential2
test38|65|>access-group vlan65_access_in in interface vlan065_confidential2
test47|32|> object network confidential3
test47|66|>access-group vlan66_access_in in interface vlan066_confidential3
test85|67|>access-group vlan80_access_in in interface vlan080_confidential4