paramiko是基于Python实现的SSHv2远程安全连接, 它支持认证及密钥的方式, 可以实现远程命令执行、文件传输、中间SSH代理功能。
安装方法
pip install paramiko --trusted-host pypi.org 或 pip install paramiko -i http://pypi.douban.com/simple (http://pypi.douban.com/simple) --trusted-host pypi.douban.com ##使用豆瓣源来安装一.SSHClient类
ssh = paramiko.SSHClient()
常用方法
1. connect #连接ssh服务器
connect(self, hostname, port=22, username=None, password=None, pkey=None, key_filename=None, timeout=None, allow_agent=True, look_for_keys=True, compress=False, sock=None, gss_auth=False, gss_kex=False, gss_deleg_creds=True, gss_host=None, banner_timeout=None, auth_timeout=None, gss_trust_dns=True, passphrase=None)
2. exec_command #在远程服务器上执行系统命令
| exec_command(self, command, bufsize=-1, timeout=None, get_pty=False, environment=None)
| Execute a command on the SSH server. A new .Channel is opened and
| the requested command is executed. The command's input and output
| streams are returned as Python file -like objects representing
| stdin, stdout, and stderr.
|
| :param str command: the command to execute
| :param int bufsize:
| interpreted the same way as by the built-in file() function in
| Python
| :param int timeout:
| set command's channel timeout. See .Channel.settimeout
| :param dict environment:
| a dict of shell environment variables, to be merged into the
| default environment that the remote command executes within.
|
| .. warning::
| Servers may silently reject some environment variables; see the
| warning in .Channel.set_environment_variable for details.
|
| :return:
| the stdin, stdout, and stderr of the executing command, as a
| 3-tuple
SFTPClient类
作为SFTP客户端对象, 根据SSH传输协议的sftp会话, 实现远程文件操作; 如:文件上传 下载 权限 状态 等操作
1. t = paramiko.Transport(('172.16.70.81',22)) #传入一个socket In [3]: help(paramiko.Transport)
Help on class Transport in module paramiko.transport:
class Transport(threading.Thread, paramiko.util.ClosingContextManager)
init (self, sock, default_window_size=2097152, default_max_packet_size=32768, gss_kex=False, gss_deleg_creds=True)
|Create a new SSH session over an existing socket, or socket-like
|object.This only creates the `.Transport` object; it doesn't begin
|the SSH session yet.Use `connect` or `start_client` to begin a client
|session, or `start_server` to begin a server session.
2. t.connect(username='root', password='meizu.com') #输入帐号密码连接
3. sftp = paramiko.SFTPClient.from_transport(t) #创建sftp
GET
In [5]: help(sftp.get) Help on method get in module paramiko.sftp_client:
get(self, remotepath, localpath, callback=None) method of paramiko.sftp_client.SFTPClient instance Copy a remote file ( remotepath) from the SFTP server to the local host as localpath. Any exception raised by operations will be passed through. This method is primarily provided as a convenience.
:param str remotepath: the remote file to copy
:param str localpath: the destination path on the local host
:param callable callback:
optional callback function (form: ``func(int, int)``) that accepts the bytes transferred so far and the total bytes to be transferred
.. versionadded:: 1.4
.. versionchanged:: 1.7.4
Added the ``callback`` param