-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
executable file
·125 lines (79 loc) · 3.79 KB
/
main.py
File metadata and controls
executable file
·125 lines (79 loc) · 3.79 KB
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
import argparse
from ssh_util.reverse_ssh_linux import ReverseSSHLinux
from ssh_util.reverse_ssh_registry_linux import ReverseSSHRegistryLinux
import sys
import platform
def main():
"""
@overview Command-line interface to configure and start a reverse SSH tunnel.
"""
parser = argparse.ArgumentParser(description="Reverse SSH Tunnel Setup Tool", add_help=False)
parser.add_argument(
"--help",
action="help",
help="[ To show the options ]"
)
parser.add_argument("--host", "-h", help="Remote SSH host (e.g., ssh.example.com)")
parser.add_argument("--user", "-u", help="Username to connect to the remote host")
parser.add_argument("--remote-port", "-rp", type=int, default=1248, help="Remote SSH server port (default: 1248)")
parser.add_argument("--bind-port", "-bp", type=int, default=8421, help="Remote bind port for the tunnel (default: 8421)")
parser.add_argument("--local-port", "-lp", type=int, default=1632, help="Local port to forward to (default: 1632)")
# For the PID associated with the remote bind
parser.add_argument("--list-tunnel", "-lt", action="store_true", help="List active reverse SSH tunnels")
parser.add_argument("--kill-tunnel", "-kt", type=int, nargs='+', help="Kill a reverse SSH tunnel by bind port (nargs)")
# There is no argument display `help` to see the options
if len(sys.argv) == 1:
parser.print_help()
sys.exit(0)
args = parser.parse_args()
# Linux platform
if platform.system() == "Linux":
if args.list_tunnel:
registry = ReverseSSHRegistryLinux()
tunnel_dict = registry.list_ssh_tunnel()
if not tunnel_dict:
print("\n[❗] No active reverse tunnel found")
else:
print("\n🔁 Active Reverse Tunnels : ")
for bind_port, info in tunnel_dict.items():
print(f" - Bind Port: {bind_port}; Remote: {info['remote_user']}@{info['remote_host']}")
sys.exit(0)
if args.kill_tunnel is not None:
registry = ReverseSSHRegistryLinux()
print("")
for bind_port in args.kill_tunnel:
registry.kill_ssh_tunnel(bind_port)
sys.exit(0)
if args.bind_port is not None:
registry = ReverseSSHRegistryLinux()
tunnel_dict = registry.list_ssh_tunnel()
if tunnel_dict:
for bind_port, info in tunnel_dict.items():
if int(bind_port) == int(args.bind_port):
print(f"\n[❗] The bind port {bind_port} is already in use...")
sys.exit(1)
# Normal tunnel setup flow
if not (args.host and args.user):
parser.error(f"⛔ The following arguments are required for tunnel creation : --host and --user")
# Check internet connection
if not ReverseSSHLinux.has_internet_connection():
print(f"\n[❗] No internet connection detected. Remote SSH setup cannot proceed")
sys.exit(1)
ssh_client = ReverseSSHLinux(
remote_user=args.user,
remote_host=args.host,
remote_bind_port=args.bind_port,
remote_port=args.remote_port,
local_port=args.local_port
)
print("\n--- Reverse SSH Setup ---\n")
try:
ssh_client.ensure_ssh_local()
ssh_client.generate_ssh_key_pair_local()
ssh_client.push_ssh_pubkey_local()
ssh_client.start_reverse_ssh_tunnel()
print("[✅] Reverse SSH tunnel established successfully\n")
except Exception as err:
print(f"[❗] Error : {err}\n")
if __name__ == "__main__":
main()