65 lines
2.3 KiB
Python
65 lines
2.3 KiB
Python
|
#报错提示
|
||
|
import paramiko
|
||
|
import time
|
||
|
import threading
|
||
|
|
||
|
# 执行命令的函数
|
||
|
def execute_commands_on_server(hostname, username, password, host):
|
||
|
try:
|
||
|
# 连接到服务器
|
||
|
client = paramiko.SSHClient()
|
||
|
client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
|
||
|
client.connect(hostname=hostname, username=username, password=password)
|
||
|
|
||
|
# 执行命令
|
||
|
shell = client.invoke_shell()
|
||
|
#启动docker
|
||
|
shell.send("cd /root/pdf_parser/zzb_data_prod\n")
|
||
|
time.sleep(1)
|
||
|
shell.send("conda activate py310\n")
|
||
|
time.sleep(1)
|
||
|
shell.send("ps -ef | grep app.py | grep -v grep | awk '{print $2}' | xargs -r kill -9\n")
|
||
|
time.sleep(1)
|
||
|
shell.send("nohup python app.py > app.log 2>&1 &\n")
|
||
|
time.sleep(1)
|
||
|
# 读取输出
|
||
|
output = shell.recv(2048).decode()
|
||
|
print(f"Output from {hostname}:\n{output}")
|
||
|
|
||
|
except paramiko.SSHException as e:
|
||
|
print(f"SSH connection error with {hostname}: {e}")
|
||
|
|
||
|
finally:
|
||
|
client.close()
|
||
|
|
||
|
# 创建线程函数
|
||
|
def thread_function(server):
|
||
|
execute_commands_on_server(server['hostname'], server['username'], server['password'], server['host'])
|
||
|
|
||
|
servers = [
|
||
|
#{'hostname': '192.168.0.163', 'username': 'root', 'password': 's6fQeVQmxxNv','host':'企业服务器1'},
|
||
|
#{'hostname': '192.168.0.26', 'username': 'root', 'password': 's6fQeVQmxxNv','host':'企业服务器2'},
|
||
|
#{'hostname': '192.168.0.2', 'username': 'root', 'password': 's6fQeVQmxxNv','host':'企业服务器3'},
|
||
|
#{'hostname': '192.168.0.128', 'username': 'root', 'password': 's6fQeVQmxxNv','host':'企业服务器4'},
|
||
|
#{'hostname': '192.168.0.136', 'username': 'root', 'password': 's6fQeVQmxxNv','host':'企业服务器5'},
|
||
|
#{'hostname': '192.168.0.239', 'username': 'root', 'password': 's6fQeVQmxxNv','host':'企业服务器6'},
|
||
|
{'hostname': '192.168.0.108', 'username': 'root', 'password': 's6fQeVQmxxNv','host':'监管服务器1'},
|
||
|
{'hostname': '192.168.0.131', 'username': 'root', 'password': 's6fQeVQmxxNv','host':'监管服务器2'},
|
||
|
|
||
|
#
|
||
|
]
|
||
|
|
||
|
# 创建并启动线程
|
||
|
threads = []
|
||
|
for server in servers:
|
||
|
thread = threading.Thread(target=thread_function, args=(server,))
|
||
|
threads.append(thread)
|
||
|
thread.start()
|
||
|
|
||
|
# 等待所有线程完成
|
||
|
for thread in threads:
|
||
|
thread.join()
|
||
|
|
||
|
print("All commands executed.")
|
||
|
|