写一个存活检测的Python脚本
时间: 2024-05-24 18:04:35 浏览: 13
下面是一个简单的 Python 存活检测脚本,可以用来检查特定 URL 是否可访问:
```python
import requests
url = 'https://www.google.com'
try:
response = requests.get(url)
if response.status_code == 200:
print(f"Success! {url} is up and running.")
else:
print(f"Error: {url} returned status code {response.status_code}")
except requests.ConnectionError:
print(f"Error: {url} is down or not accessible.")
```
在这个示例中,我们使用了 requests 库来向指定的 URL 发送 GET 请求。如果请求成功,返回状态码为 200,我们将输出一个成功的消息,否则将输出一个错误消息。
如果发生连接错误,例如由于网络故障或 URL 拼写错误等原因,我们也会输出错误消息。
一些进一步说明:
- 你可以替换 URL 变量以检查其他网站。
- 如果你需要进行循环检测,请将整个代码块封装在一个 while 循环中,并使用 time.sleep() 函数来控制检测之间的延迟时间。
- 如果你需要检查多个 URL,可以将 URL 存储在一个列表中,并在循环中遍历该列表。
相关问题
写一个python内网存活主机以及存活端口的扫描脚本
好的,以下是一段简单的 Python 代码来扫描内网中的存活主机和开放的端口。需要注意的是,这个程序需要在具有管理员权限的系统上运行,因为它需要发送 ICMP Echo 请求和 TCP SYN 请求。
``` python
import socket
import struct
import subprocess
import sys
def ping(ip):
# 发送 ICMP Echo 请求
ping_process = subprocess.Popen(
['ping', '-n', '1', '-w', '1000', ip],
stdout=subprocess.PIPE,
stderr=subprocess.PIPE
)
# 等待进程结束,获取返回结果
out, error = ping_process.communicate()
if 'TTL=' in out.decode('utf-8'):
return True
else:
return False
def scan(ip, port):
# 创建套接字
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.settimeout(1)
try:
# 发送 TCP SYN 请求
sock.connect((ip, port))
sock.close()
return True
except socket.error:
return False
def main():
# 获取本机 IP 地址
local_ip = socket.gethostbyname(socket.gethostname())
# 扫描内网 IP 地址
for i in range(1, 256):
ip = '{0}.{1}.{2}.{3}'.format(
local_ip.split('.')[0],
local_ip.split('.')[1],
local_ip.split('.')[2],
i
)
# 如果 IP 地址存活则扫描端口
if ping(ip):
for port in range(1, 1025):
if scan(ip, port):
print('{0}:{1} is open'.format(ip, port))
if __name__ == '__main__':
main()
```
这个脚本将扫描本地内网中的 IP 地址和端口,输出存活的主机和开放的端口。你可以根据需要更改代码来定制扫描规则和输出格式。
写个检测ip端是否存活的脚本
以下是Python的一种实现方式:
```python
import platform
import subprocess
def ping(host):
"""
检测IP端是否存活
:param host: IP地址或域名
:return: True表示存活,False表示不存活
"""
# 判断操作系统类型
param = '-n' if platform.system().lower() == 'windows' else '-c'
# 执行ping命令
command = ['ping', param, '1', host]
result = subprocess.call(command, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
# 判断ping命令返回值
return result == 0
# 测试
print(ping('127.0.0.1')) # True
print(ping('192.168.1.1')) # True
print(ping('www.baidu.com')) # True
print(ping('10.0.0.1')) # False
```
该脚本通过执行系统的ping命令来检测IP端是否存活,返回值为True表示存活,False表示不存活。需要注意的是,在Windows系统下,ping命令需要加上参数“-n 1”,而在Linux系统下,ping命令需要加上参数“-c 1”。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)