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)
|
||||
Reference in New Issue
Block a user