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

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

  • 相关阅读:
    团队项目-第二阶段冲刺1
    第十四周总结
    第十三周总结
    程序员修炼之道阅读笔记02
    第十二周总结
    程序员修炼之道阅读笔记01
    Spring Boot 揭秘与实战 自己实现一个简单的自动配置模块
    Spring Boot 揭秘与实战 源码分析
    Spring Boot 揭秘与实战 源码分析
    Spring Boot 揭秘与实战(九) 应用监控篇
  • 原文地址:https://www.cnblogs.com/huaibin/p/12753259.html
Copyright © 2011-2022 走看看