1. 程式人生 > >python第三方模塊之paramiko模塊

python第三方模塊之paramiko模塊

con comm get res str 文件 stdin path color

目錄:

  • 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()
View Code

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()
View Code 技術分享
 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()
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 #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=123456):
 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()
View Code

python第三方模塊之paramiko模塊