当前位置: 首页 > news >正文

运维-自动访问系统并截图

需求背景

因项目甲方要求需要对系统进行巡检,由于系统服务器较多,并且已经采用Prometheus+Grafana对系统服务器进行管理,如果要完成该任务,需要安排一个人力对各个系统和服务器进行一一截图等操作,费时费力,因此考虑开发一个脚本自动访问各个服务器的CPU、内存等页面并自动截图保存功能。

解决方案

在网上搜了一下,Python提供相关的功能,过程记录如下:

Python版本

C:\Users\admin\Desktop>python -V
Python 3.12.7

安装插件

(1)安装selenium库来控制浏览器,以及Pillow库来处理图像,执行:pip install selenium pillow
(2)打开命令行工具(如CMD、Terminal或PowerShell),运行以下命令来安装webdriver_manager:pip install webdriver-manager

测试代码

from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from webdriver_manager.chrome import ChromeDriverManager
from PIL import Image
import time
import io# 设置ChromeDriver的路径(如果使用webdriver_manager,则无需手动下载和设置路径)
service = Service(ChromeDriverManager().install())
driver = webdriver.Chrome(service=service)try:# 设置窗口大小,方法可行driver.set_window_size(1920, 1080)  # 这里设置窗口大小为1920x1080# 访问指定网页driver.get('http://192.168.1.1/login')# 增加Cookie,避免停留在登录页面,这里的cookie信息,可以通过访问一次grafana获取driver.add_cookie({'name': 'grafana_session', 'value': 'd6847db416b74d5111bea92dbf0b2bdd'})# 访问指定网页driver.get('http://192.168.1.1/d/aka/cimdi-xia-kong-jian?orgId=1&from=now-7d&to=now&var-vendor=&var-account=&var-group=All&var-name=All&var-instance=192.168.1.2&var-interval=2m&var-total=17&var-device=All&var-maxmount=%2Fdata&var-show_name=&var-iid=&var-sname=&viewPanel=7')# 等待网页加载(可选,根据需要调整)time.sleep(2)# 对网页进行截图screenshot = driver.get_screenshot_as_png()# 使用Pillow库保存截图img = Image.open(io.BytesIO(screenshot))img.save('screenshot.png')# 显示截图后的图像(可选)img.show()finally:# 关闭浏览器driver.quit()

当需要访问多个页面并生成多张图片时,优化代码如下:

from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from webdriver_manager.chrome import ChromeDriverManager
from PIL import Image
import time
import io# 设置ChromeDriver的路径(如果使用webdriver_manager,则无需手动下载和设置路径)
service = Service(ChromeDriverManager().install())
driver = webdriver.Chrome(service=service)try:# 设置窗口大小,方法可行driver.set_window_size(1920, 1080)  # 这里设置窗口大小为1920x1080# 访问指定网页driver.get('http://192.168.1.1/login')# 增加Cookiedriver.add_cookie({'name': 'grafana_session', 'value': 'd6847db416b74d5111bea92dbf0b2bdd'})for number in range(1, 50):# 访问指定网页url = 'http://192.168.1.1/d/aka/cimdi-xia-kong-jian?orgId=1&from=now-7d&to=now&var-vendor=&var-account=&var-group=All&var-name=All&var-instance=192.168.1.{}&var-interval=2m&var-total=17&var-device=All&var-maxmount=%2Fdata&var-show_name=&var-iid=&var-sname=&viewPanel=7'.format(number)driver.get(url)# 等待网页加载(可选,根据需要调整)time.sleep(2)# 对网页进行截图screenshot = driver.get_screenshot_as_png()# 使用Pillow库保存截图img = Image.open(io.BytesIO(screenshot))img.save('cpu-{}.png'.format(number))
finally:# 关闭浏览器driver.quit()

执行代码

执行命令python test.py后就会自动截图,这里需要强调一下,这里需要科学上网,否则执行容易报错,且报错信息如下:

C:\Users\admin\Desktop>python test.py
urllib3.exceptions.SSLError: [SSL: UNEXPECTED_EOF_WHILE_READING] EOF occurred in violation of protocol (_ssl.c:1000)The above exception was the direct cause of the following exception:Traceback (most recent call last):File "D:\AppData\Python\Python312\Lib\site-packages\requests\adapters.py", line 667, in sendresp = conn.urlopen(^^^^^^^^^^^^^File "D:\AppData\Python\Python312\Lib\site-packages\urllib3\connectionpool.py", line 843, in urlopenretries = retries.increment(^^^^^^^^^^^^^^^^^^File "D:\AppData\Python\Python312\Lib\site-packages\urllib3\util\retry.py", line 519, in incrementraise MaxRetryError(_pool, url, reason) from reason  # type: ignore[arg-type]^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
urllib3.exceptions.MaxRetryError: HTTPSConnectionPool(host='googlechromelabs.github.io', port=443): Max retries exceeded with url: /chrome-for-testing/latest-patch-versions-per-build.json (Caused by SSLError(SSLEOFError(8, '[SSL: UNEXPECTED_EOF_WHILE_READING] EOF occurred in violation of protocol (_ssl.c:1000)')))During handling of the above exception, another exception occurred:Traceback (most recent call last):File "D:\AppData\Python\Python312\Lib\site-packages\webdriver_manager\core\http.py", line 32, in getresp = requests.get(^^^^^^^^^^^^^File "D:\AppData\Python\Python312\Lib\site-packages\requests\api.py", line 73, in getreturn request("get", url, params=params, **kwargs)^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^File "D:\AppData\Python\Python312\Lib\site-packages\requests\api.py", line 59, in requestreturn session.request(method=method, url=url, **kwargs)^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^File "D:\AppData\Python\Python312\Lib\site-packages\requests\sessions.py", line 589, in requestresp = self.send(prep, **send_kwargs)^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^File "D:\AppData\Python\Python312\Lib\site-packages\requests\sessions.py", line 703, in sendr = adapter.send(request, **kwargs)^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^File "D:\AppData\Python\Python312\Lib\site-packages\requests\adapters.py", line 698, in sendraise SSLError(e, request=request)
requests.exceptions.SSLError: HTTPSConnectionPool(host='googlechromelabs.github.io', port=443): Max retries exceeded with url: /chrome-for-testing/latest-patch-versions-per-build.json (Caused by SSLError(SSLEOFError(8, '[SSL: UNEXPECTED_EOF_WHILE_READING] EOF occurred in violation of protocol (_ssl.c:1000)')))During handling of the above exception, another exception occurred:Traceback (most recent call last):File "C:\Users\admin\Desktop\test.py", line 9, in <module>service = Service(ChromeDriverManager().install())^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^File "D:\AppData\Python\Python312\Lib\site-packages\webdriver_manager\chrome.py", line 40, in installdriver_path = self._get_driver_binary_path(self.driver)^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^File "D:\AppData\Python\Python312\Lib\site-packages\webdriver_manager\core\manager.py", line 35, in _get_driver_binary_pathbinary_path = self._cache_manager.find_driver(driver)^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^File "D:\AppData\Python\Python312\Lib\site-packages\webdriver_manager\core\driver_cache.py", line 107, in find_driverdriver_version = self.get_cache_key_driver_version(driver)^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^File "D:\AppData\Python\Python312\Lib\site-packages\webdriver_manager\core\driver_cache.py", line 154, in get_cache_key_driver_versionreturn driver.get_driver_version_to_download()^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^File "D:\AppData\Python\Python312\Lib\site-packages\webdriver_manager\core\driver.py", line 48, in get_driver_version_to_downloadreturn self.get_latest_release_version()^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^File "D:\AppData\Python\Python312\Lib\site-packages\webdriver_manager\drivers\chrome.py", line 59, in get_latest_release_versionresponse = self._http_client.get(url)^^^^^^^^^^^^^^^^^^^^^^^^^^File "D:\AppData\Python\Python312\Lib\site-packages\webdriver_manager\core\http.py", line 35, in getraise exceptions.ConnectionError(f"Could not reach host. Are you offline?")
requests.exceptions.ConnectionError: Could not reach host. Are you offline?C:\Users\admin\Desktop>

截图效果如下所示:
在这里插入图片描述


http://www.mrgr.cn/news/90697.html

相关文章:

  • Matlab 机器人 雅可比矩阵
  • 关于AURIX在debug时elf文件丢失无法调试
  • 【工业安全】-CVE-2019-17621-D-Link Dir-859L 路由器远程代码执行漏洞
  • PyTorch Lightning LightningDataModule 介绍
  • Elasticjob在同一个实例上运行的多个分片任务
  • 1.1 库的作用与定位
  • 「软件设计模式」工厂方法模式 vs 抽象工厂模式
  • 【Unity Shader编程】之顶点着色器
  • 「软件设计模式」单例模式
  • linux的三剑客和进程处理
  • 200个Python练手项目源码
  • rocketmq-netty通信设计-request和response
  • 【DeepSeek】Deepseek辅组编程-通过卫星轨道计算终端距离、相对速度和多普勒频移
  • 【Stable Diffusion部署至GNU/Linux】安装流程
  • Ubuntu启动geteck/jetlinks实战:Docker启动
  • Proxmox VE 8.3 qm 方式导入ESXi Linux OVA UEFI模式虚拟机
  • C++类和对象进阶:运算符重载深度详解
  • 在vscode中拉取gitee里的项目并运行
  • Python----PyQt开发(PyQt高级:手搓一个文件浏览器)
  • Druid GetConnectionTimeoutException解决方案之一
  • vue-model如何自定义指令,及批量注册自定义指令
  • 【GRPO】GRPO原理原文翻译
  • docker学习---第3步:docker实操大模型
  • Hive增量迁移方案与实操PB级
  • Linux初始化 配置yum源
  • 大数据学习之PB级百战出行网约车二