Skip to content

Commit 04f401c

Browse files
author
TomasGl
authored
Support KDE Konsole in run_in_new_terminal function (Gallopsled#2023)
* Support KDE Konsole in run_in_new_terminal function * Rewrite '.stdout.read()' to '.communicate()[0]'
1 parent 84b51fc commit 04f401c

File tree

1 file changed

+27
-1
lines changed

1 file changed

+27
-1
lines changed

pwnlib/util/misc.py

+27-1
Original file line numberDiff line numberDiff line change
@@ -258,6 +258,8 @@ def run_in_new_terminal(command, terminal=None, args=None, kill_at_exit=True, pr
258258
- If ``$TERM_PROGRAM`` is set, that is used.
259259
- If X11 is detected (by the presence of the ``$DISPLAY`` environment
260260
variable), ``x-terminal-emulator`` is used.
261+
- If KDE Konsole is detected (by the presence of the ``$KONSOLE_VERSION``
262+
environment variable), a terminal will be split.
261263
- If WSL (Windows Subsystem for Linux) is detected (by the presence of
262264
a ``wsl.exe`` binary in the ``$PATH`` and ``/proc/sys/kernel/osrelease``
263265
containing ``Microsoft``), a new ``cmd.exe`` window will be opened.
@@ -297,6 +299,23 @@ def run_in_new_terminal(command, terminal=None, args=None, kill_at_exit=True, pr
297299
elif 'DISPLAY' in os.environ and which('x-terminal-emulator'):
298300
terminal = 'x-terminal-emulator'
299301
args = ['-e']
302+
elif 'KONSOLE_VERSION' in os.environ and which('qdbus'):
303+
konsole_window = os.environ['KONSOLE_DBUS_WINDOW'].split('/')[-1]
304+
konsole_dbus_service = os.environ['KONSOLE_DBUS_SERVICE']
305+
qdbus = which('qdbus')
306+
# SPLIT
307+
subprocess.run((qdbus, konsole_dbus_service, '/konsole/MainWindow_{}'.format(konsole_window),
308+
'org.kde.KMainWindow.activateAction', 'split-view-left-right'), stdout=subprocess.DEVNULL)
309+
310+
with subprocess.Popen((qdbus, konsole_dbus_service, os.environ['KONSOLE_DBUS_WINDOW'],
311+
'org.kde.konsole.Window.sessionList'), stdout=subprocess.PIPE) as proc:
312+
session_list = map(int, proc.communicate()[0].decode().split())
313+
last_konsole_session = max(session_list)
314+
315+
terminal = 'qdbus'
316+
args = [konsole_dbus_service, '/Sessions/{}'.format(last_konsole_session),
317+
'org.kde.konsole.Session.runCommand']
318+
300319
else:
301320
is_wsl = False
302321
if os.path.exists('/proc/sys/kernel/osrelease'):
@@ -359,13 +378,20 @@ def run_in_new_terminal(command, terminal=None, args=None, kill_at_exit=True, pr
359378
if terminal == 'tmux':
360379
out, _ = p.communicate()
361380
pid = int(out)
381+
elif terminal == 'qdbus':
382+
with subprocess.Popen((qdbus, konsole_dbus_service, '/Sessions/{}'.format(last_konsole_session),
383+
'org.kde.konsole.Session.processId'), stdout=subprocess.PIPE) as proc:
384+
pid = int(proc.communicate()[0].decode())
362385
else:
363386
pid = p.pid
364387

365388
if kill_at_exit:
366389
def kill():
367390
try:
368-
os.kill(pid, signal.SIGTERM)
391+
if terminal == 'qdbus':
392+
os.kill(pid, signal.SIGHUP)
393+
else:
394+
os.kill(pid, signal.SIGTERM)
369395
except OSError:
370396
pass
371397

0 commit comments

Comments
 (0)