#!/usr/bin/python -u import sys, os, libvirt, tty, termios, atexit def reset_term(): termios.tcsetattr(0, termios.TCSADRAIN, attrs) def stdin_callback(watch, fd, events, unused): global run_console readbuf = os.read(fd, 1024) if readbuf.startswith(""): run_console = False return stream.send(readbuf) def stream_callback(stream, events, unused): if events & libvirt.VIR_EVENT_HANDLE_READABLE: receivedData = stream.recv(1024) os.write(0, receivedData) libvirt.virEventRegisterDefaultImpl() atexit.register(reset_term) attrs = termios.tcgetattr(0) tty.setraw(0) connection = libvirt.open(sys.argv[1]) domain = connection.lookupByUUIDString(sys.argv[2]) stream = connection.newStream(libvirt.VIR_STREAM_NONBLOCK) domain.openConsole(None, stream, 0) run_console = True stdin_watch = libvirt.virEventAddHandle(0, libvirt.VIR_EVENT_HANDLE_READABLE, stdin_callback, None) stream.eventAddCallback(libvirt.VIR_STREAM_EVENT_READABLE, stream_callback, None) while run_console: libvirt.virEventRunDefaultImpl()