forked from ychappyman/SSHTGBot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgroup_run.py
233 lines (209 loc) · 10.3 KB
/
group_run.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
import json
import asyncio
from datetime import datetime, timedelta
import os
import paramiko
import aiohttp
import logging
import traceback
from translations import get_translation
from language_manager import language_manager
logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(name)s - %(levelname)s - %(message)s')
logger = logging.getLogger(__name__)
TELEGRAM_BOT_TOKEN = os.getenv('TELEGRAM_BOT_TOKEN')
TELEGRAM_CHAT_ID = os.getenv('TELEGRAM_CHAT_ID')
# 默认命令
DEFAULT_COMMAND = "source ~/.profile && pm2 resurrect"
def format_to_iso(date):
return date.strftime('%Y-%m-%d %H:%M:%S')
async def execute_ssh_command(sslhost, ssluser, password, command, global_path, customhostname='', vps_path=None, secret_key_path=None):
client = paramiko.SSHClient()
client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
try:
language = language_manager.get_language()
logger.info(get_translation('processing_account', language).format(account=f"{customhostname + ': ' if customhostname else ''}{ssluser}@{sslhost}"))
connection_start = asyncio.get_event_loop().time()
try:
if secret_key_path:
private_key = paramiko.RSAKey.from_private_key_file(secret_key_path)
await asyncio.wait_for(
asyncio.get_event_loop().run_in_executor(
None,
lambda: client.connect(sslhost, username=ssluser, pkey=private_key)
),
timeout=10
)
else:
await asyncio.wait_for(
asyncio.get_event_loop().run_in_executor(
None,
lambda: client.connect(sslhost, username=ssluser, password=password)
),
timeout=10
)
except asyncio.TimeoutError:
await send_telegram_message(get_translation('connecting_to_host', language).format(host=f"{customhostname + ': ' if customhostname else ''}{ssluser}@{sslhost}"))
try:
if secret_key_path:
await asyncio.wait_for(
asyncio.get_event_loop().run_in_executor(
None,
lambda: client.connect(sslhost, username=ssluser, pkey=private_key)
),
timeout=20
)
else:
await asyncio.wait_for(
asyncio.get_event_loop().run_in_executor(
None,
lambda: client.connect(sslhost, username=ssluser, password=password)
),
timeout=20
)
except asyncio.TimeoutError:
await send_telegram_message(get_translation('connection_failed', language).format(host=f"{customhostname + ': ' if customhostname else ''}{ssluser}@{sslhost}"))
return None, None, "Connection Timeout", None
if global_path:
if vps_path: # 如果主机有自己的路径设置
cd_command = f"cd {vps_path} && "
full_command = cd_command + global_path
else: # 如果主机没有自己的路径设置
full_command = command # 使用普通的setcommand命令
else:
full_command = command
logger.info(f"Executing command: {full_command}")
command_start = asyncio.get_event_loop().time()
async def execute_and_read():
stdin, stdout, stderr = await asyncio.get_event_loop().run_in_executor(
None, lambda: client.exec_command(full_command)
)
output = await asyncio.get_event_loop().run_in_executor(None, stdout.read)
error = await asyncio.get_event_loop().run_in_executor(None, stderr.read)
return output.decode(), error.decode()
try:
output, error = await asyncio.wait_for(execute_and_read(), timeout=10)
except asyncio.TimeoutError:
await send_telegram_message(get_translation('command_execution_timeout', language).format(host=f"{customhostname + ': ' if customhostname else ''}{ssluser}@{sslhost}"))
try:
output, error = await asyncio.wait_for(execute_and_read(), timeout=110)
except asyncio.TimeoutError:
await send_telegram_message(get_translation('command_execution_failed', language).format(host=f"{customhostname + ': ' if customhostname else ''}{ssluser}@{sslhost}"))
return None, None, f"Command Execution Timeout: {full_command}", full_command
command_time = asyncio.get_event_loop().time() - command_start
if command_time > 10:
logger.info(f"Command execution took {command_time:.2f} seconds")
if not error:
return client, output, None, full_command
return client, None, error, full_command
except Exception as e:
logger.error(get_translation('host_operation_error', language).format(host=f"{customhostname + ': ' if customhostname else ''}{ssluser}@{sslhost}", error=str(e), traceback=traceback.format_exc()))
return None, None, str(e), None
finally:
if client:
client.close()
async def process_account(account, send_messages, command, global_path):
ssluser = account.get('ssluser') or account.get('username')
password = account.get('password')
sslhost = account.get('sslhost') or account.get('hostname')
vps_path = account.get('path')
customhostname = account.get('customhostname', '').lower()
secret_key_path = account.get('secretkey')
now_utc = format_to_iso(datetime.utcnow())
now_beijing = format_to_iso(datetime.utcnow() + timedelta(hours=8))
language = language_manager.get_language()
logger.info(get_translation('processing_account', language).format(account=customhostname or ssluser))
try:
client, output, error, full_command = await asyncio.wait_for(
execute_ssh_command(sslhost, ssluser, password, command, global_path, customhostname, vps_path, secret_key_path),
timeout=180 # 总超时时间设置为180秒(3分钟)
)
except asyncio.TimeoutError:
await send_telegram_message(get_translation('host_operation_timeout', language).format(host=f"{customhostname + ': ' if customhostname else ''}{ssluser}@{sslhost}"))
return False
except Exception as e:
error_message = get_translation('host_operation_error', language).format(
host=f"{customhostname + ': ' if customhostname else ''}{ssluser}@{sslhost}",
error=str(e),
traceback=traceback.format_exc()
)
logger.error(error_message)
await send_telegram_message(error_message)
return False
if client:
login_success_message = get_translation('host_login_success', language).format(
host=f"{customhostname + ': ' if customhostname else ''}{ssluser}@{sslhost}",
beijing_time=now_beijing,
utc_time=now_utc
)
logger.info(login_success_message)
if send_messages:
await send_telegram_message(login_success_message)
if not error:
ssh_success_message = get_translation('host_command_success', language).format(
host=f"{customhostname + ': ' if customhostname else ''}{ssluser}@{sslhost}",
command=full_command
)
logger.info(ssh_success_message)
if send_messages:
await send_telegram_message(ssh_success_message)
return True
else:
ssh_error_message = get_translation('host_command_failed', language).format(
host=f"{customhostname + ': ' if customhostname else ''}{ssluser}@{sslhost}",
command=full_command,
error=error
)
logger.error(ssh_error_message)
if send_messages:
await send_telegram_message(ssh_error_message)
else:
ssh_error_message = get_translation('host_login_failed', language).format(
host=f"{customhostname + ': ' if customhostname else ''}{ssluser}@{sslhost}",
beijing_time=now_beijing,
utc_time=now_utc,
error=error
)
logger.error(ssh_error_message)
if send_messages:
await send_telegram_message(ssh_error_message)
return False
async def main(accounts, send_messages=True, command=DEFAULT_COMMAND, global_path=None):
tasks = [process_account(account, send_messages, command, global_path) for account in accounts]
results = await asyncio.gather(*tasks)
success_count = sum(results)
total_count = len(accounts)
language = language_manager.get_language()
completion_message = get_translation('all_hosts_complete', language).format(success_count=success_count, total_count=total_count)
logger.info(completion_message)
if send_messages:
await send_telegram_message(completion_message)
return success_count, total_count
async def send_telegram_message(message):
url = f"https://api.telegram.org/bot{TELEGRAM_BOT_TOKEN}/sendMessage"
payload = {
'chat_id': TELEGRAM_CHAT_ID,
'text': message
}
headers = {
'Content-Type': 'application/json'
}
try:
async with aiohttp.ClientSession() as session:
async with session.post(url, json=payload, headers=headers) as response:
if response.status != 200:
logger.error(f"发送消息到Telegram失败: {await response.text()}")
else:
logger.info(f"成功发送消息到Telegram: {message[:50]}...")
except Exception as e:
logger.error(f"发送消息到Telegram时发生错误: {str(e)}")
def run_main(send_messages=True, command=DEFAULT_COMMAND, global_path=None):
accounts_json = os.getenv('ACCOUNTS_JSON')
if accounts_json:
accounts = json.loads(accounts_json)
return asyncio.run(main(accounts, send_messages, command, global_path))
else:
language = language_manager.get_language()
logger.error(get_translation('no_accounts_json', language))
return 0, 0
if __name__ == "__main__":
run_main()