zoukankan      html  css  js  c++  java
  • 使用paramiko模块在服务器上面远程操作编译环境

    项目需求:在服务器上面需要调用补丁对应的编译环境(另外一台机器),执行编译打包上传至git等操作。

    具体实现:使用paramiko模块中的执行命令,获取文件,上传文件,判断文件是否存在等方法。

    官方文档:http://docs.paramiko.org/en/2.7/api/sftp.html

    类似的模块:netmiko 文档链接 https://ktbyers.github.io/netmiko/#api-documentation

    # 例子一:
        def make_temp_dir(self, path):
            """创建临时目录"""
            assert isinstance(path, str)
    
            self.client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
            self.client.connect(hostname=self.hostname,
                                port=self.port,
                                username=self.username,
                                password=self.password)
            mkdir_temp_dir_cmd = " ".join(["mkdir", path])
            try:
                stdin, stdout, stderr = self.client.exec_command(mkdir_temp_dir_cmd)
                print("mkdir temp success!", stdout.read().decode('utf-8'))
            except Exception as e:
                print("exec command error!", e)
            finally:
                self.client.close()
    
    # 例子二:
        def replace_version_file(self, src_version_path, dst_version_path):
            """替换文件"""
            assert isinstance(src_version_path, str)
            assert isinstance(dst_version_path, str)
            self.client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
            self.client.connect(hostname=self.hostname,
                                port=self.port,
                                username=self.username,
                                password=self.password)
            self.sftp = self.client.open_sftp()
            try:
                # 上传文件并重命名
                self.sftp.put(src_version_path, dst_version_path)
            except Exception as e:
                print("exec ssh ftp version file cmd error!", e)
            finally:
                self.client.close()
    
    # 注意上述代码都是try ... except ... finally的结构,可以使用with语句进行替代。
        def is_exist_of_temp(self, path):
            """判断临时目录是否存在"""
            assert isinstance(path, str)
            self.client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
            self.client.connect(hostname=self.hostname,
                                port=self.port,
                                username=self.username,
                                password=self.password)
            self.sftp = self.client.open_sftp()
            try:
                with self.sftp as sf:
                    sf.stat(path)
                    print("temp path exist!")
                    return True
            except Exception as e:
                print("temp path not exist", e)
                return False

    上面的示例代码是根据自己需求进行封装的,没有固定的格式。

  • 相关阅读:
    Codeforces 1105D Kilani and the Game【BFS】
    Codeforces 1096D Easy Problem 【DP】
    Codeforces 920F
    Codeforces 1076D Edge Deletion 【最短路+贪心】
    POJ 3090 Visible Lattice Points 【欧拉函数】
    POJ 1284 Primitive Roots (欧拉函数+原根)
    HDU 2841-Visible Trees 【容斥】
    HDU 1796 How many integers can you find 【容斥】
    HDU 4135 Co-prime (容斥+分解质因子)
    CodeForces 161D Distance in Tree【树形DP】
  • 原文地址:https://www.cnblogs.com/huaibin/p/12753259.html
Copyright © 2011-2022 走看看