Скрипт берёт из БД phpipam адреса хостов Proxmox и выполняет на них команды:
import os
import pymysql
import paramiko
import threading
conn = pymysql.connect(read_default_file="~/.my.scripts.cnf")
cur = conn.cursor()
cur.execute ("SELECT INET_NTOA(ip_addr) AS ip FROM ipaddresses WHERE custom_OS = 'Proxmox'")
ip = (list(cur.fetchall()))
cur.close()
conn.close()
def get_vm ():
cli = paramiko.client.SSHClient ()
cli.set_missing_host_key_policy(paramiko.client.AutoAddPolicy ())
cli.connect(hostname=(ipresp[0]), username="root",port='22', password='pass')
stdin_, stdout_, stderr_ = cli.exec_command ("qm list | awk 'NF>1 && NR>1{print $1}'")
stdout_.channel.recv_exit_status ()
vm = stdout_.readlines()
for v in vm:
stdin, stdout, stderr = cli.exec_command ("qm status " + (v))
stdout.channel.recv_exit_status ()
status_vm = stdout.read().decode('ascii').strip()
if "running" in status_vm:
print ("VM " + (v).strip() + " is running!")
else:
print ("VM " + (v).strip() + " is shutdown!")
cli.close ()
def get_ct ():
cli = paramiko.client.SSHClient ()
cli.set_missing_host_key_policy(paramiko.client.AutoAddPolicy ())
cli.connect(hostname=(ipresp[0]), username="root",port='22', password='pass')
stdin, stdout, stderr = cli.exec_command ("pct list | awk 'NF>1 && NR>1{print $1}'")
stdout.channel.recv_exit_status ()
ct = stdout.readlines()
for c in ct:
stdin, stdout, stderr = cli.exec_command ("pct status " + (c))
stdout.channel.recv_exit_status ()
status_ct = stdout.read().decode('ascii').strip()
if "running" in status_ct:
print ("CT " + (c).strip() + " is running!")
else:
print ("CT " + (c).strip() + " is shutdown!")
cli.close ()
if __name__ == '__main__':
for ipresp in ip:
check = os.system("ping -c 1 " + (ipresp[0]) + " >/dev/null 2>&1")
if check == 0:
get_vm_thread = threading.Thread(target=get_vm, args=())
get_ct_thread = threading.Thread(target=get_ct, args=())
get_vm_thread.start()
get_ct_thread.start()
else:
print ("Host %s is down!" % (response[0]))