2022-07-04 15:44:18 +02:00
|
|
|
from os import fdopen
|
|
|
|
from dbus import SystemBus, Array
|
|
|
|
|
|
|
|
|
2022-08-21 18:59:02 +02:00
|
|
|
def run(host, cmd, user=None):
|
2022-07-04 15:44:18 +02:00
|
|
|
bus = SystemBus()
|
|
|
|
remote_object = bus.get_object('org.freedesktop.machine1',
|
|
|
|
'/org/freedesktop/machine1',
|
|
|
|
False,
|
|
|
|
)
|
2022-08-21 18:59:02 +02:00
|
|
|
if user is not None:
|
|
|
|
cmd = ['/bin/su', '-', user, '-s', '/bin/bash', '-c', ' '.join(cmd)]
|
2022-07-04 15:44:18 +02:00
|
|
|
res = remote_object.OpenMachineShell(host,
|
|
|
|
'',
|
|
|
|
cmd[0],
|
|
|
|
Array(cmd, signature='s'),
|
|
|
|
Array(['TERM=dumb'], signature='s'),
|
|
|
|
dbus_interface='org.freedesktop.machine1.Manager',
|
|
|
|
)
|
|
|
|
fd = res[0].take()
|
|
|
|
fh = fdopen(fd)
|
|
|
|
while True:
|
|
|
|
try:
|
|
|
|
yield fh.readline().strip()
|
|
|
|
except OSError as err:
|
|
|
|
if err.errno != 5:
|
|
|
|
raise err from err
|
|
|
|
break
|