Untitled

 avatar
unknown
python
4 years ago
4.0 kB
4
Indexable
import os
import paramiko
from paramiko import sftp
from paramiko.ssh_exception import SSHException
from paramiko import AuthenticationException
from logging import error
from proxmoxer import ProxmoxAPI
from proxmoxer import ResourceException
import xml.etree.ElementTree as ET
count = 0
proxmox = ProxmoxAPI('lab1.monocloud.com.tr', user='api@pve',
                     password='YG22YEZdqX69FGan4cTnq7AcfrAdXZGW', verify_ssl=False,service='PVE')
for node in proxmox.nodes.get():
    for qemu in proxmox.nodes.get(f"{node['node']}/qemu"):
        if qemu['status'] == 'running':
            try:
                vm_ip=proxmox.nodes(node['node']).qemu(qemu["vmid"]).agent.get('network-get-interfaces').get('result')
                for x in vm_ip:
                    macadress=x["hardware-address"]
                    if macadress != '00:00:00:00:00:00' and macadress!= 'ee:ee:ee:ee:ee:ee' and x["name"]!='docker0':
                        mactostring=str(macadress)  
                for i in vm_ip:
                    if 'ip-addresses' in i:
                        for j in i['ip-addresses']:
                            if j["prefix"] == 24 and j["ip-address"].startswith("100"):
                                compareid=str(qemu["vmid"])
                                compareip=str(j['ip-address'])
                                if compareid.startswith("100") and compareip.endswith(compareid[3]):
                                    print("ID:{0} Name: {1}  Mac Adress: {2}  IP Adress: {3} matches" .format(compareid,qemu["name"],mactostring,compareip))
                                elif compareid.startswith("10") and compareip.endswith(compareid[2:]):
                                    print("ID:{0} Name: {1}  Mac Adress: {2}  IP Adress: {3} matches" .format(compareid,qemu["name"],mactostring,compareip))
                                elif compareid.startswith("1") and compareip.endswith(compareid[1:]):
                                    print("ID:{0} Name: {1}  Mac Adress: {2} IP Adress: {3} matches" .format(compareid,qemu["name"],mactostring,compareip))
                                else:
                                    print("ID:{0} Name: {1}  Mac Adress: {2}  IP Adress: {3} non matches" .format(compareid,qemu["name"],mactostring,compareip))
                                    count += 1
                                    ip_prefix= '100.101.1.'
                                    eklenecekmac = mactostring
                                    eklenecekdescr = qemu["name"]
                                    if compareid.startswith("100"):
                                        eklenecekip = ip_prefix + compareid[3:]
                                    elif compareid.startswith("10"):
                                        eklenecekip = ip_prefix + compareid[2:]
                                    else:
                                        eklenecekip = ip_prefix + compareid[1:]

            except (ResourceException,KeyError) as error:
                pass 
tree=ET.parse('/home/omertaha/Downloads/config.xml')
root = tree.getroot()
for dhcpd in root.iter('dhcpd'):
    for lan in dhcpd.iter('lan'):
        staticmap=ET.SubElement(lan,'staticmap',attrib={})
        staticmap.tail= '\n'
        staticmap.text = '\n \t'
        mac=ET.SubElement(staticmap,'mac',attrib={})
        mac.tail = '\n \t'
        mac.text=eklenecekmac
        ipaddr=ET.SubElement(staticmap,'ipaddr',attrib={})
        ipaddr.tail = '\n \t'
        ipaddr.text= eklenecekip
        descr=ET.SubElement(staticmap,'descr',attrib={})
        descr.tail = '\n \t'
        descr.text= eklenecekdescr
        winsserver=ET.SubElement(staticmap,'winsserver',attrib={})
        winsserver.tail= '\n \t'
        dnsserver=ET.SubElement(staticmap,'dnsserver',attrib={})
        dnsserver.tail = '\n \t'
        ntpserver=ET.SubElement(staticmap,'ntpserver',attrib={})
        ntpserver.tail = '\n \t'

tree1 = ET.ElementTree(root)
tree1.write("edited.xml")
Editor is loading...