使用python管理伺服器

來源:互聯網
上載者:User

標籤:paramiko

#!/usr/bin/env python#coding:utf-8‘‘‘Paramiko 安裝一、安裝,下載  1、下載安裝 pycrypto-2.6.1.tar.gz  (apt-get install python-dev)    解壓,進入,python setup.py build【編譯】,python setup.py install 【安裝】  ----》import Crypto  2、下載安裝 paramiko-1.10.1.tar.gz      解壓,進入,python setup.py build【編譯】,python setup.py install 【安裝】---》  import paramiko‘‘‘#指定使用者名稱密碼的方式登入import paramiko#執行個體化一個對象ssh = paramiko.SSHClient()#驗證ssh登陸時的yes/nossh.set_missing_host_key_policy(paramiko.AutoAddPolicy())#指定主機的ip 連接埠 使用者名稱 密碼ssh.connect(‘192.168.1.108‘, 22, ‘alex‘, ‘123‘)#執行命令stdin, stdout, stderr = ssh.exec_command(‘df‘)#讀取執行結果print stdout.read()#中斷連線ssh.close()#使用密鑰方式登入,私密金鑰登入‘‘‘執行python前 現在linux上做些準備工作產生密鑰ssh-keygen -t rsa遠程copy自己的公開金鑰給對方機器ssh-copy-id -i ~/ssh/id_rsa.pub [email protected]‘‘‘import paramiko#指定私密金鑰路徑private_key_path = ‘/home/tom/.ssh/id_rsa‘#取出私密金鑰key = paramiko.RSAKey.from_private_key_file(private_key_path)#下面和使用密碼登入步驟一樣ssh = paramiko.SSHClient()ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())ssh.connect(‘182.92.219.96 ‘, 22, ‘wupeiqi‘, pkey=key)stdin, stdout, stderr = ssh.exec_command(‘df‘)print stdout.read()ssh.close();


本文出自 “Python&Linux 's Blog” 部落格,請務必保留此出處http://topping.blog.51cto.com/12388701/1934468

使用python管理伺服器

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.