| 1 | #!/usr/bin/env python3
|
|---|
| 2 |
|
|---|
| 3 | import subprocess
|
|---|
| 4 | import sys
|
|---|
| 5 | import time
|
|---|
| 6 |
|
|---|
| 7 |
|
|---|
| 8 | vm_name, user_name, user_pw = ("Ubuntu-14.04.5-amd64", "albert", "albert")
|
|---|
| 9 | def run_vm_cmd():
|
|---|
| 10 | pout = ''
|
|---|
| 11 | perr = ''
|
|---|
| 12 |
|
|---|
| 13 | cmd = ["vboxmanage", "guestcontrol", vm_name, "run", "/bin/ls", "--username", user_name, "--password", user_pw, "--", "-l", "/home"]
|
|---|
| 14 | proc = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE, universal_newlines=True)
|
|---|
| 15 | pout, perr = proc.communicate()
|
|---|
| 16 | rc = proc.returncode
|
|---|
| 17 | if rc != 0:
|
|---|
| 18 | print('Got error code: rc = ' + str(rc) + ' trying to issue cmd: ' + str(cmd))
|
|---|
| 19 | print('POUT:-------------------------')
|
|---|
| 20 | print(pout)
|
|---|
| 21 | print('PERR:-------------------------')
|
|---|
| 22 | print(perr)
|
|---|
| 23 | sys.exit(10)
|
|---|
| 24 |
|
|---|
| 25 | return pout, perr
|
|---|
| 26 |
|
|---|
| 27 | start_time = time.monotonic()
|
|---|
| 28 | count = 0
|
|---|
| 29 |
|
|---|
| 30 | while True:
|
|---|
| 31 | pout, perr = run_vm_cmd()
|
|---|
| 32 | elapsed_time = time.monotonic() - start_time
|
|---|
| 33 | print("[%06d]"%count, time.strftime("[%H:%M:%S]", time.gmtime(elapsed_time)), "Normal return:")
|
|---|
| 34 | # if pout:
|
|---|
| 35 | # print('POUT:-------------------------')
|
|---|
| 36 | # print(pout)
|
|---|
| 37 | # if perr:
|
|---|
| 38 | # print('PERR:-------------------------')
|
|---|
| 39 | # print(perr)
|
|---|
| 40 | time.sleep(3.0) # time in seconds
|
|---|
| 41 | count += 1
|
|---|
| 42 |
|
|---|
| 43 |
|
|---|