Script Bash para checar a lista de nomes com um arquivo de configuração de firewall

0

Eu estou olhando para automatizar algo com um script no bash ou python.

Digamos que eu tenha um arquivo de configuração de um dispositivo. Um arquivo config.txt simples. O conteúdo pode ser assim (o arquivo real é muito mais longo e tem muito mais texto):

> 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


Meu segundo arquivo é uma lista (list.txt). E o conteúdo e o layout é algo assim (no notepad ++):

list.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

É possível executar um script que usa todas as palavras na coluna "nome de usuário" e "nome completo" no arquivo list.txt e verificar se existe uma correspondência no arquivo config.txt? Seria ótimo ter a saída do script em um terceiro arquivo que menciona qual palavra (aqui nome de usuário ou nome completo) é encontrada no arquivo config.txt e onde.

Digamos que eu queira saber se o test38 está em algum lugar no arquivo config.txt? Eu agora posso simplesmente grep, mas meu arquivo list.txt tem cerca de 100 usuários. Eu não quero grep 100 vezes. Além disso, vou conseguir mais listas no futuro.

    
por maxim 01.09.2017 / 09:50

2 respostas

1

Aqui está um script perl que faz o trabalho, (tanto quanto eu entendo):

#!/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/;
    }
}

outputfile para o exemplo dado

test38:confidential2 found line 30
test47:confidential3 found line 32
    
por 01.09.2017 / 14:47
0

De acordo com meu entendimento pessoal sobre sua solicitação, eu uso o comando interno while e o comando awk para resolvê-lo.

saída direta

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

grava a saída no arquivo 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

formato de saída é

user name|line no.|match content

Resultado output

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
    
por 27.09.2017 / 11:12