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
---
开心工作,认真生活;回望来时路,脚印三两,笑声无数...
开心工作,认真生活;回望来时路,脚印三两,笑声无数...