Obtenha toda a lista de arquivos dos últimos 5 segundos

1

Oi obterá uma lista de linhas do prompt de comando, como segue:

Depois de fazer logread e aplicar alguns filtros

Sat Sep 9 07:28:18 2017 notifications.google.com 192.168.150.201
Sat Sep 9 07:29:18 2017 notifications.google.com 192.168.150.201
Sat Sep 9 07:31:19 2017 plus.l.google.com 192.168.150.201
Sat Sep 9 07:34:19 2017 plus.l.google.com 192.168.150.201
Sat Sep 9 07:34:53 2017 mail.google.com fe80::dc5f:57fd:640c:6661
Sat Sep 9 07:34:53 2017 mail.google.com 192.168.150.128
Sat Sep 9 07:35:53 2017 www.google.com fe80::dc5f:57fd:640c:6661
Sat Sep 9 07:37:53 2017 www.google.com 192.168.150.128
Sat Sep 9 07:37:40 2017 24-courier.push.apple.com 192.168.150.182
Sat Sep 9 07:38:40 2017 www-cdn.icloud.com.akadns.net 192.168.150.182
Sat Sep 9 07:38:40 2017 e6858.dsce9.akamaiedge.net 192.168.150.182
Sat Sep 9 07:38:40 2017 origin.guzzoni-apple.com.akadns.net 192.168.150.182
Sat Sep 9 07:39:46 2017 beacons.gcp.gvt2.com fe80::dc5f:57fd:640c:6661
Sat Sep 9 07:40:46 2017 beacons.gcp.gvt2.com 192.168.150.128

Agora quero obter os únicos registros dos últimos 5 segundos que foram alterados.

Depois de executar o comando de filtro, preciso no seguinte formato

Eu poderia ter feito com logread|awk '$4 > "07:35:00"' , mas o problema é que preciso passar o tempo sempre. Eu há algo como > '5 seconds' , para que eu possa obter o seguinte:

Sat Sep 9 07:37:53 2017 www.google.com 192.168.150.128
    Sat Sep 9 07:37:40 2017 24-courier.push.apple.com 192.168.150.182
    Sat Sep 9 07:38:40 2017 www-cdn.icloud.com.akadns.net 192.168.150.182
    Sat Sep 9 07:38:40 2017 e6858.dsce9.akamaiedge.net 192.168.150.182
    Sat Sep 9 07:38:40 2017 origin.guzzoni-apple.com.akadns.net 192.168.150.182
    Sat Sep 9 07:39:46 2017 beacons.gcp.gvt2.com fe80::dc5f:57fd:640c:6661
    Sat Sep 9 07:40:46 2017 beacons.gcp.gvt2.com 192.168.150.128
    
por Channaveer Hakari 11.09.2017 / 13:53

2 respostas

1

Como usar, consulte a seção "Teste".

#!/bin/bash

# select only dates - five columns from the beginning
dates_only=$(cut -d ' ' -f 1-5 "$2")

# the last date is taking - we will calculate the lower time limit from it.
last_line_time=$(tail -n 1 <(echo "$dates_only"))

# generate the unix time (the amount of seconds from 1970) for
# the each date. It will be the additional column in the next.
dates_to_unix_fmt=$(date '+%s' -f <(echo "$dates_only"))

# find the unix time for the last line time.
print_from=$(date '+%s' -d "${last_line_time} - $1")

# adding the unix time in the beginning of the each line.
# it will be removed in the end by the awk.
paste <(printf "%s\n" $dates_to_unix_fmt) "$2" |
awk -v from="$print_from" '$1 > from {sub(/.*\t/, ""); print;}'

Teste

Entrada (poucas linhas foram adicionadas - para teste)

Sat Sep 9 07:28:18 2017 notifications.google.com 192.168.150.201
Sat Sep 9 07:29:18 2017 notifications.google.com 192.168.150.201
Sat Sep 9 07:31:19 2017 plus.l.google.com 192.168.150.201
Sat Sep 9 07:34:19 2017 plus.l.google.com 192.168.150.201
Sat Sep 9 07:34:53 2017 mail.google.com fe80::dc5f:57fd:640c:6661
Sat Sep 9 07:34:53 2017 mail.google.com 192.168.150.128
Sat Sep 9 07:35:53 2017 www.google.com fe80::dc5f:57fd:640c:6661
Sat Sep 9 07:37:53 2017 www.google.com 192.168.150.128
Sat Sep 9 07:37:40 2017 24-courier.push.apple.com 192.168.150.182
Sat Sep 9 07:38:40 2017 www-cdn.icloud.com.akadns.net 192.168.150.182
Sat Sep 9 07:38:40 2017 e6858.dsce9.akamaiedge.net 192.168.150.182
Sat Sep 9 07:38:40 2017 origin.guzzoni-apple.com.akadns.net 192.168.150.182
Sat Sep 9 07:39:46 2017 beacons.gcp.gvt2.com fe80::dc5f:57fd:640c:6661
Sat Sep 9 07:40:36 2017 beacons.gcp.gvt2.com 192.168.150.128
Sat Sep 9 07:40:41 2017 beacons.gcp.gvt2.com 192.168.150.128
Sat Sep 9 07:40:46 2017 beacons.gcp.gvt2.com 192.168.150.128

Resultado

$ ./time_range.sh "10 sec" input.txt 
Sat Sep 9 07:40:41 2017 beacons.gcp.gvt2.com 192.168.150.128
Sat Sep 9 07:40:46 2017 beacons.gcp.gvt2.com 192.168.150.128

$ ./time_range.sh "20 second" input.txt 
Sat Sep 9 07:40:36 2017 beacons.gcp.gvt2.com 192.168.150.128
Sat Sep 9 07:40:41 2017 beacons.gcp.gvt2.com 192.168.150.128
Sat Sep 9 07:40:46 2017 beacons.gcp.gvt2.com 192.168.150.128

$ ./time_range.sh "4 min" input.txt 
Sat Sep 9 07:37:53 2017 www.google.com 192.168.150.128
Sat Sep 9 07:37:40 2017 24-courier.push.apple.com 192.168.150.182
Sat Sep 9 07:38:40 2017 www-cdn.icloud.com.akadns.net 192.168.150.182
Sat Sep 9 07:38:40 2017 e6858.dsce9.akamaiedge.net 192.168.150.182
Sat Sep 9 07:38:40 2017 origin.guzzoni-apple.com.akadns.net 192.168.150.182
Sat Sep 9 07:39:46 2017 beacons.gcp.gvt2.com fe80::dc5f:57fd:640c:6661
Sat Sep 9 07:40:36 2017 beacons.gcp.gvt2.com 192.168.150.128
Sat Sep 9 07:40:41 2017 beacons.gcp.gvt2.com 192.168.150.128
Sat Sep 9 07:40:46 2017 beacons.gcp.gvt2.com 192.168.150.128

$ ./time_range.sh "2 minute" input.txt 
Sat Sep 9 07:39:46 2017 beacons.gcp.gvt2.com fe80::dc5f:57fd:640c:6661
Sat Sep 9 07:40:36 2017 beacons.gcp.gvt2.com 192.168.150.128
Sat Sep 9 07:40:41 2017 beacons.gcp.gvt2.com 192.168.150.128
Sat Sep 9 07:40:46 2017 beacons.gcp.gvt2.com 192.168.150.128
    
por 12.09.2017 / 00:18
1
dat=$(date -d -5sec +%T)
logread|awk -v dat=$dat '$4 > dat'

Defina o dat com o tempo de 5 segundos atrás e, em seguida, passe isso para o awk usando-o como uma variável.

    
por 11.09.2017 / 14:30