zoukankan      html  css  js  c++  java
  • python 远程执行命令、上传、下载举例

    使用python中的 paramiko 实现远程操作,需要安装 paramiko 模块。

    # vi pssh.py

    #!/usr/bin/python
    #coding=utf-8
    
    '''
    Created on Apr 13, 2016
    
    @author: root
    '''
    
    import paramiko
    
    
    class 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
  • 相关阅读:
    json基础
    css语法以及css选择器
    HTML语义化标签
    CSS插入的四种方式
    Hibernate主键生成策略及选择
    HIbernate处理数据更新丢失
    数据库的四大特性以及事务的隔离级别
    MD5加密
    redis主从复制
    redis的持久化方案
  • 原文地址:https://www.cnblogs.com/perfei/p/5386012.html
Copyright © 2011-2022 走看看