- 新增服务器控制页面,支持查看服务器状态和执行关机、重启操作 - 添加移动端适配页面,优化小屏显示效果 - 实现服务器信息展示、折叠面板、模态框等功能 - 添加 Docker 部署相关文件,支持容器化部署 - 新增测试脚本,验证 SSH 连接和命令执行功能
43 行
1.3 KiB
Python
43 行
1.3 KiB
Python
import paramiko
|
|
|
|
# 配置连接参数
|
|
hostname = "***REMOVED***" # 远程服务器的 IP 地址
|
|
port = 22 # SSH 端口,通常是 22
|
|
username = "root" # SSH 用户名
|
|
password = "***REMOVED***" # SSH 密码,或使用密钥认证
|
|
|
|
# SSH 连接并执行关机命令
|
|
def shutdown_server():
|
|
try:
|
|
# 创建 SSH 客户端对象
|
|
client = paramiko.SSHClient()
|
|
# 自动添加主机密钥(如果是首次连接)
|
|
client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
|
|
# 连接到远程服务器
|
|
client.connect(hostname, port, username, password)
|
|
|
|
# 执行关机命令
|
|
# stdin, stdout, stderr = client.exec_command('sudo shutdown -h now')
|
|
stdin, stdout, stderr = client.exec_command('sudo ls /')
|
|
|
|
# 如果需要输入 sudo 密码,可以通过 stdin 传递
|
|
stdin.write('your_sudo_password\n')
|
|
stdin.flush()
|
|
|
|
# 输出命令执行结果
|
|
output = stdout.read().decode()
|
|
error = stderr.read().decode()
|
|
|
|
if output:
|
|
print("Output:", output)
|
|
if error:
|
|
print("Error:", error)
|
|
|
|
# 关闭 SSH 连接
|
|
client.close()
|
|
|
|
except Exception as e:
|
|
print(f"An error occurred: {e}")
|
|
|
|
if __name__ == "__main__":
|
|
shutdown_server() |