-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathssh_helper.py
53 lines (46 loc) · 1.38 KB
/
ssh_helper.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
#!/usr/bin/python3 -tt
import json
import os
import curses
import subprocess
__author__ = "Logan Schmidt (d4rkd0s)"
__copyright__ = "Copyright 2018 d4rkd0s"
__credits__ = ["Logan Schmidt"]
__license__ = "MIT"
__maintainer__ = "Logan Schmidt"
__email__ = "d4rkd0s@gmail.com"
__status__ = "Production"
# Loads the database from connections.json file in ~/.ssh/
def loadDB():
database_file = open(os.environ['HOME'] + "/.ssh/connections.json", "r")
return json.loads(database_file.read())
def createNewDB():
database_file = open(os.environ['HOME'] + "/.ssh/connections.json", "w")
data = {}
config = open(os.environ['HOME'] + "/.ssh/config", "r")
config_lines = config.readlines()
num_of_lines = 0
for line in config_lines:
if line.find("Host ") != -1:
text = line.rstrip('\n')
num_of_lines += 1
data[num_of_lines] = text[5:]
database_file.write(json.dumps(data))
return loadDB()
def prepareDB():
exists = os.path.isfile(os.environ['HOME'] + "/.ssh/connections.json")
if exists:
return loadDB()
else:
createNewDB()
def printDB(database):
for key, value in database.items():
print(key + " - " + value)
def main():
database = prepareDB()
printDB(database)
choice = input("Enter server #: ")
cmd = ['ssh', database[choice]]
subprocess.call(cmd)
exit()
main()