编写一个Python脚本以ping ping IP地址列表,然后返回主机上的或向上还是向上,它只是打印我的IPS

发布于 2025-02-12 05:04:47 字数 1994 浏览 1 评论 0原文

我将脚本写入列表中的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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

今天小雨转甜 2025-02-19 05:04:47

考虑以下内容:

letters = ['A'
'B'
'C']
print(letters)

此打印abc,因为这三个文字之间没有逗号。

这是更有意义的:

letters = ['A',
'B',
'C']
print(letters)

Whitespace在Python中是有意义的,但是线路结尾并不能代替列表分离器,这仍然是

这很清楚:

#!/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.")

Consider this:

letters = ['A'
'B'
'C']
print(letters)

This prints ABC because the three literals don't have commas between them.

This makes more sense:

letters = ['A',
'B',
'C']
print(letters)

Whitespace is meaningful in Python, but line endings don't take the place of list separators, that's still a ,.

This is clearer:

#!/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.")
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文