tianyou.zhu

博客园 首页 新随笔 联系 订阅 管理

目录:

  • paramiko模块介绍
  • paramiko模块安装
  • paramiko模块使用

一、paramiko模块介绍

paramiko是一个用于做远程控制的模块,使用该模块可以对远程服务器进行命令或文件操作,值得一说的是,fabric和ansible内部的远程管理就是使用的paramiko来现实。它包含两个常用模块,SSHClient()模块,SFTPClient()模块。

二、paramiko模块安装

pycrypto,由于 paramiko 模块内部依赖pycrypto,所以先下载安装pycrypto
pip3 install pycrypto
pip3 install paramiko

三、paramiko模块使用

1、执行远程命令SSHClient()模块

 1 1 #!/usr/bin/python
 2  2 
 3  3 import paramiko
 4  4 
 5  5  
 6  6 
 7  7 ssh = paramiko.SSHClient()
 8  8 
 9  9 ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
10 10 
11 11 ssh.connect("某IP地址",22,"用户名", "口令")
12 12 
13 13 stdin, stdout, stderr = ssh.exec_command("你的命令")
14 14 
15 15 print stdout.readlines()
16 16 
17 17 ssh.close()
View Code
复制代码

2、执行远程命令SSHClient()模块之密钥登录

 1  1 import paramiko
 2  2 
 3  3 private_key_path = '/home/auto/.ssh/id_rsa'
 4  4 key = paramiko.RSAKey.from_private_key_file(private_key_path)
 5  5 
 6  6 ssh = paramiko.SSHClient()
 7  7 ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
 8  8 ssh.connect('主机名 ', 端口, '用户名', key)
 9  9 
10 10 stdin, stdout, stderr = ssh.exec_command('df')
11 11 print stdout.read()
12 12 ssh.close()
View Code
 1 1 import paramiko
 2  2 
 3  3 pravie_key_path = '/home/auto/.ssh/id_rsa'
 4  4 key = paramiko.RSAKey.from_private_key_file(pravie_key_path)
 5  5 
 6  6 t = paramiko.Transport(('182.92.219.86',22))
 7  7 t.connect(username='wupeiqi',pkey=key)
 8  8 
 9  9 sftp = paramiko.SFTPClient.from_transport(t)
10 10 sftp.put('/tmp/test3.py','/tmp/test3.py')
11 11 
12 12 t.close()
13 13 
14 14 import paramiko
15 15 
16 16 pravie_key_path = '/home/auto/.ssh/id_rsa'
17 17 key = paramiko.RSAKey.from_private_key_file(pravie_key_path)
18 18 
19 19 t = paramiko.Transport(('182.92.219.86',22))
20 20 t.connect(username='wupeiqi',pkey=key)
21 21 
22 22 sftp = paramiko.SFTPClient.from_transport(t)
23 23 sftp.get('/tmp/test3.py','/tmp/test4.py')
24 24 
25 25 t.close()
View Code

3、上传文件到远程SFTPClient()模块

1 #!/usr/bin/python
 2 
 3 import paramiko
 4 
 5  
 6 
 7 t = paramiko.Transport(("某IP地址",22))
 8 
 9 t.connect(username = "用户名", password = "口令")
10 
11 sftp = paramiko.SFTPClient.from_transport(t)
12 
13 remotepath='/tmp/test.txt'
14 
15 localpath='/tmp/test.txt'
16 
17 sftp.put(localpath,remotepath)
18 
19 t.close()
View Code

4、远程下载到本地SFTPClient()模块

 1 import paramiko
 2 
 3  
 4 
 5 t = paramiko.Transport(("某IP地址",22))
 6 
 7 t.connect(username = "用户名", password = "口令")
 8 
 9 sftp = paramiko.SFTPClient.from_transport(t)
10 
11 remotepath='/tmp/test.txt'
12 
13 localpath='/tmp/test.txt'
14 
15 sftp.get(remotepath, localpath)
16 
17 t.close()

复制代码
View Code

5、整合使用:

 1  1 #coding:utf-8
 2  2 import paramiko
 3  3 import uuid
 4  4 
 5  5 class SSHConnection(object):
 6  6 
 7  7     def __init__(self, host='192.168.2.103', port=22, username='root',pwd='123456'):
 8  8         self.host = host
 9  9         self.port = port
10 10         self.username = username
11 11         self.pwd = pwd
12 12         self.__k = None
13 13 
14 14     def connect(self):
15 15         transport = paramiko.Transport((self.host,self.port))
16 16         transport.connect(username=self.username,password=self.pwd)
17 17         self.__transport = transport
18 18 
19 19     def close(self):
20 20         self.__transport.close()
21 21 
22 22     def upload(self,local_path,target_path):
23 23         # 连接,上传
24 24         # file_name = self.create_file()
25 25         sftp = paramiko.SFTPClient.from_transport(self.__transport)
26 26         # 将location.py 上传至服务器 /tmp/test.py
27 27         sftp.put(local_path, target_path)
28 28 
29 29     def download(self,remote_path,local_path):
30 30         sftp = paramiko.SFTPClient.from_transport(self.__transport)
31 31         sftp.get(remote_path,local_path)
32 32 
33 33     def cmd(self, command):
34 34         ssh = paramiko.SSHClient()
35 35         ssh._transport = self.__transport
36 36         # 执行命令
37 37         stdin, stdout, stderr = ssh.exec_command(command)
38 38         # 获取命令结果
39 39         result = stdout.read()
40 40         print (str(result,encoding='utf-8'))
41 41         return result
42 42 
43 43 ssh = SSHConnection()
44 44 ssh.connect()
45 45 ssh.cmd("ls")
46 46 ssh.upload('s1.py','/tmp/ks77.py')
47 47 ssh.download('/tmp/test.py','kkkk',)
48 48 ssh.cmd("df")
49 49 ssh.close()
50 
51 复制代码
View Code

 

posted on 2017-06-29 14:51  tianyou.zhu  阅读(220)  评论(0编辑  收藏  举报