Script writing help

Hi all. I’m using opennebula 6.8.0.
I wrote a script that gives me information about virtual machine number 6.
I want to modify it so that information about the last created virtual machine (the machine with the highest ID value) is displayed. Help me do this.

The script:

import requests
import xml.etree.ElementTree as ET
import html

url = ‘http://IP:2633/RPC2

headers = {‘Content-Type’: ‘application/xml’}

username = ‘oneadmin’
password = ‘passwd’
auth = (username, password)

xml_request = <?xml version="1.0"?>
< methodCall>
< methodName>one.vm.info< /methodName>
< params>
< param>
< value>< string>oneadmin:passwd< /string>< /value>
< /param>
< param>
< value>< int>6< /int>< /value>
< /param>
< /params>
</ methodCall>

response = requests.post(url, headers=headers, auth=auth, data=xml_request)

decoded_response = html.unescape(response.text)

root = ET.fromstring(decoded_response)

creator_tag = root.find(“.//UNAME”)
if creator_tag is not None:
creator = creator_tag.text
else:
creator = “CREATOR INFO NOT FOUND.”

vnc_port_tag = root.find(“.//PORT”)
if vnc_port_tag is not None and vnc_port_tag.text:
vnc_port = vnc_port_tag.text
else:
vnc_port = “VNC INFO NOT FOUND.”

print(f"VM CREATOR: {creator}“)
print(f"PORT VNC: {vnc_port}”)

Hi @vadickh!

It looks like you are using Python to develop this script, why don’t you consider using OpenNebula’s Python binding (PyONE)? I think it’s easier and more convenient to use:

import pyone
one = pyone.OneServer("http://one:2633/RPC2", session="oneadmin:onepass")

# The VM Pool method returns an array with all the VMs in the pool.
# All filters are explained in the XML-RPC API documentation.
my_vm = one.vmpool.info(-1,-1,-1,-1).VM[0]
my_vm.get_ID()
my_vm.get_NAME()
my_vm.get_TEMPLATE()

You can find more information here.