python中paramiko的使用

import paramiko
import configparser

class paramikoclient:

    def __init__(self, config_str):
        self.config = configparser.ConfigParser() #后面的ConfigParser中的C和P实大写
        self.config.read(config_str)
        paramiko.util.log_to_file('paramiko.log')  # 记录日志文件
        self.client = paramiko.SSHClient()
        self.client.set_missing_host_key_policy(paramiko.AutoAddPolicy())

    def connect(self):
        try:
            self.client.connect(hostname=self.config.get("ssh", "host"), port=self.config.getint("ssh", "port"),
                                username=self.config.get("ssh", "username"),
                                password=self.config.get("ssh", "password"),
                                timeout=self.config.getfloat("ssh", "timeout"))
        except Exception as e:
            print(e)
            try:
                self.client.close()
            except:
                pass

    def run_cmd(self, cmd_str):
        stdin, stdout, stderr = self.client.exec_command(cmd_str)
        for line in stdout:
            print(line)
            
client = paramikoclient("config.ini")
client.connect()
client.run_cmd("tac /opt/tomcat_tms_test/logs/logback-tms.log") #读取tms模块最后10行的日志

python中paramiko的使用_第1张图片

你可能感兴趣的:(python中paramiko的使用)