python代码:本地压缩文件,然后连接远端linux,上传文件,解压,安装

# -*- coding: utf-8 -*-

import time
import tarfile
import paramiko
import commands

import os

class master():
    """
    Compress file and use SSH to push the .tar file to remote,
    then uncompress and setup.

    """
      
    def __init__(self, object_file_path, compress_file_name, ip, port, username, password):
        self.object_file_path = object_file_path
        self.compress_file_name = compress_file_name
        self.ip = ip
        self.port = port
        self.username = username
        self.password = password
        
        # Initiate SSH connection       
        try:
            ssh = paramiko.SSHClient()
            ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
            ssh.connect(self.ip, self.port, self.username, self.password, timeout=10)
            self.ssh_handler = ssh
                        
        except Exception, e:
            print e
            print'ssh error'
        
    def set_file_property(self, tar_file_name, remote_object_file_path, collector_file_name):
        # set file property
        try:
            self.tar_file_name = tar_file_name
            self.remote_object_file_path = remote_object_file_path
            self.tar_file_full_path = self.remote_object_file_path + self.tar_file_name
            self.collector_file_name = collector_file_name
            
        except Exception, e:
            print e
            print'set property error'
        
                        
    def execute_command(self, cmd, sync=True):
        try:
            if sync:
                i, o , e = self.ssh_handler.exec_command(cmd)
                res = e.readlines()
    
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值