import subprocess from IPython.display import clear_output def run_command(command): """Run a shell command and handle errors.""" try: subprocess.run(command, check=True, shell=True) except subprocess.CalledProcessError as e: print(f"An error occurred: {e}") # 更新包列表 run_command("apt update") # 安装 OpenSSH 服务器和 Vim 编辑器 run_command("apt install openssh-server -y") run_command("apt-get install vim -y") # 修改 SSH 配置以允许密码认证 config_client_cmd = "sudo sed -i '/PasswordAuthentication/s/^#//g' /etc/ssh/ssh_config" run_command(config_client_cmd) # 修改 SSH 配置以允许 root 登录 config_server_cmd = "sudo sed -i '/PermitRootLogin prohibit-password/s/prohibit-password/yes/' /etc/ssh/sshd_config" run_command(config_server_cmd) # 重启 SSH 服务 run_command("systemctl restart ssh") # 设置 root 用户密码 password_change_cmd = "echo 'root:qilan123' | sudo chpasswd" run_command(password_change_cmd) # 确保 PermitRootLogin 设置正确并重启服务 os.system("echo 'PermitRootLogin yes' | sudo tee -a /etc/ssh/sshd_config") run_command("service ssh restart") # 清理环境变量(如果需要) clear_output()