Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save shellscriptx/ff6dfcc86f95271733c36ab31434fe05 to your computer and use it in GitHub Desktop.
Save shellscriptx/ff6dfcc86f95271733c36ab31434fe05 to your computer and use it in GitHub Desktop.

Considere o contéudo do arquivo abaixo gerado pelo comando nmap que exibe informações de um determinado domínio.

nmap.log

Nmap scan report for 200-169-122-201.dominio.net.br (200.170.118.201)
Host is up (0.85s latency)
Nmap scan report for 200-169-118-210.dominio.net.br (200.170.118.210)
Host is up (0.49s latency)
Nmap scan report for 200-169-118-223.dominio.net.br (200.170.118.223)
Host is up (0.40s latency)
Nmap scan report for 200-169-118-231.dominio.net.br (200.170.118.231)
Host is up (0.30s latency)
Nmap scan report for 200-169-118-204.dominio.net.br (200.170.118.204)
Host is up (0.74s latency)
Nmap scan report for 200-169-118-217.dominio.net.br (200.170.118.217)
Host is up (0.15s latency)

O objetivo é filtrar o arquivo e listar somente os IP's.

Exemplo 1:

sed -nr 's/^Nmap scan.*\(([^)]+)\)/\1/p' nmap.log

O parâmetro '-n' suprime a saída do buffer padrão enquanto a flag p imprime somente as linhas que iniciam com a expressão Nmap scan.

Exemplo 2:

Utilizando a função regex.groups da biblioteca regex.sh.

#!/bin/bash

source regex.sh

regex.groups '^Nmap scan.*\(([^)]+)\)$' "$(< nmap.log)" $REG_CASE

Saída:

200.170.118.201
200.170.118.210
200.170.118.223
200.170.118.231
200.170.118.204
200.170.118.217
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment