방화벽 구성 파일을 사용하여 이름 목록을 대조 확인하는 Bash 스크립트

방화벽 구성 파일을 사용하여 이름 목록을 대조 확인하는 Bash 스크립트

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


두 번째 파일은 목록(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 파일에서 어떤 단어(여기서는 사용자 이름 또는 전체 이름)가 발견되는지와 위치를 언급하는 세 번째 파일에 스크립트 출력이 있으면 좋을 것입니다.

test38이 config.txt 파일 어딘가에 있는지 알고 싶다고 가정해 보겠습니다. 이제 간단히 grep을 수행할 수 있지만 내 list.txt 파일에는 약 100명의 사용자가 있습니다. 나는 grep을 100 번하고 싶지 않습니다. 게다가 앞으로는 더 많은 목록을 얻을 예정입니다.

답변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

관련 정보