-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathlibvirt_interface.py
87 lines (67 loc) · 2.3 KB
/
libvirt_interface.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
import libvirt
import time
from datetime import datetime
from constants import *
def getconn():
conn = libvirt.open(LIBVIRT_ADDR)
# Find the domain by name
domain = conn.lookupByName(LIBVIRT_VM_NAME)
return conn, domain
def send_string_to_vm(domain, text):
for char in text:
keycode_info = char_to_keycode.get(char)
print(f"sending keycodes {keycode_info}")
if keycode_info:
if isinstance(keycode_info, tuple):
# Press and hold the Shift key
domain.sendKey(0, 0, [keycode_info[0], keycode_info[1]], 2)
time.sleep(0.05)
# pass
else:
# Press the key
domain.sendKey(0, 0, [keycode_info], 1)
time.sleep(0.05)
def start_vm_if_not_running():
conn, domain = getconn()
if domain.info()[0] != libvirt.VIR_DOMAIN_RUNNING:
domain.create()
time.sleep(60) # hack
def type_text(cmd, delayTime=5):
# Open connection to libvirt
conn, domain = getconn()
print(f"typing {cmd}")
send_string_to_vm(domain, cmd)
time.sleep(delayTime)
conn.close()
# return grab_screenshot(conn, domain)
def key(keys):
conn, domain = getconn()
keys = keys.replace('+', ' ')
cmd_list = keys.strip().lower().split()
cmd_keycodes = []
for cmd in cmd_list:
if char_to_keycode.get(cmd):
cmd_keycodes.append(char_to_keycode.get(cmd))
elif special_keys_mapping.get(cmd):
cmd_keycodes.append(special_keys_mapping.get(cmd))
print(cmd_keycodes)
domain.sendKey(0, 0, cmd_keycodes, len(cmd_keycodes))
def grab_screenshot():
conn, domain = getconn()
stream = conn.newStream()
domain.screenshot(stream, 0)
current_datetime = datetime.now().strftime("%Y-%m-%d_%H-%M")
filename = f"screenshots/feditty-{current_datetime}.png"
fileHandler = open(filename, 'wb')
streamBytes = stream.recv(262120)
while streamBytes != b'':
fileHandler.write(streamBytes)
streamBytes = stream.recv(262120)
fileHandler.close()
stream.finish()
conn.close()
return filename
if __name__ == "__main__":
pass
# print(run_command_and_grab_screenshot("echo 'hello world'"))
# Close the connection