# -*- 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()
python代码:本地压缩文件,然后连接远端linux,上传文件,解压,安装
该博客介绍了一个使用Python编写的脚本,该脚本能够将本地文件压缩,通过SSH连接到远程Linux服务器,上传压缩文件,然后在远程服务器上解压并执行安装操作。脚本涉及paramiko库进行SSH连接,tarfile库进行文件压缩和解压,以及一些基本的Linux命令操作。
摘要由CSDN通过智能技术生成