提交 024d3ca5 authored 作者: 陈泽健's avatar 陈泽健

优化get_remote_log_with_paramiko函数,增加filter_word过滤词打印日志。

上级 00ddec0b
...@@ -1937,9 +1937,9 @@ def write_xlsx_data(xlsx_file_path, sheet_name, function_number, test_result, lo ...@@ -1937,9 +1937,9 @@ def write_xlsx_data(xlsx_file_path, sheet_name, function_number, test_result, lo
import paramiko import paramiko
import time import time
def get_remote_log_with_paramiko(host, username, private_key_path, passphrase, log_path, num_lines=1000, timeout=30): def get_remote_log_with_paramiko(host, username, private_key_path, passphrase, log_path, num_lines=1000, timeout=30, filter_word=None):
""" """
使用 Paramiko 获取远程服务器的日志文件内容. 使用 Paramiko 获取远程服务器的日志文件内容,并通过过滤词过滤日志内容.
Args: Args:
host (str): 服务器 IP 地址或域名. host (str): 服务器 IP 地址或域名.
...@@ -1949,6 +1949,7 @@ def get_remote_log_with_paramiko(host, username, private_key_path, passphrase, l ...@@ -1949,6 +1949,7 @@ def get_remote_log_with_paramiko(host, username, private_key_path, passphrase, l
log_path (str): 日志文件路径. log_path (str): 日志文件路径.
num_lines (int): 要获取的日志行数 (默认 100). num_lines (int): 要获取的日志行数 (默认 100).
timeout (int): SSH 命令执行的超时时间(秒). timeout (int): SSH 命令执行的超时时间(秒).
filter_word (str): 过滤词,只有包含该词的日志行才会被返回 (默认 None).
Returns: Returns:
str: 获取的日志内容,如果出错返回 None. str: 获取的日志内容,如果出错返回 None.
...@@ -1970,6 +1971,15 @@ def get_remote_log_with_paramiko(host, username, private_key_path, passphrase, l ...@@ -1970,6 +1971,15 @@ def get_remote_log_with_paramiko(host, username, private_key_path, passphrase, l
output = stdout.read().decode('utf-8') output = stdout.read().decode('utf-8')
print("Successfully retrieved log content.") print("Successfully retrieved log content.")
print("Full log content:")
print(output) # 打印完整的日志内容
if filter_word:
filtered_output = "\n".join([line for line in output.split('\n') if filter_word in line])
if not filtered_output:
print(f"No lines found containing the filter word: {filter_word}")
return filtered_output
return output return output
except Exception as e: except Exception as e:
...@@ -1984,7 +1994,8 @@ def get_remote_log_with_paramiko(host, username, private_key_path, passphrase, l ...@@ -1984,7 +1994,8 @@ def get_remote_log_with_paramiko(host, username, private_key_path, passphrase, l
# private_key_path = "C:\\Users\\29194\\.ssh\\id_rsa" # 替换为你的私钥文件路径 # private_key_path = "C:\\Users\\29194\\.ssh\\id_rsa" # 替换为你的私钥文件路径
# passphrase = "Ubains@123" # 替换为你的 passphrase # passphrase = "Ubains@123" # 替换为你的 passphrase
# log_path = "/var/www/java/api-java-meeting2.0/logs/ubains-INFO-AND-ERROR.log" # log_path = "/var/www/java/api-java-meeting2.0/logs/ubains-INFO-AND-ERROR.log"
# log_content = get_remote_log_with_paramiko(host, username, private_key_path, passphrase, log_path) # filter_word = "桌牌"
# log_content = get_remote_log_with_paramiko(host, username, private_key_path, passphrase, log_path, filter_word=filter_word)
# #
# if log_content: # if log_content:
# print(log_content) # print(log_content)
......
...@@ -291,4 +291,5 @@ ...@@ -291,4 +291,5 @@
81. 2025-04-17 81. 2025-04-17
- 修复因xlsx文件读取失败导致的报错。 - 修复因xlsx文件读取失败导致的报错。
82. 2025-04-18 82. 2025-04-18
- 修复展厅巡检预定系统因页面bug导致的异常定位报错。处理优化 - 修复展厅巡检预定系统因页面bug导致的异常定位报错。处理优化。
\ No newline at end of file - 优化get_remote_log_with_paramiko函数,增加filter_word过滤词打印日志。
\ No newline at end of file
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论