python paramiko模組

來源:互聯網
上載者:User

標籤:paramiko模組   python學習   python基礎   

paramiko模組能實現Linux的ssh功能


1、實現一個簡單的ssh,登入到其它伺服器執行一條命令


import paramiko    #匯入模組,前提是用pip安裝過

ssh = paramiko.SSHClient()

ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())   #允許將信任的主機自動加入到host_allow 列表,此方法必須放在connect方法的前面

ssh.connect(hostname='192.168.200.102', port=22, username='root', password='666666')#串連伺服器

stdin, stdout, stderr = ssh.exec_command('hostname')#stdin標準輸入,stdout標準輸出,stderr標準錯誤

res,err = stdout.read(),stderr.read()  

result = res if res else err     #三元遠算

print(result.decode().strip())

ssh.close()


2、以上的樣本是實現在伺服器執行命令,接下來的樣本是傳遞檔案,也就是實現scp功能

import paramiko

transport = paramiko.Transport(("192.168.200.102",22))

transport.connect(username="root",password="666666")

sftp = paramiko.SFTPClient.from_transport(transport)

sftp.put("/etc/passwd","/tmp/passwd.bak")

sftp.get("/tmp/passwd.bak","/tmp/passwd.bak")

transport.close()




python paramiko模組

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.