使用ssh和用户名,密码,登录主机。请使用subprocess写出Python代码。不要使用sshpass
时间: 2023-04-07 19:03:08 浏览: 86
用Python写一段用户登录的程序代码
5星 · 资源好评率100%
可以的,以下是Python代码:
```
import subprocess
host = "example.com"
username = "your_username"
password = "your_password"
command = ["ssh", f"{username}@{host}"]
process = subprocess.Popen(command, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
# 输入密码
out, err = process.communicate(input=f"{password}\n".encode())
print(out.decode())
```
阅读全文