vm-keys.py (895B)
1 """Types into the test VM through QEMU's monitor, as a keyboard would: vm-keys.py 'Alice\tpw\n'. 2 3 Escapes: \t Tab, \n Enter, \b Backspace. Capitals use Shift; a space, - _ and . are handled; other 4 symbols are not. 5 """ 6 import os 7 import socket, sys, time 8 MON = os.path.join(os.path.dirname(os.path.abspath(__file__)), "..", "linux", "out", "monitor.sock") 9 s = socket.socket(socket.AF_UNIX); s.connect(MON); s.settimeout(1) 10 try: s.recv(4096) 11 except Exception: pass 12 def cmd(c): 13 s.sendall((c + "\n").encode()); time.sleep(0.07) 14 try: s.recv(4096) 15 except Exception: pass 16 SPECIAL = {" ": "spc", "-": "minus", "_": "shift-minus", ".": "dot", "\t": "tab", "\n": "ret", "\b": "backspace"} 17 def key(ch): 18 if ch in SPECIAL: return SPECIAL[ch] 19 if ch.isupper(): return "shift-" + ch.lower() 20 return ch 21 for ch in sys.argv[1].encode().decode("unicode_escape"): 22 cmd("sendkey " + key(ch))