Skip to content

Commit

Permalink
新增cron参数,支持针对每个服务器自定义定时命令
Browse files Browse the repository at this point in the history
  • Loading branch information
yixiu001 committed Jul 17, 2024
1 parent 2394528 commit 11f30f5
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 32 deletions.
16 changes: 9 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,19 +30,21 @@
```json
[
{
"host": "server1_ip",
"host": "example1.com",
"port": 22,
"username": "your_username",
"password": "your_password"
"username": "user1",
"password": "password1",
"cron": "cd ~/domains/$USER.serv00.net/vless && ./check_vless.sh"
},
{
"host": "server2_ip",
"host": "example2.com",
"port": 22,
"username": "your_username",
"password": "your_password"
"username": "user2",
"password": "password2"
// 没有cron参数,使用默认命令
}
// 添加更多服务器
]

```

2. **设置 Telegram Secrets**
Expand Down
51 changes: 26 additions & 25 deletions vless/recover_vless.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,59 +3,60 @@
import subprocess
import requests

def send_telegram_message(token, chat_id, message):
telegram_url = f"https://api.telegram.org/bot{token}/sendMessage"
telegram_payload = {
"chat_id": chat_id,
"text": message,
"reply_markup": '{"inline_keyboard":[[{"text":"问题反馈❓","url":"https://t.me/yxjsjl"}]]}'
}

response = requests.post(telegram_url, json=telegram_payload)
print(f"Telegram 请求状态码:{response.status_code}")
print(f"Telegram 请求返回内容:{response.text}")

if response.status_code != 200:
print("发送 Telegram 消息失败")
else:
print("发送 Telegram 消息成功")

# 从环境变量中获取密钥
accounts_json = os.getenv('ACCOUNTS_JSON')
telegram_token = os.getenv('TELEGRAM_TOKEN')
telegram_chat_id = os.getenv('TELEGRAM_CHAT_ID')

# 解析 JSON 字符串
# 检查并解析 JSON 字符串
try:
servers = json.loads(accounts_json)
except json.JSONDecodeError:
print("ACCOUNTS_JSON 参数格式错误")
error_message = "ACCOUNTS_JSON 参数格式错误"
print(error_message)
send_telegram_message(telegram_token, telegram_chat_id, error_message)
exit(1)

# 初始化汇总消息
summary_message = "serv00-vless 恢复操作结果:\n"

# 默认恢复命令
default_restore_command = "cd ~/domains/$USER.serv00.net/vless && ./check_vless.sh"

# 遍历服务器列表并执行恢复操作
for server in servers:
host = server['host']
port = server['port']
username = server['username']
password = server['password']
cron_command = server.get('cron', default_restore_command)

print(f"连接到 {host}...")

# 执行恢复命令(这里假设使用 SSH 连接和密码认证)
restore_command = f"sshpass -p '{password}' ssh -o StrictHostKeyChecking=no -p {port} {username}@{host} 'cd ~/domains/$USER.serv00.net/vless && ./check_vless.sh'"
restore_command = f"sshpass -p '{password}' ssh -o StrictHostKeyChecking=no -p {port} {username}@{host} '{cron_command}'"
try:
output = subprocess.check_output(restore_command, shell=True, stderr=subprocess.STDOUT)
summary_message += f"\n成功恢复 {host} 上的 vless 服务:\n{output.decode('utf-8')}"
except subprocess.CalledProcessError as e:
summary_message += f"\n无法恢复 {host} 上的 vless 服务:\n{e.output.decode('utf-8')}"

# 发送汇总消息到 Telegram
telegram_url = f"https://api.telegram.org/bot{telegram_token}/sendMessage"
telegram_payload = {
"chat_id": telegram_chat_id,
"text": summary_message,
"reply_markup": '{"inline_keyboard":[[{"text":"问题反馈❓","url":"https://t.me/yxjsjl"}]]}'
}

# 打印请求的详细信息
print(f"Telegram 请求 URL: {telegram_url}")
print(f"Telegram 请求 Payload: {telegram_payload}")

# 发送请求到 Telegram
response = requests.post(telegram_url, json=telegram_payload)

# 打印请求的状态码和返回内容
print(f"Telegram 请求状态码:{response.status_code}")
print(f"Telegram 请求返回内容:{response.text}")

if response.status_code != 200:
print("发送 Telegram 消息失败")
else:
print("发送 Telegram 消息成功")
send_telegram_message(telegram_token, telegram_chat_id, summary_message)

0 comments on commit 11f30f5

Please sign in to comment.