qemu - automation of processes by python -
i'm trying write python script start process , operations atferward. commands want automate script circled red in picture. problem after performing first command, qemu environment run , other commands should executed on qemu environment. want know how can these commands script in python? because know can first command not know how can commands when going qemu environment.
could me how can process?
first thing came mind pexpect, quick search on google turned blog post automatically-testing-vms-using-pexpect-and-qemu seems pretty along lines of doing:
import pexpect image = "fedora-20.img" user = "root" password = "changeme" # define qemu cmd run # important bit redirect serial stdio cmd = "qemu-kvm" cmd += " -m 1024 -serial stdio -net user -net nic" cmd += " -snapshot -hda %s" % image cmd += " -watchdog-action poweroff" # spawn qemu process , log stdout child = pexpect.spawn(cmd) child.logfile = sys.stdout # wait login child.expect('(?i)login:') # , login credentials above child.sendline(user) child.expect('(?i)password:') child.sendline(password) child.expect('# ') # shutdown machine , end process if child.isalive(): child.sendline('init 0') child.close() if child.isalive(): print('child did not exit gracefully.') else: print('child exited gracefully.')
you subprocess.popen
also, checking stdout (qemu)
lines , writing stdin. this:
from subprocess import popen,pipe # pass initial command list of individual args p = popen(["./tracecap/temu","-monitor",.....],stdout=pipe, stdin=pipe) # store next arguments pass args = iter([arg1,arg2,arg3]) # iterate on stdout can check line in iter(p.stdout.readline,""): # if (qemu) @ prompt, enter command if line.startswith("(qemu)"): arg = next(args,"") # if have used args break if not arg: break # else write arg newline p.stdin.write(arg+"\n") print(line)# use see output
where args
contains next commands.
Comments
Post a Comment