-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathssh-group.py
More file actions
182 lines (149 loc) · 5.8 KB
/
ssh-group.py
File metadata and controls
182 lines (149 loc) · 5.8 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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
#!/usr/bin/env python3
# Terminator plugin by donfuz14
# GPL v2 only
from gi.repository import Gtk, Vte
import random
import terminatorlib.plugin as plugin
from terminatorlib.config import Config
import getpass
import json
import os
import re
import itertools
import gi
gi.require_version('Gtk', '3.0')
pluginpath = os.path.dirname(os.path.realpath(__file__))
configpath = (pluginpath + "/ssh-group-config")
# get information from config.json
for root, dirs, files in os.walk(configpath):
for file in files:
if file.endswith(".json"):
filename = (os.path.join(root, file))
try:
with open(filename) as data_file:
try:
SSHGROUP
except NameError:
SSHGROUP = json.load(data_file)
else:
SSHGROUP.update(json.load(data_file))
except Exception as e:
print("Error loading "+filename+": "+str(e))
AVAILABLE = ['SSHGroup']
current_user = getpass.getuser()
groupdict = {}
class SSHGroup(plugin.Plugin):
capabilities = ['terminal_menu']
config = None
def __init__(self):
self.config = Config()
for server, option in SSHGROUP.items():
group = self.get_property(server, 'group')
if group not in groupdict:
groupdict[group] = [server]
else:
groupdict[group].append(str.join("", server))
def callback(self, menuitems, menu, terminal):
submenu = {}
item = Gtk.MenuItem('SSH Group')
menuitems.append(item)
submenu = Gtk.Menu()
item.set_submenu(submenu)
# create groupdict for submenu
for group, servers in sorted(groupdict.items()):
sub_groups = self.add_submenu(submenu, group)
servers.sort()
for server in servers:
self.add_split_submenu(terminal, sub_groups, server)
menuitem = Gtk.SeparatorMenuItem()
submenu.append(menuitem)
def add_submenu(self, submenu, name):
menu = Gtk.MenuItem(name)
submenu.append(menu)
menu_sub = Gtk.Menu()
menu.set_submenu(menu_sub)
return menu_sub
def add_split_submenu(self, terminal, sub_groups, server):
# get username for ssh session
if self.get_property(server, 'user'):
user = self.get_property(server, 'user')
else:
user = current_user
sub_split = self.add_submenu(sub_groups, server)
menuitem = Gtk.MenuItem('Horizontal Split')
menuitem.connect('activate', self.connect_server, terminal, server,
user, 'H')
sub_split.append(menuitem)
menuitem = Gtk.MenuItem('Vertical Split')
menuitem.connect('activate', self.connect_server, terminal, server,
user, 'V')
sub_split.append(menuitem)
menuitem = Gtk.MenuItem('New Tab')
menuitem.connect('activate', self.connect_server, terminal, server,
user, 'T')
sub_split.append(menuitem)
def connect_server(self, widget, terminal, server, user, option):
focussed_terminal = None
term_windows = terminal.terminator.get_windows()
if option == 'H':
terminal.key_split_horiz()
elif option == 'V':
terminal.key_split_vert()
elif option == 'T':
for term_window in term_windows:
term_window.tab_new(term_window.get_focussed_terminal())
for term_window in term_windows:
focussed_terminal = term_window.get_focussed_terminal()
self.start_ssh(focused_terminal, user, server)
def start_ssh(self, terminal, user, server):
# Function to generate the ssh command, with specified options
if server:
command = "ssh"
if user != current_user:
command = command + " -l " + user
# check if ssh agent should be used, if not disable it
if self.get_property(server, 'agent'):
command += " -A"
else:
command += " -a"
# If port is configured, get that port
port = self.get_property(server, 'port')
if port:
command = command + " -p " + port
# If ssh-key is specified, use that key
key = self.get_property(server, 'identity')
if key:
command = command + " -i " + key
# get verbosity level
verbose = self.get_property(server, 'verbose')
if verbose:
count = 0
command = command + " -"
while count < int(verbose) < 4:
command += "v"
count += 1
command = command + " " + server
# Check if a command was generated an pass it to the terminal
if command[len(command) - 1] != '\n':
command += '\n'
self.feed_child(terminal, command)
def feed_child(self, terminal, command):
# get post command
cmd = self.get_property(server, 'cmd')
try:
terminal.vte.feed_child(str(command))
except TypeError:
terminal.vte.feed_child(command, len(command))
# send post command to terminal
if type(cmd) != bool and cmd[len(cmd) - 1] != '\n':
cmd += '\n'
try:
terminal.vte.feed_child(cmd)
except TypeError:
terminal.vte.feed_child(cmd, len(cmd))
def get_property(self, server, prop, default=False):
# Check if property and server exsist return true else return false
if SSHGROUP.has_key(server) and SSHGROUP[server].has_key(prop):
return str.join("", SSHGROUP[server][prop])
else:
return default