forked from appformation/easy-connect
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconnect.py
executable file
·145 lines (103 loc) · 3.68 KB
/
connect.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
#!/usr/bin/python
# Copyright (C) 2016 Appformation sp. z o.o.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
from colorclass import Color
import os
import sys
import subprocess
import time
# Contains all ssh entries
ssh_entries = []
# Config file
config_file_path = '%s/.ssh/config' % os.path.expanduser('~')
# Load and parse ssh config file and fill out ssh_entries
def load_and_parse_ssh_config():
with open(config_file_path) as f:
content = f.readlines()
last_entry = None
current_letter = 49
for entry in content:
if entry.startswith('Host '):
if last_entry is not None:
if "host" not in last_entry:
last_entry['host'] = last_entry['name']
ssh_entries.append(last_entry)
current_letter += 1
last_entry = None
if last_entry is None:
last_entry = {}
if current_letter == 58:
current_letter = 97
last_entry['key'] = current_letter
last_entry['name'] = entry[5:].strip()
elif entry.strip().startswith("HostName "):
last_entry['host'] = entry.strip()[9:]
elif entry.strip().startswith("User "):
last_entry['user'] = entry.strip()[5:]
if last_entry is not None:
if "host" in last_entry:
ssh_entries.append(last_entry)
# Print header
def print_header():
print ''
print Color(' {autocyan}EasyConnect{/autocyan}')
print Color(' {autocyan}-----------{/autocyan}')
print Color(' {autoyellow}Select ssh configuration to launch{/autoyellow}')
print ''
# Print list of all entries
def print_list_of_entries():
for entry in ssh_entries:
print Color(' {autocyan}' + chr(entry['key']) + '){/autocyan} {autowhite}' + entry['name'] + '{/autowhite}')
print ""
# Wait for a key press on the console and return it
# Found on http://stackoverflow.com/a/34956791/2024830
# Credits goes to Gringo Suave
def wait_key():
result = None
print Color(' {autowhite}>{/autowhite}'),
if os.name == 'nt':
import msvcrt
result = msvcrt.getch()
else:
import termios
fd = sys.stdin.fileno()
old_term = termios.tcgetattr(fd)
new_attr = termios.tcgetattr(fd)
new_attr[3] = new_attr[3] & ~termios.ICANON & ~termios.ECHO
termios.tcsetattr(fd, termios.TCSANOW, new_attr)
try:
result = sys.stdin.read(1)
except IOError:
pass
finally:
termios.tcsetattr(fd, termios.TCSAFLUSH, old_term)
print Color('{autoyellow}' + result + '{/autoyellow}\n')
return result
# Launch
if __name__ == "__main__":
print_header()
if not os.path.isfile(config_file_path):
print Color(' {autored}~/.ssh/config file is missing{/autored}\n')
quit()
load_and_parse_ssh_config()
print_list_of_entries()
try:
key = wait_key()
for entry in ssh_entries:
if entry['key'] == ord(key.lower()):
time.sleep(1)
subprocess.call('clear')
subprocess.call('ssh %s' % entry['name'], shell=True)
except:
pass