Я пытаюсь прочитать данные из tcpdump, но не получаю правильный вывод
import socket
import colorama
import time
import os
import csv
from datetime import datetime
colorama.init()
BLUE = colorama.Fore.BLUE
GRAY = colorama.Fore.LIGHTBLACK_EX
RED = colorama.Fore.RED
GREEN = colorama.Fore.GREEN
YELLOW = colorama.Fore.YELLOW
RESET = colorama.Fore.RESET
def preservation():
def data():
data = os.system('tcpdump -i en0 -z 192.168.0.1 -c 10')
return data
signal = str(data())
print(f'{RED}{signal}{RESET}')
while True:
if 'seavers-mbp' in signal:
now = datetime.now()
print(f'{RED}***PACKET FOUND***{RESET}')
print("now = ", now)
dt_string = now.strftime("%d/%m/%Y %H:%M:%S")
caught = [(dt_string), (signal)]
with open('watchdog.csv' 'a') as file:
file_writer = csv.writer(file)
file_writer.writerow(caught)
print(f'{BLUE}cycle complete{RESET}')
time.sleep(.5)
signal = str(data())
preservation()
сигнал возвращается как 0
вместо фактического tcpdump
мне нужно сохранить его в файл pcap и прочитать этот файл или можно сохранить вывод как переменную
(кстати, я запускаю этот скрипт как суперпользователь)
os.system(command)
возвращает код выхода команды, а не вывод.
В документации указано (https://docs.python.org/3/library/os.html#os.system):
Execute the command (a string) in a subshell. This is implemented by calling the Standard C function system(), and has the same limitations. Changes to sys.stdin, etc. are not reflected in the environment of the executed command. If command generates any output, it will be sent to the interpreter standard output stream.
Поэтому вам нужно будет перенаправить вывод в файл или использовать модуль subprocess
для получения вызовов stdout