import libvirt
import tkinter as tk
import os
import subprocess
import xml.etree.ElementTree as ET
import uuid
import paramiko
def close_connection(): # 关闭连接
try:
conn.close()
except:
print("--关闭连接失败--")
return 1
print('--成功关闭连接--')
def list_connection(): # 主机信息
set = tk.Tk()
set.title('主机信息')
nodeinfo = conn.getInfo() # 获取虚拟化主机信息
labelNew = tk.Label(set, fg='#000000', font='Helvetica -18 bold', text="node2主机信息:")
labelNew.grid(row=0, column=0, padx=2, pady=6, sticky='W')
labelNew = tk.Label(set, fg='#000000', font='Helvetica -18 bold', text="模型: {}".format(str(nodeinfo[0])))
labelNew.grid(row=1, column=0, padx=2, pady=6, sticky='W')
labelNew = tk.Label(set, fg='#000000', font='Helvetica -18 bold', text="内存大小: {}MB".format(str(nodeinfo[1])))
labelNew.grid(row=2, column=0, padx=2, pady=6, sticky='W')
labelNew = tk.Label(set, fg='#000000', font='Helvetica -18 bold', text="CPU数量: {}".format(str(nodeinfo[2])))
labelNew.grid(row=3, column=0, padx=2, pady=6, sticky='W')
labelNew = tk.Label(set, fg='#000000', font='Helvetica -18 bold', text="虚拟类型: {}".format(conn.getType()))
labelNew.grid(row=4, column=0, padx=2, pady=6, sticky='W')
labelNew = tk.Label(set, fg='#000000', font='Helvetica -18 bold', text="连接URI: {}".format(conn.getURI()))
labelNew.grid(row=5, column=0, padx=2, pady=6, sticky='W')
labelNew = tk.Label(set, fg='#000000', font='Helvetica -18 bold', text="空闲内存: {}bytes".format(conn.getFreeMemory()))
labelNew.grid(row=6, column=0, padx=2, pady=6, sticky='W')
nodeinfo = conn1.getInfo()
labelNew = tk.Label(set, fg='#000000', font='Helvetica -18 bold', text="node1主机信息:")
labelNew.grid(row=0, column=1, padx=2, pady=6, sticky='W')
labelNew = tk.Label(set, fg='#000000', font='Helvetica -18 bold', text="模型: {}".format(str(nodeinfo[0])))
labelNew.grid(row=1, column=1, padx=2, pady=6, sticky='W')
labelNew = tk.Label(set, fg='#000000', font='Helvetica -18 bold', text="内存大小: {}MB".format(str(nodeinfo[1])))
labelNew.grid(row=2, column=1, padx=2, pady=6, sticky='W')
labelNew = tk.Label(set, fg='#000000', font='Helvetica -18 bold', text="CPU数量: {}".format(str(nodeinfo[2])))
labelNew.grid(row=3, column=1, padx=2, pady=6, sticky='W')
labelNew = tk.Label(set, fg='#000000', font='Helvetica -18 bold', text="虚拟类型: {}".format(conn1.getType()))
labelNew.grid(row=4, column=1, padx=2, pady=6, sticky='W')
labelNew = tk.Label(set, fg='#000000', font='Helvetica -18 bold', text="连接URI: {}".format(conn1.getURI()))
labelNew.grid(row=5, column=1, padx=2, pady=6, sticky='W')
labelNew = tk.Label(set, fg='#000000', font='Helvetica -18 bold',
text="空闲内存: {}bytes".format(conn1.getFreeMemory()))
labelNew.grid(row=6, column=1, padx=2, pady=6, sticky='W')
def show_machine(): # 显示虚拟机状态
# node2开启的域
vms_dict = {}
domain_list = conn.listDomainsID()
openID = []
openNAME = []
shutNAME = []
# node1开启的域
vms_dict1 = {}
domain_list1 = conn1.listDomainsID()
openID1 = []
openNAME1 = []
shutNAME1 = []
for vm in domain_list:
vms_dict[str(vm)] = conn.lookupByID(vm).name()
for vm in domain_list1:
vms_dict1[str(vm)] = conn1.lookupByID(vm).name()
if vms_dict:
for id_part, name_part in vms_dict.items():
openID.append(id_part)
openNAME.append(name_part)
if vms_dict1:
for id_part, name_part in vms_dict1.items():
openID1.append(id_part)
openNAME1.append(name_part)
# 关闭的域
global vmd_list
vmd_list = []
for i in conn.listDefinedDomains():
vmd_list.append(i)
global vmd_list1
vmd_list1 = []
for i in conn1.listDefinedDomains():
vmd_list1.append(i)
if vmd_list:
for name_part in vmd_list:
shutNAME.append(name_part)
if vmd_list1:
for name_part in vmd_list1:
shutNAME1.append(name_part)
set = tk.Tk()
set.title('虚拟机状态')
labelNew = tk.Label(set, fg='#000000', font='Helvetica -18 bold', text="node2虚拟机信息:")
labelNew.grid(row=0, column=0, padx=2, pady=6, sticky='W')
labelNew = tk.Label(set, fg='#000000', font='Helvetica -18 bold', text="处于开启状态的虚拟机:")
labelNew.grid(row=1, column=0, padx=2, pady=6, sticky='W')
labelNew = tk.Label(set, fg='#000000', font='Helveti
python连接KVM
最新推荐文章于 2024-05-02 15:40:20 发布
本文介绍如何利用Python的libvirt库连接并管理KVM虚拟机,包括启动、停止、监控等操作,适用于自动化部署和虚拟化管理场景。
摘要由CSDN通过智能技术生成