Paramiko模組安裝與使用
Paramiko介紹
Paramiko遵循SSH2協議的python類庫,支援以加密和認證的方式,可以進行遠端伺服器的連線。其中ansible也是通過Paramiko來進行ssh連線的。用它做遠端管理時僅需要在本地上安裝相應的軟體(python以及PyCrypto),對遠端伺服器沒有配置要求,對於連線多臺伺服器,進行復雜的連線操作特別有方便。
安裝
Paramiko安裝依賴於pycrypto、ecdsa模組
- shell> wget https://pypi.python.org/packages/source/e/ecdsa/ecdsa-0.13.tar.gz
- shell> tar zxvf ecdsa-0.13.tar.gz
- shell> cd ecdsa-0.13
- shell> python setup.py install
- shell> wget https://pypi.python.org/packages/source/p/pycrypto/pycrypto-2.6.1.tar.gz
- shell> tar zxvf pycrypto-2.6.1.tar.gz
- shell> cd pycrypto-2.6.1
- shell> python setup.py install
- shell> wget https://pypi.python.org/packages/source/p/paramiko/paramiko-1.16.0.tar.gz
- shell> unzip paramiko-1.16.zip
- shell> cd paramiko-1.16
- shell> python setup.py install
Paramiko使用示例
執行遠端命令
- #!/usr/bin/python
- import paramiko
- ssh = paramiko.SSHClient()
- # 允許連線不在known_hosts檔案列表中的主機
- ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
- ssh.connect("遠端IP,埠,"使用者", "密碼")
- stdin, stdout, stderr = ssh.exec_command("ls -l /tmp")
- print(stdout.readlines())
- ssh.close()
上傳檔案到遠端伺服器
- #!/usr/bin/python
- import paramiko
- tp = paramiko.Transport("遠端IP",埠)
- tp.connect( username = "使用者", password = "密碼")
- sftp = paramiko.SFTPClient.from_transport(tp)
- remotepath = '/tmp/Python-2.7.9.tgz'
- localpath = '/tmp/Python-2.7.9.tgz'
- sftp.put(localpath, remotepath)
- tp.close()
從遠端伺服器下載檔案
- #!/usr/bin/python
- import paramiko
- tp = paramiko.Transport("遠端IP",埠)
- tp.connect( username = "使用者", password = "密碼")
- sftp = paramiko.SFTPClient.from_transport(tp)
- remotepath = '/tmp/Python-2.7.9.tgz'
- localpath = '/tmp/Python-2.7.9.tgz'
- sftp.get(remotepath, localpath)
- tp.close()
來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/29733787/viewspace-2074776/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- Python paramiko模組的安裝與使用Python
- Python筆記之paramiko模組安裝和使用示例Python筆記
- Paramiko模組簡單使用
- python常用模組之paramiko與sshPython
- Python中paramiko 模組的用法Python
- paramiko 2.4.1原始碼安裝原始碼
- 用paramiko模組寫的發版機
- python 模組安裝Python
- PyMySQL模組安裝MySql
- Python模組安裝Python
- python 3呼叫paramiko模組報錯AttributeError: modulePythonError
- php 安裝zip模組PHP
- Python安裝selenium模組Python
- windows 安裝 Pillow 模組Windows
- 【PHP】Mcrypt 擴充套件模組安裝及使用PHP套件
- python 安裝模組的方法Python
- pip 命令安裝模組包
- python openssl模組如何安裝?Python
- pip進行模組安裝
- Python 庫/模組的pip安裝和IPython的使用Python
- Python模組、第三方模組安裝、模組匯入教程Python
- 深入淺析Nodejs的安裝方法與模組系統NodeJS
- 模組的釋出和安裝
- nodejs檢查已安裝模組NodeJS
- Mac 編譯安裝 PHPRedis 模組Mac編譯PHPRedis
- Python安裝模組有哪些方法?Python
- 怎麼用anaconda安裝模組?
- PHP檔案型別檢查及fileinfo模組安裝使用PHP型別
- 強制解除安裝報錯模組
- Nginx安裝nginx-rtmp-module模組Nginx
- 手撕Vuex-安裝模組方法Vue
- FreeSwitch一些模組的安裝
- python模組安裝目錄在哪裡Python
- 怎樣安裝python的GPIO模組Python
- 安裝fbprophet模組詳細步驟
- Centos下安裝FastDFS及Nginx模組CentOSASTNginx
- Python:檢視已安裝模組 和 檢視可匯入模組Python
- python(pip)包/模組:如何離線安裝?Python
- Python如何檢視安裝了哪些模組?Python