mirror of
https://github.com/cirosantilli/linux-kernel-module-cheat.git
synced 2026-01-26 11:41:35 +01:00
46 lines
1.3 KiB
Python
Executable File
46 lines
1.3 KiB
Python
Executable File
#!/usr/bin/env python3
|
|
|
|
import os
|
|
import sys
|
|
import telnetlib
|
|
|
|
import common
|
|
from shell_helpers import LF
|
|
|
|
prompt = b'\n(qemu) '
|
|
|
|
parser = self.get_argparse({
|
|
'description': '''\
|
|
Run a command on the QEMU monitor of a running QEMU instance
|
|
|
|
If the stdin is a terminal, open an interact shell. Otherwise,
|
|
run commands from stdin and quit.
|
|
'''
|
|
})
|
|
parser.add_argument(
|
|
'command',
|
|
help='If given, run this command and quit',
|
|
nargs='*',
|
|
)
|
|
args = self.setup(parser)
|
|
|
|
def write_and_read(tn, cmd, prompt):
|
|
tn.write(cmd.encode('utf-8'))
|
|
return '\n'.join(tn.read_until(prompt).decode('utf-8').splitlines()[1:])[:-len(prompt)]
|
|
|
|
with telnetlib.Telnet('localhost', kwargs['qemu_monitor_port']) as tn:
|
|
# Couldn't disable server echo, so just removing the write for now.
|
|
# https://stackoverflow.com/questions/12421799/how-to-disable-telnet-echo-in-python-telnetlib
|
|
# sock = tn.get_socket()
|
|
# sock.send(telnetlib.IAC + telnetlib.WILL + telnetlib.ECHO)
|
|
if os.isatty(sys.stdin.fileno()):
|
|
if kwargs['command'] == []:
|
|
print(tn.read_until(prompt).decode('utf-8'), end='')
|
|
tn.interact()
|
|
else:
|
|
tn.read_until(prompt)
|
|
print(write_and_read(tn, ' '.join(kwargs['command']) + '\n', prompt))
|
|
else:
|
|
tn.read_until(prompt)
|
|
print(write_and_read(tn, sys.stdin.read() + '\n', prompt))
|