编写一个Python脚本以ping ping IP地址列表,然后返回主机上的或向上还是向上,它只是打印我的IPS
我将脚本写入列表中的IP地址列表。但是,当我运行脚本时,它只是打印我的所有IP地址,但实际上并没有从我能告诉的。
任何可以告诉我我想念的东西的人,我会最感激。
#!/usr/bin/env python3
import os
ip_list = ['8.8.8.8'
'8.8.4.4'
'1.1.1.1'
'4.4.4.4']
for ip in ip_list:
response = os.popen(f"ping {ip}").read()
if "Received = 4" in response:
print(f"UP {ip} Ping Successful, Host is UP!")
else:
print(f"DOWN {ip} Ping Unsuccessful, Host is DOWN.")
我对代码进行了以下操作,但它仍然不喜欢我正在做的事情。我正在MacOS上的bash壳中跑步。
#!/usr/bin/env python3
import subprocess
ip_list = ['8.8.8.8', '8.8.4.4', '1.1.1.1']
for ip in ip_list:
p = subprocess.run(['ping '+ip])
p.wait()
if p.poll():
print (ip+" is down")
else:
print (ip+" is up")
我从该代码中获得的输出在这里:
matt$ python3 Python_Ping_Public3.py
Traceback (most recent call last):
File "/Users/matt/Desktop/Python_Ping_Public3.py", line 7, in <module>
p = subprocess.run(['ping '+ip])
File "/usr/local/Cellar/[email protected]/3.9.13_1/Frameworks/Python.framework/Versions/3.9/lib/python3.9/subprocess.py", line 505, in run
with Popen(*popenargs, **kwargs) as process:
File "/usr/local/Cellar/[email protected]/3.9.13_1/Frameworks/Python.framework/Versions/3.9/lib/python3.9/subprocess.py", line 951, in __init__
self._execute_child(args, executable, preexec_fn, close_fds,
File "/usr/local/Cellar/[email protected]/3.9.13_1/Frameworks/Python.framework/Versions/3.9/lib/python3.9/subprocess.py", line 1821, in _execute_child
raise child_exception_type(errno_num, err_msg, err_filename)
FileNotFoundError: [Errno 2] No such file or directory: 'ping 8.8.8.8'
I've written my script to ping a list of IP addresses in a list. However, when I run the script, it just prints all of my IP addresses, but doesn't actually ping them from what I can tell.
Anyone that could tell me what I'm missing, I'd be most appreciative.
#!/usr/bin/env python3
import os
ip_list = ['8.8.8.8'
'8.8.4.4'
'1.1.1.1'
'4.4.4.4']
for ip in ip_list:
response = os.popen(f"ping {ip}").read()
if "Received = 4" in response:
print(f"UP {ip} Ping Successful, Host is UP!")
else:
print(f"DOWN {ip} Ping Unsuccessful, Host is DOWN.")
I reworked the code as follows, but it's still not liking something I'm doing. I'm running in a bash shell on macOS.
#!/usr/bin/env python3
import subprocess
ip_list = ['8.8.8.8', '8.8.4.4', '1.1.1.1']
for ip in ip_list:
p = subprocess.run(['ping '+ip])
p.wait()
if p.poll():
print (ip+" is down")
else:
print (ip+" is up")
The output I'm getting from that code is here:
matt$ python3 Python_Ping_Public3.py
Traceback (most recent call last):
File "/Users/matt/Desktop/Python_Ping_Public3.py", line 7, in <module>
p = subprocess.run(['ping '+ip])
File "/usr/local/Cellar/[email protected]/3.9.13_1/Frameworks/Python.framework/Versions/3.9/lib/python3.9/subprocess.py", line 505, in run
with Popen(*popenargs, **kwargs) as process:
File "/usr/local/Cellar/[email protected]/3.9.13_1/Frameworks/Python.framework/Versions/3.9/lib/python3.9/subprocess.py", line 951, in __init__
self._execute_child(args, executable, preexec_fn, close_fds,
File "/usr/local/Cellar/[email protected]/3.9.13_1/Frameworks/Python.framework/Versions/3.9/lib/python3.9/subprocess.py", line 1821, in _execute_child
raise child_exception_type(errno_num, err_msg, err_filename)
FileNotFoundError: [Errno 2] No such file or directory: 'ping 8.8.8.8'
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
考虑以下内容:
此打印
abc
,因为这三个文字之间没有逗号。这是更有意义的:
Whitespace在Python中是有意义的,但是线路结尾并不能代替列表分离器,这仍然是
,
。这很清楚:
Consider this:
This prints
ABC
because the three literals don't have commas between them.This makes more sense:
Whitespace is meaningful in Python, but line endings don't take the place of list separators, that's still a
,
.This is clearer: