używam „Re import i sys”Otwórz plik, przeczytaj treść sprawiają, że zawartość do listy za pomocą wyrażenia regularnego, a potem listy drukowania w python
na terminalu, gdy wpisuję „1.py a.txt " chcę ją przeczytać«a.txt», który ma te treści:
17:18:42.525964 IP 66.185.85.146.80 > 192.168.0.15.34436: Flags [.], seq 1:1449, ack 2555, win 1320, options [nop,nop,TS val 3551057710 ecr 43002332], length 1448
17:18:42.526623 IP 66.185.85.146.80 > 192.168.0.15.34436: Flags [.], seq 1449:2897, ack 2555, win 1320, options [nop,nop,TS val 3551057710 ecr 43002332], length 1448
17:18:42.526900 IP 192.168.0.15.34436 > 66.185.85.146.80: Flags [.], ack 2897, win 1444, options [nop,nop,TS val 43002448 ecr 3551057710], length 0
17:18:42.527694 IP 66.185.85.146.80 > 192.168.0.15.34436: Flags [.], seq 2897:14481, ack 2555, win 1320, options [nop,nop,TS val 3551057710 ecr 43002332], length 11584
17:18:42.527716 IP 192.168.0.15.34436 > 66.185.85.146.80: Flags [.], ack 14481, win 1444, options [nop,nop,TS val 43002448 ecr 3551057710], length 0
17:18:42.528794 IP 66.185.85.146.80 > 192.168.0.15.34436: Flags [.], seq 14481:23169, ack 2555, win 1320, options [nop,nop,TS val 3551057710 ecr 43002332], length 8688
17:18:42.528813 IP 192.168.0.15.34436 > 66.185.85.146.80: Flags [.], ack 23169, win 1444, options [nop,nop,TS val 43002448 ecr 3551057710], length 0
17:18:42.545191 IP 192.168.0.15.60030 > 52.2.63.29.80: Flags [.], seq 4113773418:4113774866, ack 850072640, win 270, options [nop,nop,TS val 43002452 ecr 9849626], length 1448
następnie użyć wyrażenia regularnego, aby usunąć wszystko oprócz adresów iP i długości (łącznie), a następnie wydrukować go jako:
source: 66.185.85.146 dest: 192.168.0.15 total:1448
source: 66.185.85.146 dest: 192.168.0.15 total:1448
source: 192.168.0.15 dest: 66.185.85.146 total:0
, ale jeśli istnieje e duplikatów, a następnie będzie to brzmienie, gdzie będzie dodać łączne kwoty duplikatów:
source: 66.185.85.146 dest: 192.168.0.15 total:2896
source: 192.168.0.15 dest: 66.185.85.146 total:0
Ponadto, jeśli wpiszesz „-s” w terminalu tak:
"1.py -s a.txt"
lub
"1.py a.txt -s 192.168.0.15"
powinno rozwiązać przez pierwsze -s, to sortowanie i drukowanie zawartości, a jeśli -s IP, a następnie posortować IPS.
Obecnie to jest to, co mam dla każdego produktu, chcę wiedzieć, jak używać ich wszystkich razem.
#!/usr/bin/python3
import re
import sys
file = sys.argv[1]
a = open(file, "r")
for line in a:
line = line.rstrip()
c = re.findall(r'^(?:[0-9]{1,3}\.){3}[0-9]{1,3}$',line) #Yes I know its not the best regex for this, but I am testing it out for now
d = re.findall(r'\b(\d+)$\b',line)
if len(c) > 0 and len(d) > 0:
print("source:", c[0],"\t","dest:",c[1],"\t", "total:",d[0])
To, co mam do tej pory, nie wiem, jak korzystać z „-s” lub sposób sortowania, a także jak usunąć duplikaty, a także dodać sumy gdy kopie zostały usunięte.
proszę pamiętać, aby zaakceptować odpowiedź, jeśli było to pomocne dla Ciebie. – eLRuLL