主机电源相关测试脚本:ping通 - 停止唤醒

devtools/2024/9/23 6:31:44/

简介:在进行一些涉及服务器或者PC主机的电源关机、开机、重启相关的测试中,远程开机或者唤醒,结合pythonping模块处理ping,可以节省出不必要的硬性等待时间,规避开机时间不稳定的情况,而且不会造成堵塞现象,易于提取。现提供一次性脚本给予各位看官参考。

历史攻略:

Python:用pythonping处理ping

测试网络连接:ping和telnet

gping:升级版ping可视化工具

远程开机:wakeonlan

IPMI开源库pyghmi基本使用

设计思路:

1、初始化为开机状态,支持唤醒或者远程开机

2、设置循环次数,循环开始

3、进行每一轮的指定测试内容

4、关机 或者 重启

5、设置必要的基础等待时间。为保护设备健康,强烈建议等待20 - 30秒

6、开机或者等待开机

7、ping目标主机,如果ping不通则等待10秒(可变),重复执行步骤6。

8、如果ping通,退出唤醒或者相关远程操作

9、重复执行 步骤 2 - 8

案例源码:

# -*- coding: utf-8 -*-
# time: 2024/4/15 21:38
# file: ping_demo.py
# 公众号: 玩转测试开发import time
import subprocess
from pythonping import pingdef sub(command, timeout=360):print(f"{command}")popen = subprocess.Popen(command, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE,universal_newlines=True, bufsize=1)out, err = popen.communicate(timeout=timeout)message = out + errprint(f"{message}")popen.kill()time.sleep(0.1)return popen, messagedef check_online(ip):message = ping(ip)print(message)success_ping = "Reply"if success_ping in str(message):print(f"Ping to {ip} success.")online_result = Trueelse:print(f"Ping to {ip} fail.")online_result = Falsereturn online_resultdef run(count):# initprint("do the init things.")for each_run in range(1, count + 1):print(f"The cycle {each_run} start")print("testing for something.")# shutdown or reboot# shell_command = "sudo shutdown -P now"# sub(f'ssh admin@10.11.12.13 "{shell_command}"')time.sleep(30)# shell_command = "wakeonlan 01:02:03:04:05:06"# sub(f'ssh admin@10.11.12.13 "{shell_command}"')target_ip = "10.11.12.13"wake_time_summary = 0while True:target_result = check_online(target_ip)if target_result:breakelse:time.sleep(10)wake_time_summary += 10# shell_command = "wakeonlan 01:02:03:04:05:06"# sub(f'ssh admin@10.11.12.13 "{shell_command}"')print(f"wake_time_summary:{wake_time_summary}")print(f"The cycle {each_run} finish.")if __name__ == '__main__':count = 2run(count)

运行结果:

图片


http://www.ppmy.cn/devtools/16858.html

相关文章

使用selenium时出现element click intercepted报错的解决办法

win10,python3.8.10。 selenium版本如下(用pip38 show selenium查看): 在定位中,定位了一个按钮(特点:button下还有span然后才是文本),代码如下: from sele…

构造函数及es6类写单例模式

前言,什么叫做单例模式?简单点说,单例模式就是只能创建一个由构造函数及类的实体,多次调用得到的都是第一次new出来的对象。用处有,例如在需要全局管理的资源、频繁访问的配置信息、日志记录器、 数据库连接池 等情况下…

Python中使用Gradient Boosting Decision Trees (GBDT)进行特征重要性分析

在机器学习中,了解哪些特征对模型的预测有重要影响是至关重要的。这不仅帮助我们理解模型的决策过程,还可以指导我们进行特征选择,从而提高模型的效率和准确性。Gradient Boosting Decision Trees(GBDT)是一种强大的集…

mybatis-plus 动态表名简易使用

场景&#xff1a;由于有些表是分表的&#xff0c;需要给表名添加后缀才能正确地访问表&#xff0c;如sys_user_2024_01 代码 依赖版本 <dependency><groupId>com.baomidou</groupId><artifactId>mybatis-plus-boot-starter</artifactId><ve…

VUE3与Uniapp 三 (Class变量和内联样式)

<template><!-- 通过class绑定开启或关闭某个CSS --><view class"box" :class"{box2:true}">box1</view><view class"box" :class"{box2:isActive}">box2</view><!-- 使用三元表达式实现开启关…

力扣爆刷第127天之动态规划五连刷(整数拆分、一和零、背包)

力扣爆刷第127天之动态规划五连刷&#xff08;整数拆分、一和零、背包&#xff09; 文章目录 力扣爆刷第127天之动态规划五连刷&#xff08;整数拆分、一和零、背包&#xff09;关于0 1 背包问题的总结01背包遍历顺序&#xff1a;完全背包遍历顺序&#xff1a; 一、343. 整数拆…

vue2实现字节流byte[]数组的图片预览

项目使用vantui框架&#xff0c;后端返回图片的字节流byte[]数组&#xff0c;在移动端实现预览&#xff0c;实现代码如下&#xff1a; <template><!-- 附件预览 --><div class"file-preview-wrap"><van-overlay :show"show"><…

js网络请求---fetch和XMLHttpRequest的用法

fetch 语法规则 let promise fetch(url, [options]) //url —— 字符串&#xff1a;要访问的 URL。 //options —— 对象&#xff1a;可选参数&#xff1a;method&#xff0c;header 等。 fetch函数返回一个promise&#xff0c;若存在网络问题&#xff0c;或网址不存在&…