python连接Linux读取日志
import paramiko from common import load_file yaml_dict = load_file.get_root_yaml() linux_info = yaml_dict["linux_db_info"] class SSH: def __init__(self): self.ip = yaml_dict["linux_db_info"]["host"] self.user = yaml_dict["linux_db_info"]["user"] self.password = yaml_dict["linux_db_info"]["password"] self.port = yaml_dict["linux_db_info"]["port"] # 链接失败的重试次数 self.try_times = 3 def connect_server(self): while True: try: self.ssh = paramiko.SSHClient() # 创建SSHClient 实例对象 self.ssh.set_missing_host_key_policy( paramiko.AutoAddPolicy()) # 调用方法,表示没有存储远程机器的公钥,允许访问 print('------------开始连接服务器(%s)-----------' % self.ip) self.ssh.connect(self.ip, self.port, username=self.user, password=self.password) # 连接远程机器,地址,端口,用户名密码 print('------------连接服务器成功!.....-----------') return except Exception: if self.try_times != 0: print(f'连接远程linux服务器(ip:{self.ip})失败,进行重试!') self.try_times -= 1 else: print(u'重试3次失败,结束程序') exit(1) def exec_commands(self, cmd): stdin, stdout, stderr = self.ssh.exec_command(cmd) # stdout 为正确输出,stderr为错误输出 # results = str(stdout.read()).encode(encoding="utf-8").decode() # print(stdout.read()) # print(type(stdout.read())) results = stdout.read().decode("utf-8") print("----",results) # error_mess = str(stderr.read()) return results def get_grep_num(grep_log,modle_log="core"): ssh = SSH() ssh.connect_server() cmd = f"cd /data/workspace/nova-iwms-{modle_log}/logs/;cat iwms-{modle_log}_info.log |grep '{grep_log}'|wc -l" print_str = ssh.exec_commands(cmd) return print_str if __name__ == '__main__': get_grep_num("封箱提示,接口通知系统配置未打开。") # host=SSH() # host.connect_server() # cmd="cd /data/workspace/nova-iwms-station/logs/;cat iwms-station.log |tail -1" # res = host.exec_commands(cmd) # if "发送电子标签指令" in res: # print(111)
查看操作有无日志产生:
1、在操作前查看日志数量
2、操作动作
3、在操作后查看日志数量
4、判断数量增加了则有产生日志