During taking output of ping test into a textfile,error is showing “FileNotFoundError: [WinError 2] The system cannot find the file specifie”
During taking output of ping test into a textfile,error is showing “FileNotFoundError: [WinError 2] The system cannot find the file specifie”
Currently I am trying to write a code to ping some hosts from text files in python. I want to take that output into a new text file. This is my code
Kindly help to get the solution.
import sys
import os
import platform
import subprocess
plat = platform.system()
scriptDir = sys.path[0]
hosts = os.path.join(scriptDir, 'hosts.txt')
hostsFile = open(hosts, "r")
lines = hostsFile.readlines()
if plat == "Windows":
for line in lines:
line = line.strip()
ping = subprocess.Popen(
["ping", "-n", "10", "-l", "10", "-w", "100", line],
#stdout=subprocess.PIPE,
stderr=subprocess.PIPE
)
out, error = ping.communicate()
print(out)
print(error)
if plat == "Linux":
for line in lines:
line = line.strip()
ping = subprocess.Popen(
["ping", "-c", "1", line],
stdout=subprocess.PIPE,
stderr=subprocess.PIPE
)
out, error = ping.communicate()
print(out)
print(error)
with open("stdout.txt", "w") as out, open("stderr.txt", "wb") as err:
proc=subprocess.Popen("ls", stdout=out, stderr=err)
hostsFile.close()
Thank you.
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.