• 使用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

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

  • 相关阅读:
    安装补丁“此更新不适用于你的计算机”解决办法
    spanning-tree portfast什么意思?
    Win10离线安装.NET Framework 3.5的方法
    逐帧轮播图效果实现
    纯css实现同一页面下选择之后更换内容效果
    纯html+css中实现静态选座位效果技巧(input+label使用小技巧)
    js中实现杨辉三角
    用css实现html中单选框样式改变
    下划线hover下动态出现技巧
    CSS样式整理
  • 原文地址:https://www.cnblogs.com/huaibin/p/12753259.html
Copyright © 2020-2023  润新知