paramiko模块

paramiko模块是作为ssh client 去连接服务端,在服务端上执行命令,获取返回结果,或者执行上传下载任务
 
  1. paramiko的安装
paramiko不是python的标准库,需要另外安装
第一种安装方式:yum install -y python-paramiko         //安装的版本较低

第二种安装方式:pip install paramiko

 

2. paramikoSSHClient() 这个类的用法

In [1]: import paramiko

In [2]: help(paramiko.SSHClient)
Help on class SSHClient in module paramiko:

class SSHClient(__builtin__.object)
 |  A high-level representation of a session with an SSH server.  This class
 |  wraps L{Transport}, L{Channel}, and L{SFTPClient} to take care of most
 |  aspects of authenticating and opening channels.  A typical use case is::
 | 
 |      client = SSHClient()
 |      client.load_system_host_keys()
 |      client.connect('ssh.example.com')
 |      stdin, stdout, stderr = client.exec_command('ls -l')
 |     
 |  You may pass in explicit overrides for authentication and server host key
 |  checking.  The default mechanism is to try to use local key files or an
 |  SSH agent (if one is running).
 | 
 |  @since: 1.6
 | 
 |  Methods defined here:
 | 
 |  __init__(self)
 |      Create a new SSHClient.                 //创建一个ssh client object
 | 
 |
 |.......这个类有很多方法,下面三个例子会说明他们的用法

 

 

2.1 用密码登陆服务端

In [5]: client = paramiko.SSHClient()                 //创建ssh client object



In [6]: client
Out[6]: <paramiko.SSHClient at 0x2bf9a10>

In [7]: help(client.load_host_keys)
Help on method load_host_keys in module paramiko.client:

load_host_keys(self, filename) method of paramiko.SSHClient instance
    Load host keys from a local host-key file           

In [9]: host_file = '/root/.ssh/known_hosts'           //默认的host_keys就是这个,如果这台主机没有登陆过任何其他主机,这个文件是没有的

In [10]: client.load_host_keys(host_file)              //加载host_keys,用于客户端和服务端加密通信

In [12]: client.set_missing_host_key_policy(paramiko.AutoAddPolicy())       //如果没有known_hosts这个文件,让客户端自己选择加密策略



In [13]: help(client.connect)
Help on method connect in module paramiko.client:

connect(self, hostname, port=22, username=None, password=None, pkey=None, key_filename=None, timeout=None, allow_agent=True, look_for_keys=True)

In [21]: client.connect(hostname='192.168.123.101', username='root', password='123456')            //连接服务端



In [22]: help(client.exec_command)
Help on method exec_command in module paramiko.client:

exec_command(self, command, bufsize=-1) method of paramiko.SSHClient instance           //在服务端执行命令
    Execute a command on the SSH server.  A new L{Channel} is opened and
    the requested command is executed.  The command's input and output
    streams are returned as python C{file}-like objects representing                   //返回三个对象,分别是标准输入,标准输出,标准错误输出
    stdin, stdout, and stderr                                                          //需要定义三个变量接收对象

In [24]: stdin, stdout, stderr = client.exec_command('hostname')

In [26]: stdout.read()
Out[26]: 'test\n'


In [28]: client.close()                      //关闭连接

 

 

2.2 用密钥登陆服务端

In [1]: import paramiko

In [3]: host_file = '/root/.ssh/known_hosts'

In [4]: client.load_host_keys(host_file)            //注意:如果known_hosts里没有该服务端对应的记录,不能使用这个方法,需要用自动选择策略那个方法



In [6]: help(paramiko.RSAKey.from_private_key_file)
Help on method from_private_key_file in module paramiko.pkey:

from_private_key_file(cls, filename, password=None) method of __builtin__.type instance      //filename就是私钥,password是私钥的密码,默认私钥没有密码
    Create a key object by reading a private key file              //读取私钥并生成一个key对象

In [3]: key_file = '/root/.ssh/id_rsa'

In [5]: key = paramiko.RSAKey.from_private_key_file(key_file)          //如果是其他类型的密钥,就用其他类型的方法,这个是RSAkey的

In [9]: client.connect(hostname='192.168.123.101', username='root', pkey=key)




In [10]: stdin, stdout, stderr = client.exec_command('whoami')

In [11]: stdout.read()
Out[11]: 'root\n'

In [12]: client.close()

 

2.3 用带密码的密钥登陆服务端

In [1]: import paramiko

In [2]: client = paramiko.SSHClient()

In [3]: host_file = '/root/.ssh/known_hosts'

In [4]: client.load_host_keys(host_file)

In [5]: key_file = '/root/.ssh/id_rsa'

In [6]: key = paramiko.RSAKey.from_private_key_file(key_file, password='123456')        //这个密码是密钥的密码

In [7]: client.connect(hostname='192.168.123.101', username='root', pkey=key)

In [8]: stdin, stdout, stderr = client.exec_command('uptime')

In [9]: stdout.read()
Out[9]: ' 11:53:24 up  1:18,  1 user,  load average: 0.00, 0.01, 0.04\n'

In [11]: client.close()

 

实例应用

In [1]: import paramiko

In [2]: client = paramiko.SSHClient()

In [3]: host_file = '/root/.ssh/known_hosts'

In [4]: client.load_host_keys(host_file)

In [5]: key_file = '/root/.ssh/id_rsa'

In [6]: key = paramiko.RSAKey.from_private_key_file(key_file, password='123456')



In [16]: paramiko.Transport(('192.168.123.101', 22))
Out[16]: <paramiko.Transport at 0x210fc90L (unconnected)>

In [17]: s = paramiko.Transport(('192.168.123.101', 22))            //实例化一个对象



In [18]: help(s.connect)
Help on method connect in module paramiko.transport:

connect(self, hostkey=None, username='', password=None, pkey=None) method of paramiko.Transport instance

In [8]: s.connect(username='root', pkey=key)         //连接到服务端



In [9]: help(paramiko.SFTPClient.from_transport)
Help on method from_transport in module paramiko.sftp_client:

from_transport(cls, t) method of __builtin__.type instance
    Create an SFTP client channel from an open L{Transport}.

In [10]: sftp = paramiko.SFTPClient.from_transport(s)                //创建sftp客户端信道,接受一个object作为参数,s是paramiko.Transport()实例化的一个对象




In [11]: help(sftp.get)
Help on method get in module paramiko.sftp_client:

get(self, remotepath, localpath, callback=None) method of paramiko.SFTPClient instance

In [13]: sftp.get('/etc/passwd', '/tmp/a.txt')              //下载文件



In [14]: help(sftp.put)
Help on method put in module paramiko.sftp_client:

put(self, localpath, remotepath, callback=None) method of paramiko.SFTPClient instance

In [15]: sftp.put('/etc/hosts', '/tmp/b.txt')         //上传文件
Out[15]: <SFTPAttributes: [ size=183 uid=0 gid=0 mode=0100644 atime=1505017165 mtime=1505017165 ]>

 

转载于:https://www.cnblogs.com/tobeone/p/8191234.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值