目录:

  • 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 #!/usr/bin/python
2
3 import paramiko
4
5
6
7 ssh = paramiko.SSHClient()
8
9 ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
10
11 ssh.connect("某IP地址",22,"用户名", "口令")
12
13 stdin, stdout, stderr = ssh.exec_command("你的命令")
14
15 print stdout.readlines()
16
17 ssh.close()
python_paramiko-LMLPHP

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

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

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()

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() 复制代码

5、整合使用:

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