#!/usr/bin/python import os import ovirtsdk.api import ovirtsdk.xml import subprocess import tempfile import urllib # The parameters to connect to the engine: engine_host = "rhel.example.com" engine_port = 443 engine_user = "admin@internal" engine_password = "redhat123" # The name of the vm: vm_name = "myvm" # A template to generate the viewer configuration file: config_template = """\ [virt-viewer] type={type} host={host} port={port} password={password} tls-port={tls_port} fullscreen=0 title={title} enable-smartcard=0 enable-usb-autoshare=1 delete-this-file=1 usb-filter=-1,-1,-1,-1,0 tls-ciphers=DEFAULT host-subject={tls_subject} ca={ca} toggle-fullscreen=shift+f11 release-cursor=shift+f12 secure-channels=main;inputs;cursor;playback;record;display;usbredir;smartcard """ # Connect to the API: api_url = "https://{host}:{port}/api".format( host=engine_host, port=engine_port ) api = ovirtsdk.api.API( url=api_url, username=engine_user, password=engine_password, insecure=True, debug=True ) # Download the CA certificate, as we need to pass this to the viewer so that it # will trust the SSL certificate of the host: ca_url = "https://{host}:{port}/ca.crt".format( host=engine_host, port=engine_port ) ca_path, _ = urllib.urlretrieve(ca_url) with open(ca_path, "r") as ca_file: ca_content = ca_file.read() ca_content = ca_content.replace("\n", "\\n") # Find the VM and get the display details: vm = api.vms.get(name=vm_name, all_content=True) display = vm.get_display() # Request a ticket for the display of the VM: ticket_result = vm.ticket() ticket = ticket_result.get_ticket() # Create the viewer configuration: config_content = config_template.format( type=display.get_type(), host=display.get_address(), port=display.get_port(), password=ticket.get_value(), tls_port=display.get_secure_port(), title=vm_name, tls_subject=display.get_certificate().get_subject(), ca=ca_content ) config_fd, config_path = tempfile.mkstemp() with os.fdopen(config_fd, "w") as config_file: config_file.write(config_content) # Run the viewer: subprocess.call(["remote-viewer", config_path])