python paramiko ssh linux服务器 提示鉴权失败
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了python paramiko ssh linux服务器 提示鉴权失败相关的知识,希望对你有一定的参考价值。
bash shell ssh到目标服务器正常。但是使用python 的paramiko 就提示paramiko.ssh_exception.AuthenticationException: Authentication failed.
试验了很多方法,都不行 用户名 密码是正确的,请协助
**/path/anaconda2/bin/python**
1
,这样改之后我以为万事大吉了,没想到又出现ImportError: libcudnn.so.6: cannot open shared object file: No such file or directory这个问题,这里是由于cuda没有加入到自己的环境中,这里我在~/.bashrc中加入了如下代码
export LD_LIBRARY_PATH="$LD_LIBRARY_PATH:/usr/local/cuda/lib64:/usr/local/cuda/extras/CUPTI/lib64"
export CUDA_HOME=/usr/local/cuda
1
2
! 参考技术A 在ssh 连接参数中新增allow_agent=False, look_for_keys=False即可用解决问题
ssh.connect(hostname=ip, username=user, password=passwd, allow_agent=False, look_for_keys=False)
python ssh之paramiko模块使用
1.安装:
sudo pip install paramiko
2.连接到linux服务器
方法一:
#paramiko.util.log_to_file(‘ssh.log‘) #写日志文件 client = paramiko.SSHClient() client.set_missing_host_key_policy(paramiko.AutoAddPolicy()) #允许连接不在~/.ssh/known_hosts文件中的主机 client.connect(‘ip‘,22, ‘username‘,‘password‘) #指定ip地址,端口,用户名和密码进行连接
方法二:
transport = paramiko.Transport((‘ip‘, port)) #获取连接 transport.connect(username=‘username‘,password=‘password‘) ssh = paramiko.SSHClient() ssh._transport = transport
3.用transport实现上传下载以及命令的执行
# coding=utf-8 import paramiko class SSHConnection(object): def __init__(self, host, port, username, password): self._host = host self._port = port self._username = username self._password = password self._transport = None self._sftp = None self._client = None self._connect() # 建立连接 def _connect(self): transport = paramiko.Transport((self._host, self._port)) transport.connect(username=self._username, password=self._password) self._transport = transport #下载 def download(self, remotepath, localpath): if self._sftp is None: self._sftp = paramiko.SFTPClient.from_transport(self._transport) self._sftp.get(remotepath, localpath) #上传 def put(self, localpath, remotepath): if self._sftp is None: self._sftp = paramiko.SFTPClient.from_transport(self._transport) self._sftp.put(localpath, remotepath) #执行命令 def exec_command(self, command): if self._client is None: self._client = paramiko.SSHClient() self._client._transport = self._transport stdin, stdout, stderr = self._client.exec_command(command) data = stdout.read() if len(data) > 0: print data.strip() #打印正确结果 return data err = stderr.read() if len(err) > 0: print err.strip() #输出错误结果 return err def close(self): if self._transport: self._transport.close() if self._client: self._client.close() if __name__ == "__main__": conn = SSHConnection(‘192.168.87.200‘, 22, ‘username‘, ‘password‘) localpath = ‘hello.txt‘ remotepath = ‘/home/hupeng/WorkSpace/Python/test/hello.txt‘ print ‘downlaod start‘ conn.download(remotepath, localpath) print ‘download end‘ print ‘put begin‘ conn.put(localpath, remotepath) print ‘put end‘ conn.exec_command(‘whoami‘) conn.exec_command(‘cd WorkSpace/Python/test;pwd‘) #cd需要特别处理 conn.exec_command(‘pwd‘) conn.exec_command(‘tree WorkSpace/Python/test‘) conn.exec_command(‘ls -l‘) conn.exec_command(‘echo "hello python" > python.txt‘) conn.exec_command(‘ls hello‘) #显示错误信息 conn.close()
以上是关于python paramiko ssh linux服务器 提示鉴权失败的主要内容,如果未能解决你的问题,请参考以下文章
python paramiko ssh linux服务器 提示鉴权失败
python中连接linux好用的模块paramiko(附带案例)