-
Notifications
You must be signed in to change notification settings - Fork 0
/
SSH.py
executable file
·71 lines (51 loc) · 1.64 KB
/
SSH.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
#!/usr/bin/env python3
import subprocess
import signal
import time
from ec2Instances import list_awssocks_running_instances, awssocks_instance_ip_address, awssocks_instance_state
from env import AWSSOCKS_KEY, AWSSOCKS_LOCAL_SSH_PORT, log_configuration
from loggerFactory import logger_factory
logger = logger_factory(__name__)
def check_instance_status():
instance_ids = list_awssocks_running_instances()
if not instance_ids:
logger.info("No EC2 instances found.")
return None
return instance_ids[0]
def start_ssh_tunnel(instance_id):
public_ip = awssocks_instance_ip_address(instance_id)
ssh_command = [
'ssh',
'-o', 'StrictHostKeyChecking=no',
'-C', '-N',
'-i', f'~/.ssh/{AWSSOCKS_KEY}',
f'ec2-user@{public_ip}',
'-D', f'{AWSSOCKS_LOCAL_SSH_PORT}'
]
logger.info("Executing SSH command: %s", ' '.join(ssh_command))
process = subprocess.Popen(ssh_command)
def signal_handler(sig, frame):
logger.info("Terminating SSH tunnel...")
process.terminate()
process.wait()
logger.info("...SSH tunnel closed")
logger.info('-' * 42)
logger.info("SSH TUNNEL")
logger.info('-' * 42)
exit(0)
signal.signal(signal.SIGINT, signal_handler)
while True:
time.sleep(1)
def main():
logger.info('=' * 42)
logger.info("SSH TUNNEL")
logger.info('-' * 42)
log_configuration()
instance = check_instance_status()
if instance:
start_ssh_tunnel(instance)
logger.info('-' * 42)
logger.info("SSH TUNNEL")
logger.info('-' * 42)
if __name__ == '__main__':
main()