qemu-devel
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [Qemu-devel] How to use monitor socket in python to connect VM?


From: Daniel P. Berrange
Subject: Re: [Qemu-devel] How to use monitor socket in python to connect VM?
Date: Mon, 4 Sep 2017 10:36:23 +0100
User-agent: Mutt/1.8.3 (2017-05-23)

On Mon, Sep 04, 2017 at 02:17:39PM +0800, Sam wrote:
> Hi all,
> 
> I'm using python socket to connect VM's monitor socket like this:
> 
> address@hidden tests]# python
> > Python 2.7.5 (default, Jun 24 2015, 00:41:19)
> > [GCC 4.8.3 20140911 (Red Hat 4.8.3-9)] on linux2
> > Type "help", "copyright", "credits" or "license" for more information.
> > >>> from socket import *
> > >>> sock = socket(AF_INET, SOCK_STREAM, 0)
> > >>> sock.connect(('127.0.0.1', 55902))

NB, running the QEMU monitor on an IP socket is not secure. There
is no authentication, and a monitor connection allows you do fully
exploit the user account QEMU is running under (because the user
can attach arbitrary files as virtual disks and thus access their
content).  You always want to use a UNIX domain socket connection.

> > >>> sock.recv(1024)
> > "QEMU 2.6.0 monitor - type 'help' for more information\r\n(qemu) "
> > >>> sock.recv(1024)

[snip]

> But as we see, there are problems:
> 1. commands like '>>> sock.send('chardev-add
> socket,id=char-vhost_test_intf1,path=/usr/local/var/run/openvswitch/vhost_test_intf1,server=on')
> 106'  does not work, this I'm very sure and I use some method to verify it.
> 2. commands like '>>> sock.recv(1024)' got a lot of unknown characters.

You're trying to interact with the *human* monitor here, so all the
garbage you see is the result of the readline library trying to
do interactive command processing for you.

To automate QEMU you should always use the QMP monitor. eg

  $QEMU -qmp unix:/some/path/qemu.sock

> 
> So is there some one who use python(or shell) to connect monitor socket of
> VM? Could you please share the code or tell me the way to operate?

You might want to look at scripts/qemu.py in the QEMU git tree. It is the
python code we use for communicating with QMP monitor in QEMU test suites.
You can probably use it as a starting point.

> For example, should I send command with '(qemu) ' before my command? Should
> I recv result by the end of '\r\n' or something?

No, you should use QMP and avoid this pain entirely :-)

Regards,
Daniel
-- 
|: https://berrange.com      -o-    https://www.flickr.com/photos/dberrange :|
|: https://libvirt.org         -o-            https://fstop138.berrange.com :|
|: https://entangle-photo.org    -o-    https://www.instagram.com/dberrange :|



reply via email to

[Prev in Thread] Current Thread [Next in Thread]