This repository has been archived by the owner on Jul 1, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
/
sshconnect.py
executable file
·142 lines (117 loc) · 3.27 KB
/
sshconnect.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
#!/usr/bin/env python
import sys, getopt, json
from subprocess import call
from os.path import expanduser
##############
# SSHCONNECT #
##############
APPNAME = "sshconnect.py"
VERSION = 0.3
# Path to configuration file
HOME = expanduser("~")
CONFIG_FILE = HOME + "/.sshconnect"
# Util Variables
enableX11 = False
verboseMode = False
##################################################
# Do not change anything below here
##################################################
def main(argv):
preSelectedCon = -1;
try:
opts, args = getopt.getopt(argv, "XhvVc:", ["help","version","connection=","X11"])
except getopt.GetoptError:
print 'Wrong parameter(s): ' + ' '.join(argv)
help()
sys.exit(2)
for opt, arg in opts:
if opt in ("-h", "--help"):
help()
sys.exit(0)
elif opt in ("-V", "--version"):
print APPNAME, VERSION
sys.exit(0)
elif opt in ("-c", "--connection"):
preSelectedCon = int(arg)
elif opt in ("-X"):
setX11(True)
elif opt in ("-v", "--verbose"):
setVerboseMode(True)
config = openConfig()
if config is not None:
con = preSelectedCon
if con < 1:
con = chooseConnection(config)
if con is not None:
connectTo(con, config['connections'])
def setX11(value):
global enableX11
enableX11 = value
def setVerboseMode(value):
global verboseMode
verboseMode = value
def help():
print "Usage:", APPNAME, "[-c connection no]"
print " -h This message"
print " -c, --connection Preselect a connection by its number"
print " -X, Enable X11 Forwarding"
print " -v, --verbose Print the command "
print " -V, --version Print the version number"
def openConfig():
try:
config = json.loads(open(CONFIG_FILE).read())
return config
except IOError:
print "Config file: \"" + CONFIG_FILE + "\" not found"
return None
def chooseConnection(config):
while True:
numConnections = printMenu(config['connections'])
try:
print "Choose connection:",
conn = int(raw_input()) # Read connection number
if conn > numConnections or conn < 1:
print "Invalid connection number. Please try again!"
else:
return conn
except ValueError:
print "Only digits are allowed! Please try again!"
except KeyboardInterrupt:
print ""
break
return None
def printMenu(connections):
num = 0
print "Available connections:"
for conn in connections:
num += 1
print " " + str(num) + "\t" + conn['name']
return num
def connectTo(conNumber, connections):
num = 0
for conn in connections: # Find the selected connection
num += 1
if num == conNumber:
try:
args = []
# Connect with X11 forwarding
if enableX11 == True or 'enableX11' in conn:
args.append("-X");
# Connect with ssh key
if 'key' in conn:
args.append("-i" + conn['key']);
# Connect to specific port
if len(conn['port']) > 1:
args.append("-p" + conn['port']);
# Builds the SSH Command
command = "ssh " + ' '.join(args) + ' ' + conn['user']+"@"+conn['host']
# Print the command
if verboseMode == True:
print command
call(command, shell=True)
#check_output(["ssh", ','.join(args), conn['connection']])
#call(command)
except KeyboardInterrupt:
print "Connection aborted by user"
if __name__ == "__main__":
sys.exit(main(sys.argv[1:]))