-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathmain.py
More file actions
executable file
·73 lines (50 loc) · 1.94 KB
/
Copy pathmain.py
File metadata and controls
executable file
·73 lines (50 loc) · 1.94 KB
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
#! /usr/bin/env python3
import os
import subprocess
import sys
from gi.repository import GLib
import dbus.service
from dbus.mainloop.glib import DBusGMainLoop
DBusGMainLoop(set_as_default=True)
objpath = "/ssh"
iface = "org.kde.krunner1"
class Runner(dbus.service.Object):
def __init__(self, terminal_command):
self.terminal_command = terminal_command
dbus.service.Object.__init__(self, dbus.service.BusName("com.selfcoders.ssh-runner", dbus.SessionBus()), objpath)
@dbus.service.method(iface, out_signature="a(sss)")
def Actions(self, msg):
return []
@dbus.service.method(iface, in_signature="s", out_signature="a(sssida{sv})")
def Match(self, query):
query = query.split(" ")
if len(query) > 1 and query[0] == "ssh":
query = query[1:]
query = " ".join(query)
known_hosts_file = os.path.join(os.path.expanduser("~"), ".ssh", "known_hosts")
if not os.path.isfile(known_hosts_file):
return []
results = []
with open(known_hosts_file, "r") as file:
for line in file:
hostname = line.split(" ")[0].split(",")[0]
if not hostname.startswith(query):
continue
# actionId, actionName, iconName, Type, relevance (0-1), properties
results.append((hostname, hostname, "terminal", 100, 0, {}))
return results
@dbus.service.method(iface, in_signature="ss")
def Run(self, matchId, actionId):
command = []
for argument in self.terminal_command:
if "{}" in argument:
argument = argument.format(matchId)
command.append(argument)
subprocess.call(command)
if __name__ == "__main__":
terminal_command = ["konsole", "-e", "ssh {}"]
if len(sys.argv) > 1:
terminal_command = sys.argv[1:]
runner = Runner(terminal_command)
loop = GLib.MainLoop()
loop.run()