-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcvldemo.py
290 lines (249 loc) · 11.4 KB
/
cvldemo.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
# CVL Administrator - A GUI for launching/managing NeCTAR instances.
# Copyright (C) 2012 James Wettenhall, Monash University
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
#
# Enquires: [email protected] or [email protected]
import sys
import os
import traceback
import argparse
import ssh # Pure Python-based ssh module, based on Paramiko, published on PyPi
import subprocess
import time
global localPortNumber
localPortNumber = "5901"
global cipher
global ciphers
if sys.platform.startswith("win"):
cipher = "arcfour"
ciphers = ["3des-cbc", "blowfish-cbc", "arcfour"]
else:
cipher = "arcfour128"
ciphers = ["3des-cbc", "blowfish-cbc", "arcfour128"]
version = "0.0.1"
cvldemo_ip_address = "115.146.93.198"
ssh_private_key_filename = "<ssh_private_key_filename>"
username = "<username>"
parser = argparse.ArgumentParser(description="Create a user account on the CVL Demo virtual machine.")
parser.add_argument("-i", "--identity", action="store", default="<ssh_private_key_filename>",help="SSH private key file")
parser.add_argument("-u", "--username", action="store", default="<username>", help="Username")
parser.add_argument("-c", "--cipher", action="store", default=cipher, help="Cipher for SSH tunnel (man ssh_config).")
options = parser.parse_args()
ssh_private_key_filename = options.identity
username = options.username
cipher = options.cipher
if ssh_private_key_filename == "<ssh_private_key_filename>":
parser.print_help()
sys.exit(1)
if username == "<username>":
parser.print_help()
sys.exit(1)
print 'SSH private key file :', ssh_private_key_filename
print 'username :', username
try:
import getpass
password = getpass.getpass()
privateKeyFile = os.path.expanduser(ssh_private_key_filename)
privateKey = ssh.RSAKey.from_private_key_file(privateKeyFile)
sshClient = ssh.SSHClient()
sshClient.set_missing_host_key_policy(ssh.AutoAddPolicy())
sshClient.connect(cvldemo_ip_address, username="root", pkey=privateKey)
stdin,stdout,stderr = sshClient.exec_command("useradd " + username)
stderrRead = stderr.read()
userAlreadyExists = False
if len(stderrRead) > 0:
if "already exists" in stderrRead:
userAlreadyExists = True
# We will ignore this exception.
else:
raise Exception(stderrRead)
stdin,stdout,stderr = sshClient.exec_command("passwd " + username)
stdin.write(password + "\n")
stdin.flush()
stdin.write(password + "\n")
stdin.flush()
stderrRead = stderr.read()
if len(stderrRead) > 0:
if "BAD PASSWORD" in stderrRead:
raise Exception(stderrRead)
elif not "Retype new password" in stderrRead:
raise Exception(stderrRead)
stdin,stdout,stderr = sshClient.exec_command("su " + username + " -c \"vncpasswd\"")
stdin.write(password + "\n")
stdin.flush()
stdin.write(password + "\n")
stdin.flush()
if len(stderrRead) > 0:
if "BAD PASSWORD" in stderrRead:
raise Exception(stderrRead)
elif not "Retype new password" in stderrRead:
raise Exception(stderrRead)
stdin,stdout,stderr = sshClient.exec_command("su " + username + " -c \"vncserver\"")
stderrRead = stderr.read()
stderrLines = stderrRead.split("\n")
stderrLinesSplit = stderrLines[1].split(":")
displayNumber = stderrLinesSplit[2]
print "Display number = " + displayNumber
sshClient.close()
print "Generating SSH key-pair for tunnel...\n"
sshClient = ssh.SSHClient()
sshClient.set_missing_host_key_policy(ssh.AutoAddPolicy())
sshClient.connect(cvldemo_ip_address, username=username, password=password)
stdin,stdout,stderr = sshClient.exec_command("/bin/rm -f ~/CVLdemoKeyPair*")
if len(stderr.read()) > 0:
print stderr.read()
stdin,stdout,stderr = sshClient.exec_command("/usr/bin/ssh-keygen -C \"CVL Demo\" -N \"\" -t rsa -f ~/CVLdemoKeyPair")
if len(stderr.read()) > 0:
print stderr.read()
stdin,stdout,stderr = sshClient.exec_command("/bin/mkdir ~/.ssh")
if len(stderr.read()) > 0:
print stderr.read()
stdin,stdout,stderr = sshClient.exec_command("chmod 700 ~/.ssh")
if len(stderr.read()) > 0:
print stderr.read()
stdin,stdout,stderr = sshClient.exec_command("/bin/touch ~/.ssh/authorized_keys")
if len(stderr.read()) > 0:
print stderr.read()
stdin,stdout,stderr = sshClient.exec_command("chmod 600 ~/.ssh/authorized_keys")
if len(stderr.read()) > 0:
print stderr.read()
stdin,stdout,stderr = sshClient.exec_command("/bin/sed -i -e \"/CVL Demo/d\" ~/.ssh/authorized_keys")
if len(stderr.read()) > 0:
print stderr.read()
stdin,stdout,stderr = sshClient.exec_command("/bin/cat CVLdemoKeyPair.pub >> ~/.ssh/authorized_keys")
if len(stderr.read()) > 0:
print stderr.read()
stdin,stdout,stderr = sshClient.exec_command("/bin/rm -f ~/CVLdemoKeyPair.pub")
if len(stderr.read()) > 0:
print stderr.read()
stdin,stdout,stderr = sshClient.exec_command("/bin/cat CVLdemoKeyPair")
if len(stderr.read()) > 0:
print stderr.read()
privateKeyString = stdout.read()
stdin,stdout,stderr = sshClient.exec_command("/bin/rm -f ~/CVLdemoKeyPair")
if len(stderr.read()) > 0:
print stderr.read()
import tempfile
privateKeyFile = tempfile.NamedTemporaryFile(mode='w+t')
privateKeyFile.write(privateKeyString)
privateKeyFile.flush()
sshClient.close()
def createTunnel():
print "Starting tunnelled SSH session...\n"
global localPortNumber
try:
if sys.platform.startswith("win"):
sshBinary = "ssh.exe"
if hasattr(sys, 'frozen'):
cvlDemoBinary = sys.executable
cvlDemoPath = os.path.dirname(cvlDemoBinary)
sshBinary = "\"" + os.path.join(cvlDemoPath, sshBinary) + "\""
else:
sshBinary = "\"" + os.path.join(os.getcwd(), "sshwindows", sshBinary) + "\""
else:
sshBinary = "/usr/bin/ssh"
print "Requesting ephemeral port..."
localPortNumber = "5901"
# Request an ephemeral port from the operating system (by specifying port 0) :
import socket
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.bind(('localhost', 0))
localPortNumber = sock.getsockname()[1]
sock.close()
localPortNumber = str(localPortNumber)
print "localPortNumber = " + localPortNumber
remotePortNumber = str(5900 + int(displayNumber))
tunnel_cmd = sshBinary + " -i " + privateKeyFile.name + " -c " + cipher + " " \
"-oStrictHostKeyChecking=no " \
"-L " + localPortNumber + ":localhost:" + remotePortNumber + " -l " + username+" "+cvldemo_ip_address
sys.stdout.write(tunnel_cmd + "\n")
proc = subprocess.Popen(tunnel_cmd,
universal_newlines=True,shell=True,stdout=subprocess.PIPE, stderr=subprocess.STDOUT, stdin=subprocess.PIPE)
except KeyboardInterrupt:
sys.stdout.write("C-c: Port forwarding stopped.")
os._exit(0)
except:
sys.stdout.write("CVL Demo v" + version + "\n")
sys.stdout.write(traceback.format_exc())
import threading
tunnelThread = threading.Thread(target=createTunnel)
tunnelThread.start()
time.sleep(3)
if sys.platform.startswith("win"):
vnc = r"C:\Program Files\TurboVNC\vncviewer.exe"
else:
vnc = "/opt/TurboVNC/bin/vncviewer"
if sys.platform.startswith("win"):
import _winreg
key = None
queryResult = None
foundTurboVncInRegistry = False
if not foundTurboVncInRegistry:
try:
key = _winreg.OpenKey(_winreg.HKEY_CURRENT_USER, r"SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\TurboVNC 64-bit_is1", 0, _winreg.KEY_WOW64_64KEY | _winreg.KEY_ALL_ACCESS)
queryResult = _winreg.QueryValueEx(key, "InstallLocation")
vnc = os.path.join(queryResult[0], "vncviewer.exe")
foundTurboVncInRegistry = True
except:
foundTurboVncInRegistry = False
if not foundTurboVncInRegistry:
try:
key = _winreg.OpenKey(_winreg.HKEY_LOCAL_MACHINE, r"SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\TurboVNC 64-bit_is1", 0, _winreg.KEY_WOW64_64KEY | _winreg.KEY_ALL_ACCESS)
queryResult = _winreg.QueryValueEx(key, "InstallLocation")
vnc = os.path.join(queryResult[0], "vncviewer.exe")
foundTurboVncInRegistry = True
except:
foundTurboVncInRegistry = False
if not foundTurboVncInRegistry:
try:
key = _winreg.OpenKey(_winreg.HKEY_CURRENT_USER, r"SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\TurboVNC_is1", 0, _winreg.KEY_ALL_ACCESS)
queryResult = _winreg.QueryValueEx(key, "InstallLocation")
vnc = os.path.join(queryResult[0], "vncviewer.exe")
foundTurboVncInRegistry = True
except:
foundTurboVncInRegistry = False
if not foundTurboVncInRegistry:
try:
key = _winreg.OpenKey(_winreg.HKEY_LOCAL_MACHINE, r"SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\TurboVNC_is1", 0, _winreg.KEY_ALL_ACCESS)
queryResult = _winreg.QueryValueEx(key, "InstallLocation")
vnc = os.path.join(queryResult[0], "vncviewer.exe")
foundTurboVncInRegistry = True
except:
foundTurboVncInRegistry = False
if os.path.exists(vnc):
sys.stdout.write("TurboVNC was found in " + vnc + "\n")
else:
sys.stdout.write("Error: TurboVNC was not found in " + vnc + "\n")
sys.stdout.write("\nLaunching TurboVNC...\n")
try:
if sys.platform.startswith("win"):
proc = subprocess.Popen("\""+vnc+"\" /user "+username+" /autopass localhost:" + localPortNumber,
stdout=subprocess.PIPE, stdin=subprocess.PIPE, stderr=subprocess.STDOUT, shell=True,
universal_newlines=True)
proc.communicate(input=password)
else:
subprocess.call("echo \"" + password + "\" | " + vnc + " -user " + username + " -autopass localhost:" + localPortNumber,shell=True)
print vnc + " -user " + username + " -autopass localhost:" + localPortNumber + "\n"
try:
privateKeyFile.close() # Automatically removes the temporary file.
finally:
os._exit(0)
except:
sys.stdout.write(traceback.format_exc())
except:
sys.stdout.write(traceback.format_exc())
finally:
sshClient.close()