From: "Daniel P. Berrange" <berrange@redhat.com>
To: Sam <batmanustc@gmail.com>
Cc: QEMU Developers <qemu-devel@nongnu.org>
Subject: Re: [Qemu-devel] How to use monitor socket in python to connect VM?
Date: Mon, 4 Sep 2017 10:36:23 +0100 [thread overview]
Message-ID: <20170904093623.GB7186@redhat.com> (raw)
In-Reply-To: <CAOE=1Z3xuudQenX+Oe11fPDZfrC_d02S50HQ9mJ=RfnJ0B5DUg@mail.gmail.com>
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:
>
> [root@yf-mos-test-net09 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 :|
next prev parent reply other threads:[~2017-09-04 9:36 UTC|newest]
Thread overview: 8+ messages / expand[flat|nested] mbox.gz Atom feed top
2017-09-04 6:17 [Qemu-devel] How to use monitor socket in python to connect VM? Sam
2017-09-04 9:36 ` Daniel P. Berrange [this message]
2017-09-04 10:11 ` Sam
2017-09-04 10:16 ` Daniel P. Berrange
2017-09-04 12:31 ` Sam
2017-09-04 12:32 ` Sam
2017-09-04 13:00 ` Daniel P. Berrange
2017-09-04 12:59 ` Daniel P. Berrange
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20170904093623.GB7186@redhat.com \
--to=berrange@redhat.com \
--cc=batmanustc@gmail.com \
--cc=qemu-devel@nongnu.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).