SSHClient
import paramiko
# 创建ssh对象
ssh = paramiko.SSHClient()
# 允许连接不在.ssh/know_hosts文件中的主机
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
# 连接服务器
ssh.connect(hostname='192.168.229.130', port=22, username='jason', password='123')
# 执行命令
stdin, stdout, stderr = ssh.exec_command('df')
# 获取命令结果
res, err = stdout.read(), stderr.read()
result = res if res else err # 三元运算
ssh.close()
print(result.decode())
SFTP
import paramiko
transport = paramiko.Transport('192.168.229.131', 22)
transport.connect(username='jason', password='123')
sftp = paramiko.SFTPClient.from_transport(transport)
sftp.get("test.txt", "tmp/test131.txt")