paramiko简介

paramiko是用Python语言写的一个模块,遵循SSH2协议,支持以加密和认证的方shell

 

式进行远程服务器的链接。服务器

安装paramikosession

pip install paramikossh

遇到的错误加密

提示:spa

distutils.errors.DistutilsError: Setup script exited with error: ip

 

command 'gcc' failed with exit status 1ci

解决方法:get

yum install -y libffi-develit

再次安装时,先uninstall

pip uninstall paramiko

然后

pip install paramiko

import paramiko

 

类的实例化

help(paramiko.Transport)查看用法

。。。

__init__(self, sock, default_window_size=2097152, 

 

default_max_packet_size=32768, gss_kex=False, gss_deleg_creds=True)

。。。

注意是一个sock,须要使用(host, port)

ssh = paramiko.Transport((host, port))

 

链接远程服务器

ssh.connect(username='xxx', password='xxx')

 

经过秘钥链接远程服务器

key = paramiko.RSAKey.from_private_key_file(key_file)

ssh.connect(username='xxx', pkey=key, timeout=n)

 

开启一个伪终端

console=ssh.open_session()

 

在终端执行shell命令,执行完后会自动console.close()关闭伪终端

console.exec_command('shell_command')

 

上传文件到远程服务器

sftp = paramiko.SFTPClient.from_transport(ssh)

sftp.put(source_file, dst_file)

 

远程服务器下载文件到本地

sftp = paramiko.SFTPClient.from_transport(ssh)

sftp.get(dst_file, source_file)

 

关闭ssh远程链接

ssh.close()

相关文章
相关标签/搜索