由于公司业务需求,购买了大量云主机分配给业务人员使用,但是业务人员的资源使用回收意识不高,经常导致数百台云主机属于闲置状态,造成严重的资源浪费现象。为了防止资源浪费,所以需要判断他们的云主机是否使用,一个一个的找对应的使用人去问,浪费时间不说,还麻烦。
因此,是否可以开发一个脚本,用来程序来验证云主机的使用情况呢?根据这个思路我们有了一个想法,那就是想办法查看云主机运行的程序软件状态。
import pandas
from winrm.protocol import Protocol
#
def check_software_run(vm_ip, username, password):
try:
# 创建WinRMSession对象
session = Protocol(
endpoint='http://' + vm_ip + ':5985/wsman',
transport='ntlm',
username=username,
password=password,
server_cert_validation='ignore'
)
# 执行PowerShell命令,获取服务器正在运行的程序进程名
shell_id = session.open_shell()
command_id = session.run_command(shell_id, 'tasklist | find "WXWork.exe"')
std_out, std_err, status_code = session.get_command_output(shell_id, command_id)
session.cleanup_command(shell_id, command_id)
session.close_shell(shell_id)
# 输出结果
if std_out.decode() == '':
print('企微未运行')
return vm_ip
else:
print('企微正在运行')
return None
except Exception as e:
print(str(vm_ip)+":IP地址或密码有误")
return None
# 存储未开启企微的ip地址
no_login_wxwork = []
# check_software_run(vm_ip='10.18.3.113', username='hnu1', password='BhwJfHxo4LyXgWr')
filename = r'C:\Users\Administrator\Desktop\虚拟机ip-1.xlsx'
df = pandas.read_excel(filename, sheet_name=0, usecols=['ip', '密码(修改区)', '部门', '使用人','状态'])
for index, row in df.iterrows():
ip = row['ip']
password = row['密码(修改区)']
vm_ip=check_software_run(vm_ip=ip,username='hnu1',password=password)
if vm_ip:
no_login_wxwork.append(vm_ip)
else:
print(str(ip) + ':' + str(password))
# 数据筛选 无企微进程ip数据
screening_df = df[df['ip'].isin(no_login_wxwork)]
# 保存筛选数据
with pandas.ExcelWriter(r'文件路径', engine='openpyxl', mode='w') as w:
screening_df.to_excel(w, index=False, sheet_name='未登录企微ip')
#new_row.to_excel(w, index=False, sheet_name='未登记ip')
print('保存成功')
本文介绍了一种通过编写Python脚本,利用WinRM和PowerShell技术,监测云主机上企业微信(WXWork.exe)的运行状态,以解决业务人员资源不回收导致的云主机闲置问题,实现资源使用的有效监控和自动化管理。
1万+

被折叠的 条评论
为什么被折叠?



