python 遠程執行命令、上傳、下載舉例

來源:互聯網
上載者:User

標籤:

 

使用python中的 paramiko 實現遠程操作,需要安裝 paramiko 模組。

# vi pssh.py

#!/usr/bin/python#coding=utf-8‘‘‘Created on Apr 13, 2016@author: root‘‘‘import paramikoclass PSSH():            def __init__(self,ip,sort,username,password):                self.ip = ip        self.sort = sort        self.username = username        self.passwrod = password            def exec_cmd(self,cmd):        ssh = paramiko.SSHClient()        ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())        #ssh.connect(hostname, port, username, password, pkey, key_filename, timeout, allow_agent, look_for_keys, compress, sock)        ssh.connect(self.ip,self.sort,self.username,self.passwrod)        stdin,stdout,stderr = ssh.exec_command(cmd)        print stdout.readlines()        ssh.close()        def upfile(self,localpath,remotepath):        ‘‘‘        ‘‘‘        t = paramiko.Transport((self.ip,self.sort))        t.connect(username= self.username,password=self.passwrod)        sftp = paramiko.SFTPClient.from_transport(t)        sftp.put(localpath,remotepath)        t.close()                    def downfile(self,remotepath,localpath):        ‘‘‘        ‘‘‘        t = paramiko.Transport((self.ip,self.sort))        t.connect(username= self.username,password=self.passwrod)        sftp = paramiko.SFTPClient.from_transport(t)        sftp.get(remotepath,localpath)        t.close()                if __name__ == ‘__main__‘:    pssh = PSSH("192.168.1.77",22,"oracle","oracle");   # pssh.exec_cmd("cat /tmp/a.txt")    #pssh.upfile("/tmp/1.txt", "/tmp/1.txt")    pssh.downfile("/tmp/a.txt", "/tmp/a.txt")        print ‘------success!-----‘    

測試下載功能

# chmod +x pssh.py # ./pssh.py ------success!-----# ll | grep a.txt -rw-r--r--. 1 root   root          4 Apr 13 10:16 a.txt

 

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.