modify dirs
This commit is contained in:
60
tools/auto_init.py
Normal file
60
tools/auto_init.py
Normal file
@ -0,0 +1,60 @@
|
|||||||
|
import paramiko
|
||||||
|
|
||||||
|
# 服务器列表,包含主机名、端口、用户名和密码
|
||||||
|
servers = [
|
||||||
|
{
|
||||||
|
"hostname": "your_host1",
|
||||||
|
"port": 22,
|
||||||
|
"username": "your_username1",
|
||||||
|
"password": "your_password1"
|
||||||
|
}
|
||||||
|
]
|
||||||
|
|
||||||
|
# Git 仓库信息
|
||||||
|
git_username = "your_git_username"
|
||||||
|
git_password = "your_git_password"
|
||||||
|
git_repo_url = "https://github.com/your-repo/your-project.git"
|
||||||
|
# 嵌入凭证的 Git URL
|
||||||
|
git_url_with_credentials = f"https://{git_username}:{git_password}@github.com/your-repo/your-project.git"
|
||||||
|
|
||||||
|
# 要执行的命令列表
|
||||||
|
commands = [
|
||||||
|
"sudo apt update",
|
||||||
|
"sudo apt install -y python3 python3-pip git",
|
||||||
|
"pip3 install requests numpy",
|
||||||
|
"mkdir -p /path/to/your/project",
|
||||||
|
f"git clone {git_url_with_credentials} /path/to/your/project",
|
||||||
|
"cd /path/to/your/project && python3 init_script.py",
|
||||||
|
"cd /path/to/your/project && python3 main.py"
|
||||||
|
]
|
||||||
|
|
||||||
|
|
||||||
|
def execute_commands_on_server(server):
|
||||||
|
try:
|
||||||
|
ssh = paramiko.SSHClient()
|
||||||
|
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
|
||||||
|
ssh.connect(hostname=server["hostname"], port=server["port"],
|
||||||
|
username=server["username"], password=server["password"])
|
||||||
|
|
||||||
|
for command in commands:
|
||||||
|
print(f"在 {server['hostname']} 上执行命令: {command}")
|
||||||
|
stdin, stdout, stderr = ssh.exec_command(command)
|
||||||
|
result = stdout.read().decode()
|
||||||
|
error = stderr.read().decode()
|
||||||
|
|
||||||
|
if result:
|
||||||
|
print(f"命令输出:\n{result}")
|
||||||
|
if error:
|
||||||
|
print(f"命令错误信息:\n{error}")
|
||||||
|
|
||||||
|
ssh.close()
|
||||||
|
except paramiko.AuthenticationException:
|
||||||
|
print(f"连接 {server['hostname']} 时认证失败,请检查用户名和密码。")
|
||||||
|
except paramiko.SSHException as ssh_ex:
|
||||||
|
print(f"连接 {server['hostname']} 时发生 SSH 异常: {ssh_ex}")
|
||||||
|
except Exception as ex:
|
||||||
|
print(f"连接 {server['hostname']} 时发生未知异常: {ex}")
|
||||||
|
|
||||||
|
|
||||||
|
for server in servers:
|
||||||
|
execute_commands_on_server(server)
|
||||||
70
tools/send_to_wecom.py
Normal file
70
tools/send_to_wecom.py
Normal file
@ -0,0 +1,70 @@
|
|||||||
|
import requests
|
||||||
|
import time
|
||||||
|
import sys
|
||||||
|
|
||||||
|
# 企业微信相关信息
|
||||||
|
CORP_ID = 'ww5d7d350d9b8c0be3'
|
||||||
|
SECRET = 'YhagYQpaNIK9j1ATopgKNQhw3D13mpGZ64YVr23Je-A'
|
||||||
|
AGENT_ID = '1000003'
|
||||||
|
|
||||||
|
# 获取 access_token
|
||||||
|
def get_access_token():
|
||||||
|
url = f'https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid={CORP_ID}&corpsecret={SECRET}'
|
||||||
|
response = requests.get(url)
|
||||||
|
result = response.json()
|
||||||
|
if result.get('errcode') == 0:
|
||||||
|
return result.get('access_token')
|
||||||
|
else:
|
||||||
|
print(f"获取 access_token 失败: {result.get('errmsg')}")
|
||||||
|
return None
|
||||||
|
|
||||||
|
# 发送消息到企业微信
|
||||||
|
def send_message(access_token, message, touser=None, toparty=None, totag=None):
|
||||||
|
url = f'https://qyapi.weixin.qq.com/cgi-bin/message/send?access_token={access_token}'
|
||||||
|
data = {
|
||||||
|
"msgtype": "text",
|
||||||
|
"agentid": AGENT_ID,
|
||||||
|
"text": {
|
||||||
|
"content": message
|
||||||
|
},
|
||||||
|
"safe": 0
|
||||||
|
}
|
||||||
|
if touser:
|
||||||
|
data["touser"] = touser
|
||||||
|
if toparty:
|
||||||
|
data["toparty"] = toparty
|
||||||
|
if totag:
|
||||||
|
data["totag"] = totag
|
||||||
|
|
||||||
|
response = requests.post(url, json=data)
|
||||||
|
result = response.json()
|
||||||
|
if result.get('errcode') == 0:
|
||||||
|
print("消息发送成功")
|
||||||
|
else:
|
||||||
|
print(f"消息发送失败: {result.get('errmsg')}")
|
||||||
|
|
||||||
|
# 主函数
|
||||||
|
def main(report_content=None):
|
||||||
|
# 模拟数据报表内容
|
||||||
|
if report_content is None:
|
||||||
|
report_content = "这是第一行\n这是第二行\n这是第三行"
|
||||||
|
else:
|
||||||
|
# 处理转义字符
|
||||||
|
report_content = report_content.encode().decode('unicode_escape')
|
||||||
|
|
||||||
|
# 获取 access_token
|
||||||
|
access_token = get_access_token()
|
||||||
|
if access_token:
|
||||||
|
# 示例:发送给特定人员
|
||||||
|
send_message(access_token, report_content, touser='oscar')
|
||||||
|
# 示例:发送给特定部门
|
||||||
|
# send_message(access_token, report_content, toparty='department1|department2')
|
||||||
|
# 示例:发送给特定标签
|
||||||
|
# send_message(access_token, report_content, totag='tag1|tag2')
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
if len(sys.argv) > 1:
|
||||||
|
message = sys.argv[1]
|
||||||
|
main(message)
|
||||||
|
else:
|
||||||
|
main()
|
||||||
42
tools/ssh_key_push.sh
Normal file
42
tools/ssh_key_push.sh
Normal file
@ -0,0 +1,42 @@
|
|||||||
|
#!/bin/bash
|
||||||
|
|
||||||
|
# 检查本地是否存在 id_rsa.pub
|
||||||
|
if [ ! -f ~/.ssh/id_rsa.pub ]; then
|
||||||
|
echo "本地未找到 id_rsa.pub,开始生成新的 SSH 密钥对..."
|
||||||
|
ssh-keygen -t rsa -b 4096 -C "oscar@easyprompt8.com" -N "" -f ~/.ssh/id_rsa
|
||||||
|
if [ $? -eq 0 ]; then
|
||||||
|
echo "SSH 密钥对生成成功。"
|
||||||
|
else
|
||||||
|
echo "生成 SSH 密钥对时出错。"
|
||||||
|
exit 1
|
||||||
|
fi
|
||||||
|
fi
|
||||||
|
|
||||||
|
# 处理服务器列表
|
||||||
|
# bash push_ssh_key.sh user1@server1 user2@server2
|
||||||
|
if [ $# -gt 0 ]; then
|
||||||
|
servers=("$@")
|
||||||
|
else
|
||||||
|
echo "请提供包含服务器列表的文件路径:"
|
||||||
|
read file_path
|
||||||
|
if [ ! -f "$file_path" ]; then
|
||||||
|
echo "文件 $file_path 不存在。"
|
||||||
|
exit 1
|
||||||
|
fi
|
||||||
|
servers=()
|
||||||
|
while IFS= read -r line; do
|
||||||
|
servers+=("$line")
|
||||||
|
done < "$file_path"
|
||||||
|
fi
|
||||||
|
|
||||||
|
# 推送公钥到远程服务器
|
||||||
|
for server in "${servers[@]}"; do
|
||||||
|
public_key=$(cat ~/.ssh/id_rsa.pub)
|
||||||
|
ssh "$server" "mkdir -p ~/.ssh && echo '$public_key' >> ~/.ssh/authorized_keys"
|
||||||
|
if [ $? -eq 0 ]; then
|
||||||
|
echo "公钥已成功推送到 $server"
|
||||||
|
else
|
||||||
|
echo "推送公钥到 $server 时出错。"
|
||||||
|
fi
|
||||||
|
done
|
||||||
|
|
||||||
Reference in New Issue
Block a user