Files changed (1) hide show
  1. ssh.py +0 -124
ssh.py DELETED
@@ -1,124 +0,0 @@
1
- import os
2
- os.system("pip install paramiko")
3
- import socket
4
- import sys
5
- import threading
6
- import paramiko
7
- import select
8
- import time
9
- # 配置参数
10
- USERNAME = 'root'
11
- PASSWORD = os.environ.get('SSH_PASSWORD')
12
- HOST = 'MikaOvO.com'
13
- REMOTE_PORT = 65432
14
- LOCAL_HOST = '127.0.0.1'
15
- LOCAL_PORT = 65432
16
- KEEP_ALIVE_INTERVAL = 60 # 发送保持连接的间隔时间(秒)
17
-
18
- # 定义一个类来处理转发请求
19
- class ReverseForwardServer(threading.Thread):
20
- def __init__(self, transport, remote_port, local_host, local_port):
21
- super(ReverseForwardServer, self).__init__()
22
- self.transport = transport
23
- self.remote_port = remote_port
24
- self.local_host = local_host
25
- self.local_port = local_port
26
- self.daemon = True # 使线程随主线程退出
27
- self.start()
28
-
29
- def run(self):
30
- # 请求远程端口转发
31
- try:
32
- self.transport.request_port_forward('', self.remote_port)
33
- print(f"请求在远程服务器上监听端口 {self.remote_port},转发到本地 {self.local_host}:{self.local_port}")
34
- except Exception as e:
35
- print(f'无法请求远程端口转发: {e}')
36
- sys.exit(1)
37
-
38
- while True:
39
- try:
40
- chan = self.transport.accept(1000) # 等待新连接,超时1秒
41
- if chan is None:
42
- continue
43
- thr = threading.Thread(target=self.handle_channel, args=(chan,))
44
- thr.daemon = True
45
- thr.start()
46
- except Exception as e:
47
- print(f"处理通道时发生错误: {e}")
48
- break
49
-
50
- def handle_channel(self, chan):
51
- try:
52
- # 连接到本地服务
53
- sock = socket.socket()
54
- sock.connect((self.local_host, self.local_port))
55
- except Exception as e:
56
- print(f"无法连接到本地服务 {self.local_host}:{self.local_port}: {e}")
57
- chan.close()
58
- return
59
-
60
- print(f"建立连接: {chan.origin_addr} -> {self.local_host}:{self.local_port}")
61
-
62
- # 双向转发数据
63
- def forward(src, dst):
64
- try:
65
- while True:
66
- data = src.recv(1024)
67
- if not data:
68
- break
69
- dst.sendall(data)
70
- except Exception:
71
- pass
72
- finally:
73
- src.close()
74
- dst.close()
75
-
76
- # 启动两个线程进行数据转发
77
- t1 = threading.Thread(target=forward, args=(chan, sock))
78
- t2 = threading.Thread(target=forward, args=(sock, chan))
79
- t1.daemon = True
80
- t2.daemon = True
81
- t1.start()
82
- t2.start()
83
-
84
- def main():
85
- while True: # 自动重连循环
86
- client = paramiko.SSHClient()
87
- client.set_missing_host_key_policy(paramiko.AutoAddPolicy()) # 自动添加主机密钥
88
-
89
- try:
90
- print(f"正在连接到 {HOST}...")
91
- client.connect(hostname=HOST, username=USERNAME, password=PASSWORD)
92
- print("连接成功!")
93
-
94
- transport = client.get_transport()
95
- if transport is None or not transport.is_active():
96
- print("SSH Transport不可用")
97
- raise Exception("SSH Transport不可用")
98
-
99
- # 设置保持连接(发送心跳)
100
- transport.set_keepalive(KEEP_ALIVE_INTERVAL)
101
- print(f"已设置保持连接,每 {KEEP_ALIVE_INTERVAL} 秒发送一次心跳包")
102
-
103
- # 启动反向端口转发服务器
104
- ReverseForwardServer(transport, REMOTE_PORT, LOCAL_HOST, LOCAL_PORT)
105
-
106
- print(f"已在远程服务器 {HOST} 上监听端口 {REMOTE_PORT},并转发到本地 {LOCAL_HOST}:{LOCAL_PORT}")
107
-
108
- # 保持主线程运行,并监控连接状态
109
- while transport.is_active():
110
- time.sleep(1)
111
-
112
- except KeyboardInterrupt:
113
- print("用户中断,关闭连接。")
114
- break
115
- except Exception as e:
116
- print(f"发生错误: {e}")
117
- print("尝试重新连接...")
118
- time.sleep(5) # 等待5秒后重试
119
- finally:
120
- client.close()
121
- print("SSH连接已关闭。")
122
-
123
- if __name__ == "__main__":
124
- main()