Reputation: 7
I am tring to write a test case where i download a file(PCAP) file, then i save it with name test1.pcap
then i want it to be validated via tshark
cli tool,
"tshark -r {} -Y http".format(filename)
``
its says ```
OSError(2, 'No such file or directory')
however the file exists when i check it with os.path.exsists(filename)
My code is:
def test_pcap():
.....
pcap_response = requests.get(pcap_url)
assert pcap_response.status_code == 200, "Failed to retrieve PCAP file. Status code:
{pcap_response.status_code}"
assert len(pcap_response.content) > 0, "PCAP file is empty"
print(pcap_response)
print("PCAP file successfully retrieved.,")
with open(pcap_file_path, 'w') as f:
f.write(pcap_response.content)
if pcap_file_path is None:
raise ValueError("PCAP file path is None")
if not os.path.exists(pcap_file_path):
print("PCAP file does not exist")
isValid = validate_pcap(pcap_file_path)
print("pcap validation result is: ",isValid)
process_pcap(pcap_file_path)
assert isValid, "Sanitized PCAP file still contains sensitive information."
def validate_pcap(pcap_file):
if not os.path.exists(pcap_file):
print("File does not exist.", pcap_file)
return False
try:
print("tshark -r {} -Y http".format(pcap_file))
result = subprocess.Popen(
["sudo", "tshark", "-r {}".format(pcap_file), "-Y", "http"],
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
text=True
)
r1, r2 = result.communicate
print("Next step after result and result is: ", result, r1, r2)
if result.returncode == 0 and result.stdout:
print("PCAP file validation passed.")
print("Output:\n", result.stdout)
return True
else:
print("No matching traffic found or tshark error.")
print("Error:\n", result.stderr)
return False
except Exception as e:
print("Error running tshark:", e)
return
Upvotes: 0
Views: 32
Reputation: 169338
You're not printing the same command line you're actually executing, to begin with. Then, you shouldn't need sudo
to run tshark
if you already have a capture file.
I would suggest using shutil.which()
to find tshark:
tshark = shutil.which("tshark")
if not tshark:
raise FileNotFoundError("tshark not found")
cmd = [tshark, "-r", pcap_file, "-Y", "http"]
result = subprocess.Popen(cmd, ...
Upvotes: 1