• python 远程执行linux命令


    python3执行Linux命令,这里用模块 subprocess,此模块详情可参考:https://www.cnblogs.com/lincappu/p/8270709.html

    以下是自己封装的一些方法:

    check_output传参太复杂,转用os.system执行

    class RemoteLinux:
        def __init__(self, ip, username, password):
            self.ip = ip
            self.username = username
            self.password = password
    
        def run_cmd(self, cmd):
            cmd_str = "sshpass -p {} ssh {}@{} '{}'".format(self.password, self.username, self.ip, cmd)
            result = os.system(cmd_str)
            logger.info("命令[%s]执行返回码:%s", cmd_str, result)
            return result
    
        def run_scp_2_remote(self, local_dir, remote_dir):
            cmd_str = "sshpass -p {} scp -r {}/* {}@{}:{}".format(self.password, local_dir, self.username, self.ip,
                                                                remote_dir)
            result = os.system(cmd_str)
            logger.info("命令[%s]执行返回码:%s", cmd_str, result)
            return result
    
        def run_scp_2_local(self, local_dir, remote_dir):
            cmd_str = "sshpass -p {} scp -r {}@{}:{}/* {}".format(self.password, self.username, self.ip, remote_dir,
                                                                local_dir)
            result = os.system(cmd_str)
            logger.info("命令[%s]执行返回码:%s", cmd_str, result)
            return result
  • 相关阅读:
    Linux常用命令2
    Linux常用命令1
    Nginx配置Kafka
    SpringBoot整合Druid
    spring boot jpa
    mybatis-plus_2
    copy data to map
    HashMap容量问题
    在SpringBoot主启动类中获取实例化的Bean
    Linux环境中Rsync增量备份文件
  • 原文地址:https://www.cnblogs.com/dannyyao/p/12252877.html
Copyright © 2020-2023  润新知