from paramiko import *
hostname = 'ip'
port = 22
username = 'root'
password = 'xxxx'
util.log_to_file('paramiko.log')
s = SSHClient()
s.load_system_host_keys()
s.connect(hostname, port, username, password)
stdin,stdout,stderr = s.exec_command('df -h')
print(stdout.read())
s.close()
报错:paramiko.ssh_exception.SSHException: Server 'ip' not found in known_hosts
原因:/root/.ssh/known_hosts文件里面没有相关的ssh 登录秘钥
解决:
ssh 远程连接到对应的机器,如果出现known_hosts2 ,那么将 known_hosts2 中的 数据导入到 known_hosts 中
2.免密码登录