-
Notifications
You must be signed in to change notification settings - Fork 0
/
sshgo.py
executable file
·623 lines (535 loc) · 18.9 KB
/
sshgo.py
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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import os, sys
import curses
import locale
import math
import re
import linecache
from optparse import OptionParser
locale.setlocale(locale.LC_ALL, "")
script = sys.path[0] + "/login.sh"
script_nest = sys.path[0] + "/login_nest.sh"
sshHosts = sys.path[0] + "/hosts"
def _assert(exp, err):
if not exp:
print(err, file=sys.stderr)
sys.exit(1)
def _dedup(ls):
table = {}
for item in ls:
table[item] = None
ls_dedup = list(table.keys())
ls_dedup.sort()
return ls_dedup
def _get_known_hosts():
fn = os.path.expanduser(sshHosts)
hosts = []
if not os.path.exists(fn):
return hosts
fp = open(fn, "r")
try:
lines = fp.readlines()
for line in lines:
host = line.split(" ")[0]
if host is None or len(host) == 0:
continue
if len(host.split(",")) == 2:
# hostname,ip
host = host[0]
continue
hosts.append(host)
finally:
fp.close()
return _dedup(hosts)
def _get_host_name(line_con, node_host):
node_infos = line_con.split("# ")
node_name = node_infos[1] if len(node_infos) >= 2 else node_host
# 如果host文件中包含注释的内容,则将注释的内容拼到选项中
if node_host != node_name:
node_name = node_host + " (" + node_name.strip() + ")"
return node_name
class SSHGO:
UP = -1
DOWN = 1
KEY_O = 79
KEY_R = 82
KEY_G = 71
KEY_o = 111
KEY_r = 114
KEY_g = 103
KEY_c = 99
KEY_C = 67
KEY_m = 109
KEY_M = 77
KEY_d = 0x64
KEY_u = 0x75
KEY_SPACE = 32
KEY_ENTER = 10
KEY_q = 113
KEY_ESC = 27
KEY_j = 106
KEY_k = 107
KEY_SPLASH = 47
screen = None
def _parse_tree_from_config_file(self, config_file):
tree = {"line_number": None, "expanded": True, "line": None, "sub_lines": []}
def find_parent_line(new_node):
line_number = new_node["line_number"]
level = new_node["level"]
if level == 0:
return tree
stack = tree["sub_lines"] + []
parent = None
while len(stack):
node = stack.pop()
if node["line_number"] < line_number and node["level"] == level - 1:
if parent is None:
parent = node
elif node["line_number"] > parent["line_number"]:
parent = node
if len(node["sub_lines"]) and node["level"] < level:
stack = stack + node["sub_lines"]
continue
return parent
tree_level = None
nodes_pool = []
line_number = 0
for line in open(config_file, "r"):
line_number += 1
line_con = line.strip()
if line_con == "" or line_con[0] == "#":
continue
expand = True
if line_con[0] == "-":
line_con = line_con[1:]
expand = False
indent = re.findall(r"^[\t ]*(?=[^\t ])", line)[0]
line_level = indent.count(" ") + indent.count("\t")
if tree_level == None:
_assert(line_level == 0, "invalid indent,line:" + str(line_number))
else:
_assert(
line_level <= tree_level or line_level == tree_level + 1,
"invalid indent,line:" + str(line_number),
)
tree_level = line_level
node_infos = re.split(r"\s+", line_con.strip())
node_host = node_infos[0] if len(node_infos) >= 2 else line_con
node_user = node_infos[1] if len(node_infos) >= 2 else ""
node_pass = node_infos[2] if len(node_infos) >= 3 else ""
node_id_file = node_infos[3] if len(node_infos) == 4 else ""
node_name = _get_host_name(line_con, node_host)
# line 对象用于显示名称
new_node = {
"nest_parent": None,
"level": tree_level,
"expanded": expand,
"line_number": line_number,
"line": node_name,
"user": node_user,
"password": node_pass,
"id_file": node_id_file,
"host": node_host,
"sub_lines": [],
}
nodes_pool.append(new_node)
parent = find_parent_line(new_node)
# add the nest_parent to node
if parent.get("level"):
nest_parent_node = find_parent_line(parent)
if nest_parent_node and len(nest_parent_node["sub_lines"]) > 0:
new_node["nest_parent"] = nest_parent_node["sub_lines"][0]
parent["sub_lines"].append(new_node)
return tree, nodes_pool
def __init__(self, config_file):
self.hosts_tree, self.hosts_pool = self._parse_tree_from_config_file(
config_file
)
known_host_list = _get_known_hosts()
MAGIC_LINE_NUMBER = 666
known_hosts = {
"sub_lines": [],
"line_number": MAGIC_LINE_NUMBER,
"line": "known hosts",
"expanded": False,
"level": 0,
}
for host in known_host_list:
known_hosts["sub_lines"].append(
{
"sub_lines": [],
"line_number": MAGIC_LINE_NUMBER,
"line": host,
"expanded": True,
"level": 1,
}
)
self.hosts_tree["sub_lines"].append(known_hosts)
self.hosts_pool.append(known_hosts)
self.screen = curses.initscr()
curses.noecho()
curses.cbreak()
curses.curs_set(0)
self.screen.keypad(1)
self.screen.border(0)
self.top_line_number = 0
self.highlight_line_number = 0
self.search_keyword = None
curses.start_color()
curses.use_default_colors()
# highlight
curses.init_pair(2, curses.COLOR_WHITE, curses.COLOR_BLUE)
self.COLOR_HIGHLIGHT = 2
# red
curses.init_pair(3, curses.COLOR_RED, -1)
self.COLOR_RED = 3
# red highlight
curses.init_pair(4, curses.COLOR_RED, curses.COLOR_BLUE)
self.COLOR_RED_HIGH = 4
# white bg
curses.init_pair(5, curses.COLOR_BLACK, curses.COLOR_WHITE)
self.COLOR_WBG = 5
# black bg
curses.init_pair(6, curses.COLOR_BLACK, curses.COLOR_BLACK)
self.COLOR_BBG = 6
self.run()
def run(self):
while True:
self.render_screen()
c = self.screen.getch()
if c == curses.KEY_UP or c == self.KEY_k:
self.updown(-1)
elif c == curses.KEY_DOWN or c == self.KEY_j:
self.updown(1)
elif c == self.KEY_u:
for i in range(0, curses.tigetnum("lines")):
self.updown(-1)
elif c == self.KEY_d:
for i in range(0, curses.tigetnum("lines")):
self.updown(1)
elif c == self.KEY_ENTER or c == self.KEY_SPACE:
self.toggle_node()
elif c == self.KEY_ESC or c == self.KEY_q:
self.exit()
elif c == self.KEY_O or c == self.KEY_M:
self.open_all()
elif c == self.KEY_o or c == self.KEY_m:
self.open_node()
elif c == self.KEY_C or c == self.KEY_R:
self.close_all()
elif c == self.KEY_c or c == self.KEY_r:
self.close_node()
elif c == self.KEY_g:
self.page_top()
elif c == self.KEY_G:
self.page_bottom()
elif c == self.KEY_SPLASH:
self.enter_search_mode()
def exit(self):
if self.search_keyword is not None:
self.search_keyword = None
else:
sys.exit(0)
def enter_search_mode(self):
screen_cols = curses.tigetnum("cols")
self.screen.addstr(0, 0, "/" + " " * screen_cols)
curses.echo()
curses.curs_set(1)
self.search_keyword = self.screen.getstr(0, 1)
curses.noecho()
curses.curs_set(0)
def _get_visible_lines_for_render(self):
lines = []
stack = self.hosts_tree["sub_lines"] + []
while len(stack):
node = stack.pop()
lines.append(node)
if node["expanded"] and len(node["sub_lines"]):
stack = stack + node["sub_lines"]
lines.sort(key=lambda n: n["line_number"], reverse=False)
return lines
def _search_node(self):
rt = []
try:
kre = re.compile(self.search_keyword, re.I)
except:
return rt
for node in self.hosts_pool:
if len(node["sub_lines"]) == 0 and kre.search(node["line"]) is not None:
rt.append(node)
return rt
def get_lines(self):
if self.search_keyword is not None:
return self._search_node()
else:
return self._get_visible_lines_for_render()
def page_top(self):
self.top_line_number = 0
self.highlight_line_number = 0
def page_bottom(self):
screen_lines = curses.tigetnum("lines")
visible_hosts = self.get_lines()
self.top_line_number = max(len(visible_hosts) - screen_lines, 0)
self.highlight_line_number = min(screen_lines, len(visible_hosts)) - 1
def open_node(self):
visible_hosts = self.get_lines()
linenum = self.top_line_number + self.highlight_line_number
node = visible_hosts[linenum]
if not len(node["sub_lines"]):
return
stack = [node]
while len(stack):
node = stack.pop()
node["expanded"] = True
if len(node["sub_lines"]):
stack = stack + node["sub_lines"]
def close_node(self):
visible_hosts = self.get_lines()
linenum = self.top_line_number + self.highlight_line_number
node = visible_hosts[linenum]
if not len(node["sub_lines"]):
return
stack = [node]
while len(stack):
node = stack.pop()
node["expanded"] = False
if len(node["sub_lines"]):
stack = stack + node["sub_lines"]
def open_all(self):
for node in self.hosts_pool:
if len(node["sub_lines"]):
node["expanded"] = True
def close_all(self):
for node in self.hosts_pool:
if len(node["sub_lines"]):
node["expanded"] = False
def toggle_node(self):
visible_hosts = self.get_lines()
linenum = self.top_line_number + self.highlight_line_number
node = visible_hosts[linenum]
if len(node["sub_lines"]):
node["expanded"] = not node["expanded"]
else:
self.restore_screen()
# ssh = 'ssh'
# if os.popen('which zssh 2> /dev/null').read().strip() != '':
# ssh = 'zssh'
# run script instead of 'ssh'
nest_parent = node["nest_parent"]
ssh = script
if nest_parent:
ssh = script_nest
host_info = re.split(":", node["host"].strip())
port = "22"
host = ""
if len(host_info) == 2:
host = host_info[0]
port = host_info[1]
else:
host = host_info[0]
if nest_parent:
nest_host_info = re.split(":", nest_parent["host"].strip())
nest_port = "22"
nest_host = ""
if len(nest_host_info) == 2:
nest_host = nest_host_info[0]
nest_port = nest_host_info[1]
else:
nest_host = nest_host_info[0]
exe_args = [
ssh,
nest_host,
nest_port,
nest_parent["user"],
nest_parent["password"],
nest_parent["id_file"],
host,
port,
node["user"],
node["password"],
]
else:
exe_args = [
ssh,
host,
port,
node["user"],
node["password"],
node["id_file"],
]
os.execvp(ssh, exe_args)
def render_screen(self):
# clear screen
self.screen.clear()
# now paint the rows
screen_lines = curses.tigetnum("lines")
screen_cols = curses.tigetnum("cols")
if self.highlight_line_number >= screen_lines:
self.highlight_line_number = screen_lines - 1
all_nodes = self.get_lines()
if self.top_line_number >= len(all_nodes):
self.top_line_number = 0
top = self.top_line_number
bottom = self.top_line_number + screen_lines
nodes = all_nodes[top:bottom]
if not len(nodes):
self.screen.refresh()
return
if self.highlight_line_number >= len(nodes):
self.highlight_line_number = len(nodes) - 1
if self.top_line_number >= len(all_nodes):
self.top_line_number = 0
for (
index,
node,
) in enumerate(nodes):
# linenum = self.top_line_number + index
line = node["line"]
if len(node["sub_lines"]):
line += "(%d)" % len(node["sub_lines"])
prefix = ""
if self.search_keyword is None:
prefix += " " * node["level"]
if len(node["sub_lines"]):
if node["expanded"]:
prefix += "-"
else:
prefix += "+"
else:
prefix += "o"
prefix += " "
# highlight current line
if index != self.highlight_line_number:
self.screen.addstr(index, 0, prefix, curses.color_pair(self.COLOR_RED))
self.screen.addstr(index, len(prefix), line)
else:
self.screen.addstr(
index, 0, prefix, curses.color_pair(self.COLOR_RED_HIGH)
)
self.screen.addstr(
index, len(prefix), line, curses.color_pair(self.COLOR_HIGHLIGHT)
)
# render scroll bar
for i in range(screen_lines):
self.screen.addstr(
i, screen_cols - 2, "|", curses.color_pair(self.COLOR_WBG)
)
scroll_top = int(
math.ceil(
(self.top_line_number + 1.0)
/ max(len(all_nodes), screen_lines)
* screen_lines
- 1
)
)
scroll_height = int(
math.ceil((len(nodes) + 0.0) / len(all_nodes) * screen_lines)
)
highlight_pos = int(
math.ceil(
scroll_height
* ((self.highlight_line_number + 1.0) / min(screen_lines, len(nodes)))
)
)
self.screen.addstr(
scroll_top, screen_cols - 2, "^", curses.color_pair(self.COLOR_WBG)
)
self.screen.addstr(
min(screen_lines, scroll_top + scroll_height) - 1,
screen_cols - 2,
"v",
curses.color_pair(self.COLOR_WBG),
)
self.screen.addstr(
min(screen_lines, scroll_top + highlight_pos) - 1,
screen_cols - 2,
"+",
curses.color_pair(self.COLOR_WBG),
)
self.screen.refresh()
# move highlight up/down one line
def updown(self, increment):
visible_hosts = self.get_lines()
visible_lines_count = len(visible_hosts)
next_line_number = self.highlight_line_number + increment
# paging
if (
increment < 0
and self.highlight_line_number == 0
and self.top_line_number != 0
):
self.top_line_number += self.UP
return
elif (
increment > 0
and next_line_number == curses.tigetnum("lines")
and (self.top_line_number + curses.tigetnum("lines")) != visible_lines_count
):
self.top_line_number += self.DOWN
return
# scroll highlight line
if increment < 0 and (
self.top_line_number != 0 or self.highlight_line_number != 0
):
self.highlight_line_number = next_line_number
elif (
increment > 0
and (self.top_line_number + self.highlight_line_number + 1)
!= visible_lines_count
and self.highlight_line_number != curses.tigetnum("lines")
):
self.highlight_line_number = next_line_number
def restore_screen(self):
curses.initscr()
curses.nocbreak()
curses.echo()
curses.endwin()
# catch any weird termination situations
def __del__(self):
self.restore_screen()
def login_main_host(host_file, sarg):
# 解析文件,获取第二行相关配置, 默认第二行为默认配置
# TODO 优化node info获取节点的名称,目前含义有些模糊
default_conf_line = linecache.getline(host_file, 2)
default_node_infos = re.split(r"\s+", default_conf_line.strip())
default_node_host = (
default_node_infos[0] if len(default_node_infos) >= 2 else default_conf_line
)
default_node_user = default_node_infos[1] if len(default_node_infos) >= 2 else ""
default_node_pass = default_node_infos[2] if len(default_node_infos) >= 3 else ""
default_node_id_file = default_node_infos[3] if len(default_node_infos) == 4 else ""
default_host_info = re.split(":", default_node_host.strip())
ssh = script
exe_args = [
ssh,
default_host_info[0],
default_host_info[1],
default_node_user,
default_node_pass,
default_node_id_file,
sarg,
]
os.execvp(ssh, exe_args)
if __name__ == "__main__":
parser = OptionParser()
parser.add_option(
"-c", "--config", help="use specified config file instead of hosts"
)
options, args = parser.parse_args(sys.argv)
host_file = os.path.expanduser(sshHosts)
if options.config is not None:
host_file = options.config
if not os.path.exists(host_file):
print("hosts is not found, create it", file=sys.stderr)
fp = open(host_file, "w")
fp.close()
if len(args) > 1:
sarg = args[1]
if re.match("\d+\.\d+", str(sarg), flags=0) is not None:
# 如果匹配到输入的是ip格式(包含完整或者一部分的ipv4, 即 xxx.xxx)
login_main_host(host_file, sarg)
else:
sshgo = SSHGO(host_file)
else:
sshgo = SSHGO(host_file)