I try to execute a ping
process from python:
import subprocess
with open('ips') as f:
line = f.readlines()
for i in line:
results=subprocess.Popen(["ping -c 1" + i])
print(results)
It fails with this error:
Traceback (most recent call last):
File "python_test.py", line 9, in <module>
results=subprocess.Popen(["ping -c 1" + i])
File "/usr/lib/python3.5/subprocess.py", line 676, in __init__
restore_signals, start_new_session)
File "/usr/lib/python3.5/subprocess.py", line 1289, in _execute_child
raise child_exception_type(errno_num, err_msg)
FileNotFoundError: [Errno 2] No such file or directory: 'ping -c 110.11.1.1\n'
What am I doing wrong here?
if you want use the Popen
method, then the program name and arguments need to be separate elements of the list, like this:
for i in lines:
p1 = subprocess.Popen(['ping', '-c 2', i], stdout=subprocess.PIPE)
# Run the command
output = p1.communicate()[0]
print(output)
Read from the official document, may be the run
method more used.
The official document
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.