-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlibtunnel.py
executable file
·94 lines (70 loc) · 2.59 KB
/
libtunnel.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
#!/usr/bin/env python3
import paramiko
import sshtunnel
import socket
import os
pkey = os.path.expanduser('~/.ssh/id_rsa')
rt_server = 'rt.cohesity.com'
rt_port = 22
rt_username = 'cohesity'
cluster_username = 'cohesity'
cluster_password = 'Cohe$1ty'
local_ip = '127.0.0.1'
def _create_tunnel(port):
server = sshtunnel.SSHTunnelForwarder(
(rt_server, rt_port),
ssh_username=rt_username,
ssh_pkey=pkey,
remote_bind_address=(rt_server, port),
local_bind_address=(local_ip, port))
return server
def _check_port(port_num):
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
result = sock.connect_ex((local_ip, port_num))
sock.close()
return result
def _get_cluster_details(cluster_id):
client = paramiko.SSHClient()
client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
try:
client.connect(rt_server, username=rt_username, key_filename=pkey)
except paramiko.ssh_exception.SSHException:
os.system("/usr/bin/ssh-keygen -N '' -p -m PEM -f {}".format(pkey))
client.connect(rt_server, username=rt_username, key_filename=pkey)
_, stdout, _ = client.exec_command("tf {}".format(cluster_id))
output = stdout.read().decode('utf-8')
cluster_port = int(output.split(':')[2])
client.close()
return cluster_port
def open_tunnel(cluster_id):
server = None
remote_port = _get_cluster_details(cluster_id)
port_open_status = _check_port(remote_port)
if port_open_status != 0:
try:
server = _create_tunnel(remote_port)
except ValueError:
os.system("/usr/bin/ssh-keygen -N '' -p -m PEM -f {}".format(pkey))
server = _create_tunnel()
server.start()
else:
raise ValueError ('Port is already open')
os.sys.exit(1)
return (server, server.local_bind_port)
def close_tunnel(tunnel_name):
if isinstance(tunnel_name, sshtunnel.SSHTunnelForwarder):
tunnel_name.stop()
else:
print("Cannot close {}, not of type SSHTunnelForwarder".format(server_name))
os._exit(2)
def cluster_run(cluster_id, cmd):
(tunnel, local_port) = open_tunnel(cluster_id)
client = paramiko.SSHClient()
client.load_system_host_keys()
client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
client.connect(local_ip, local_port, username=cluster_username, password=cluster_password)
_, stdout, _ = client.exec_command("{} 2>&1".format(cmd))
output = stdout.read().decode('utf-8')
client.close()
close_tunnel(tunnel)
return output