ganzl

(瓜)

导航

paramiko模块

安装:

# pycrypto,由于 paramiko 模块内部依赖pycrypto,所以先下载安装pycrypto
(1)
wget http://ftp.dlitz.net/pub/dlitz/crypto/pycrypto/pycrypto-2.6.tar.gz
tar -zxvf pycrypto-2.6.tar.gz
cd pycrypto-2.6/
python setup.py build && python setup.py install

(可能报错)
python>> import Crypto
这是因为缺少python-dev的软件包,所:yum -y install python-devel

(编译时报错:error: command 'gcc' failed with exit status 1;这是因为缺少python-dev的软件包,所yum -y install python-devel)
(2)
get http://www.lag.net/paramiko/download/paramiko-1.7.7.1.tar.gz
tar xvzf paramiko-1.7.7.1.tar.gz
cd paramiko-1.7.7.1/
python setup.py build && python setup.py install

# 进入python环境,导入paramiko检查是否安装成功

1.paramiko执行命令(用户名密码方式),(分为用SSHClient对象与Transport对象两种)

#!/bin/usr/env python
#-*- coding:utf-8 -*-
import paramiko

ssh = paramiko.SSHClient()# 创建SSH对象

ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())# 允许连接不在know_hosts文件中的主机
ssh.connect(hostname='10.1.1.10', port=22, username='ganzl', password='123456')# 连接服务器
stdin, stdout, stderr = ssh.exec_command('df')# 执行命令
result = stdout.read()# 获取命令结果
print result

ssh.close()  # 关闭连接
View Code
#!/bin/usr/env python
#-*- coding:utf-8 -*-

import paramiko

transport = paramiko.Transport(('10.1.1.10', 22))
transport.connect(username='ganzl', password='123456')

ssh = paramiko.SSHClient()
ssh._transport = transport

stdin, stdout, stderr = ssh.exec_command('df')
print stdout.read()

transport.close()
View Code

2.paramiko执行命令(公钥方式),(分为用SSHClient对象与Transport对象两种)

#-*- coding:utf-8 -*-
import paramiko
 
private_key = paramiko.RSAKey.from_private_key_file('/home/ganzl/.ssh/id_rsa')
 
ssh = paramiko.SSHClient()# 创建SSH对象
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())# 允许连接不在know_hosts文件中的主机
ssh.connect(hostname='10.1.1.10', port=22, username='ganzl',pkey=private_key)# 连接服务器
stdin, stdout, stderr = ssh.exec_command('df')# 执行命令
result = stdout.read()# 获取命令结果
print result
 
ssh.close()# 关闭连接
View Code
#!/bin/usr/env python
#-*- coding:utf-8 -*-

import paramiko
private_key = paramiko.RSAKey.from_private_key_file('/home/ganzl/.ssh/id_rsa')
transport = paramiko.Transport(('10.1.1.10', 22))
transport.connect(username='ganzl', pkey=private_key)

ssh = paramiko.SSHClient()
ssh._transport = transport

stdin, stdout, stderr = ssh.exec_command('df')
print stdout
print stdin
print stderr

transport.close()

这个在执行结果上如下:
-----------------------
<paramiko.ChannelFile from <paramiko.Channel 1 (open) window=2097152 -> <paramiko.Transport at 0x2407eb10L (cipher aes128-ctr, 128 bits) (active; 1 open channel(s))>>>
<paramiko.ChannelFile from <paramiko.Channel 1 (open) window=2097152 -> <paramiko.Transport at 0x2407eb10L (cipher aes128-ctr, 128 bits) (active; 1 open channel(s))>>>
<paramiko.ChannelFile from <paramiko.Channel 1 (open) window=2097152 -> <paramiko.Transport at 0x2407eb10L (cipher aes128-ctr, 128 bits) (active; 1 open channel(s))>>>

这个方式的执行结果有时间了,再整明白下。
View Code

3.FTP进行上传下载文件

#!/bin/usr/env python
#-*- coding:utf-8 -*-
import paramiko

private_key = paramiko.RSAKey.from_private_key_file('/home/ganzl/.ssh/id_rsa')
transport = paramiko.Transport(('10.1.1.10', 22))
transport.connect(username='ganzl', pkey=private_key)

sftp = paramiko.SFTPClient.from_transport(transport)#然后创建SFTPClient并基于transport连接,把他俩做个绑定
sftp.put('/home/ganzl/bak/beifen-lianlian.sh', '/home/ganzl/bak/beifen-lianlian.sh') 
sftp.get('/home/ganzl/bak/test.sh', '/home/ganzl/bak/test.sh')  
transport.close()
View Code
#-*- coding:utf-8 -*-
import paramiko
 
transport = paramiko.Transport(('10.1.1.10',22))
transport.connect(username='ganzl',password='123456')
 
sftp = paramiko.SFTPClient.from_transport(transport)

sftp.put('/home/ganzl/bak/beifen-lianlian.sh', '/home/ganzl/bak/beifen-lianlian.sh') 
sftp.get('/home/ganzl/bak/test.sh', '/home/ganzl/bak/test.sh')  
 
transport.close()
View Code

为什么要搞两种方式,应该是SSHClient对象不支持文件的上次下载,而Transport这个类是对SSHClient类的封装。

写代码的时候,尽量放到一起。

 用面向对象的方式将登入,执行命令,上次下载放到不同的方法里:

#!/sur/bin/env python
# -*- coding:utf-8 -*-
__author__ = 'ganzl'
import uuid
import paramiko

class properties(object):
    def __init__(self):
        self.host = '10.1.1.10'
        self.port = 22
        self.username = 'ganzl'
        self.password = '123456'

    def creat_file(self):
        str_file = str(uuid.uuid4())
        with open(str_file,'w') as f:
            f.write('写入配置文件内容,或者拿到配置文件')
        return  str_file

    def connect(self):
        transport = paramiko.Transport((self.host,self.port))
        transport.connect(username=self.username,password=self.password)
        self.conn = transport

    def close(self):
        self.conn.close()

    def upload(self):
        file_name = self.creat_file()
        sftp = paramiko.SFTPClient.from_transport(self.conn)
        sftp.put(file_name, '/home/lbsweb/bak/beifen-lianlian.sh')

    def download(self):
        sftp = paramiko.SFTPClient.from_transport(self.conn)
        sftp.get('/home/lbsweb/bak/test.sh', '/home/ganzl/bak/test.sh')

    def my_cmd(self):
        ssh = paramiko.SSHClient()
        ssh._transport = self.conn  #注意,这里是将封装的transport放到SSHClient中去,执行命令
        stdin, stdout, stderr = ssh.exec_command('df')# 执行命令
        result = stdout.read()
        print result

    def run(self):
        self.connect()
        self.my_cmd()
        #self.upload()   这里测试只执行命令。
        self.close()
        


pro = properties()
pro.run()
        

 

posted on 2016-01-26 11:15  ganzl  阅读(413)  评论(0编辑  收藏  举报