最美情侣中文字幕电影,在线麻豆精品传媒,在线网站高清黄,久久黄色视频

歡迎光臨散文網(wǎng) 會員登陸 & 注冊

Python下基于paramiko打造自動配置ubuntu22.04

2023-08-07 20:52 作者:奔跑D艾緹大薯  | 我要投稿

import paramiko

import time


def execute_command(ssh_client, command):

? ? stdin, stdout, stderr = ssh_client.exec_command(command)

? ? exit_status = stdout.channel.recv_exit_status()

? ? output = stdout.read().decode("utf-8")

? ? return exit_status, output


def wait_for_reboot(ssh_client, hostname, port, username, password):

? ? while True:

? ? ? ? time.sleep(600) ?# 等待一段時間,避免頻繁嘗試連接

? ? ? ? try:

? ? ? ? ? ? ssh_client.connect(hostname, port, username, password)

? ? ? ? ? ? ssh_client.close()

? ? ? ? except paramiko.ssh_exception.SSHException:

? ? ? ? ? ? print("主機尚未重啟完成,繼續(xù)等待...")

? ? ? ? else:

? ? ? ? ? ? print("主機重啟完成")

? ? ? ? ? ? break


def run_as_root(ssh_client, sudo_password, commands):

? ? for command in commands:

? ? ? ? print(f"正在執(zhí)行命令:{command}")

? ? ? ? # 使用sudo切換到root權(quán)限執(zhí)行命令

? ? ? ? full_command = f"echo '{sudo_password}' | sudo -S {command}"

? ? ? ? exit_status, command_output = execute_command(ssh_client, full_command)

? ? ? ? print("命令執(zhí)行結(jié)果:")

? ? ? ? print(command_output)

? ? ? ? print(f"退出狀態(tài):{exit_status}")


if __name__ == "__main__":

? ? hostname = "192.168.154.136"

? ? port = 22

? ? username = "lwp"

? ? password = "ABCabc@123"

? ? sudo_password = "ABCabc@123" ?# sudo密碼


? ? try:

? ? ? ? # 創(chuàng)建SSH客戶端實例

? ? ? ? ssh_client = paramiko.SSHClient()

? ? ? ? ssh_client.set_missing_host_key_policy(paramiko.AutoAddPolicy())


? ? ? ? # 連接到Linux主機

? ? ? ? ssh_client.connect(hostname, port, username, password)


? ? ? ? # 切換到root權(quán)限并執(zhí)行命令

? ? ? ? root_commands = [

? ? ? ? ? ? # 在此添加要在root權(quán)限下執(zhí)行的命令

? ? ? ? ? ? "apt update -y",

? ? ? ? ? ? "apt install vim -y",

? ? ? ? ? ? "apt install build-essential -y",

? ? ? ? ? ? "apt install stress -y",

? ? ? ? ? ? "apt install htop -y",

? ? ? ? ? ? "apt install lm-sensors -y",


? ? ? ? ? ? # 安裝nohup命令

? ? ? ? ? ? "apt-get install coreutils",

? ? ? ? ? ? "mkdir DRIVER",

? ? ? ? ? ? "mkdir GPU",

? ? ? ? ? ? ? ? ? ? ? ?

? ? ? ? ? ? # 下載Nvidia 530.41驅(qū)動

? ? ? ? ? ? "wget https://us.download.nvidia.com/XFree86/Linux-x86_64/530.41.03/NVIDIA-Linux-x86_64-530.41.03.run -P DRIVER/",

? ? ? ? ? ? "cd DRIVER/",

? ? ? ? ? ? "chmod +x ./DRIVER/NVIDIA-Linux-x86_64-530.41.03.run",

? ? ? ? ? ? "CD ../",


? ? ? ? ? ? # 下載CUDA12.0驅(qū)動并等待8分鐘

? ? ? ? ? ? "wget --show-progress --no-check-certificate -c -b https://developer.download.nvidia.com/compute/cuda/12.0.0/local_installers/cuda_12.0.0_525.60.13_linux.run -P DRIVER/",

? ? ? ? ? ? #"echo '等待8分鐘...' && sleep 480", ?# 等待8分鐘(8分鐘 = 480秒)


? ? ? ? ? ?

? ? ? ? ? ? # 編輯/etc/profile文件并在末尾添加兩行

? ? ? ? ? ? "tee -a /etc/profile <<< 'export PATH=/usr/local/cuda/bin:$PATH'",

? ? ? ? ? ? "tee -a /etc/profile <<< 'export LD_LIBRARY_PATH=/usr/local/cuda/lib64:$LD_LIBRARY_PATH'",

? ? ? ? ? ? ? ?

? ? ? ? ? ? # 編輯/etc/modprobe.d/blacklist.conf文件并在末尾添加兩行

? ? ? ? ? ? "tee -a /etc/modprobe.d/blacklist.conf <<< 'blacklist nouveau'",

? ? ? ? ? ? "tee -a /etc/modprobe.d/blacklist.conf <<< 'options nouveau modeset=0'",

? ? ? ?

? ? ? ? ? ? # 執(zhí)行sudo update-initramfs -u命令更新內(nèi)核并返回執(zhí)行結(jié)果

? ? ? ? ? ? "update-initramfs -u",

? ? ? ? ? ? # ... 其他命令 ...

? ? ? ? ]

? ? ? ? run_as_root(ssh_client, sudo_password, root_commands)


? ? ? ? run_as_root(ssh_client, sudo_password, root_commands)


? ? ? ? # 執(zhí)行重啟命令

? ? ? ? reboot_command = "shutdown -r now"

? ? ? ? print(f"正在以root權(quán)限執(zhí)行重啟命令:{reboot_command}")

? ? ? ? exit_status, command_output = execute_command(ssh_client, reboot_command)

? ? ? ? print("命令執(zhí)行結(jié)果:")

? ? ? ? print(command_output)

? ? ? ? print(f"退出狀態(tài):{exit_status}")


? ? ? ? # 等待主機重啟完成

? ? ? ? wait_for_reboot(ssh_client, hostname, port, username, password)


? ? ? ? # 重新連接到主機

? ? ? ? ssh_client.connect(hostname, port, username, password)


? ? ? ? # 繼續(xù)以root權(quán)限執(zhí)行剩余的命令

? ? ? ? remaining_root_commands = [

? ? ? ? ? ? # 在此添加在root權(quán)限下執(zhí)行的剩余命令

? ? ? ? ? ? "lsmod | grep nouveau",

? ? ? ? ? ? # "",


? ? ? ? ? ? # 安裝Nvidia驅(qū)動

? ? ? ? ? ? "touch ./DRIVER/nvidia_install.log",

? ? ? ? ? ? "nohup sh ./DRIVER/NVIDIA-Linux-x86_64-530.41.03.run --no-opengl-files > ./DRIVER/nvidia_install.log 2>&1 &",

? ? ? ? ? ? "echo '等待NVIDIA驅(qū)動安裝完成...' && sleep 300",

? ? ? ? ? ? "nvidia-smi",


? ? ? ? ? ? # 安裝CUDA12.0

? ? ? ? ? ? "chmod +x ./DRIVER/cuda_12.0.0_525.60.13_linux.run",

? ? ? ? ? ? "touch ./DRIVER/cuda_install.log",

? ? ? ? ? ? "nohup ./DRIVER/cuda_12.0.0_525.60.13_linux.run --silent --driver --no-opengl-libs --no-drm --no-opengl-flavor > ./DRIVER/cuda_install.log 2>&1 &",

? ? ? ? ? ? "echo '等待CUDA驅(qū)動安裝完成...' && sleep 300",

? ? ? ? ? ? "source /etc/profile",

? ? ? ? ? ? "nvcc -V",

? ? ? ? ? ?

? ? ? ? ? ? # 下載gpu-burn并解壓

? ? ? ? ? ? "wget http://wili.cc/blog/entries/gpu-burn/gpu_burn-1.1.tar.gz -P GPU/",

? ? ? ? ? ? "tar -zxvf ./GPU/gpu_burn-1.1.tar.gz -C GPU/",


? ? ? ? ? ? # 編輯Makefile

? ? ? ? ? ? "sed -i 's/-arch=compute_30/-arch=compute_60/' ./GPU/Makefile",

? ? ? ? ? ? "cd GPU/",

? ? ? ? ? ? "make",


? ? ? ? ? ?

? ? ? ? ? ? # ...

? ? ? ? ]

? ? ? ? run_as_root(ssh_client, sudo_password, remaining_root_commands)


? ? except paramiko.AuthenticationException:

? ? ? ? print("認證失敗,請檢查您的憑據(jù)。")

? ? except paramiko.SSHException as ssh_ex:

? ? ? ? print(f"SSH連接錯誤:{ssh_ex}")

? ? except Exception as ex:

? ? ? ? print(f"錯誤:{ex}")

? ? finally:

? ? ? ? # 關(guān)閉SSH連接

? ? ? ? ssh_client.close()


Python下基于paramiko打造自動配置ubuntu22.04的評論 (共 條)

分享到微博請遵守國家法律
庆云县| 保德县| 汽车| 高雄市| 石景山区| 尖扎县| 江门市| 柘城县| 嘉祥县| 广宁县| 含山县| 石林| 华池县| 贵港市| 宽城| 保德县| 襄汾县| 台前县| 乐至县| 新竹市| 雷州市| 光山县| 乐安县| 黑龙江省| 洪洞县| 新邵县| 恩平市| 砀山县| 富民县| 大竹县| 绥中县| 北川| 安塞县| 瓮安县| 新田县| 依兰县| 高青县| 丘北县| 钦州市| 河曲县| 修水县|