mysql二进制日志查看工具_Python 简易web日志查看工具&可改装为命令行工具

c74e9dbc38955a0d510b7419cdee25f9.png
f27305eb4baaa012a9f2748186f6163d.png
955417b46c4b9ea9a7210681380f9cfd.png
e004fcc93a5b7bfe875f8abf1e9c6ebd.png
75d30424279a7578187edf42bb677602.png

原理

利用python的paramiko库模拟ssh登录操作,并执行tail命令

所需库

flask、paramiko、gevent,python3

部分代码

@app.route('/api/do', methods=["POST", "GET"])def api_do(): try: ip = flask.request.form.get("ip") port = flask.request.form.get("port") username = flask.request.form.get("username") password = flask.request.form.get("password") path = flask.request.form.get("path") count = flask.request.form.get("count") except: return { "status": "err", "code": "200", "error": "params error" } if ip is None: ip = "127.0.0.1" if port is None: port = 22 if username is None: username = "root" if password is None: password = "root" if path is None: path = "/usr/local/logs" if count is None: count = 100 if tool.check_ip(ip) is False: return { "status": "err", "code": "202", "error": "ip error" } if tool.check_file(path) is False: return { "status": "err", "code": "203", "error": "file path error or forbid" } tf, ssh = tail.get_ssh(ip, username, password, port=port) if tf is False: return { "status": "err", "code": "201", "error": "ssh login error" } res = tail.tail_one(ssh, path, count=count) return res
def get_ssh(ip, username, password, port=22): # 实例化SSHClient client = pm.SSHClient() # 自动添加策略,保存服务器的主机名和密钥信息,如果不添加,那么不再本地know_hosts文件中记录的主机将无法连接 client.set_missing_host_key_policy(pm.AutoAddPolicy()) # 连接SSH服务端,以用户名和密码进行认证 try: client.connect(hostname=ip, port=port, username=username, password=password) except BaseException as e: print(e) return False, e return True, clientdef tail_one(ssh, path, count=100): # 获取日志信息,传入利用get_ssh生成的client,路径,显示数量 cmd = "tail -n {count} {path}".format(count=count, path=path) stdin, stdout, stderr = ssh.exec_command(cmd) err = stderr.read().decode() if len(err) != 0: return { "status": "err", "code": "300", "error": err } else: return { "status": "ok", "code": "100", "result": stdout.read().decode() }
def check_ip(ip): # 检查ip是否合法 ip_format = r'^((([1-2][0-9]{2})|([1-9][0-9])|([0-9]))[.]){3}(([1-2][0-9]{2})|([1-9][0-9])|([0-9]))$' # ip_format = r'^[1-2]?[0-9]{1,2}.[1-2]?[0-9]{1,2}.[1-2]?[0-9]{1,2}.[1-2]?[0-9]{1,2}$' match = re.search(ip_format, ip) # print(match) if match: return match.group() else: return Falsedef check_file(path): # 检查路径是否合法,限定使用区域,禁止执行其他命令 path_default = "/usr/local/logs" forbid_word = r'&|||[ ]|`|"|'' allow_path = r'/usr/local/' match = re.search(forbid_word, path) if match: return False match = re.search(allow_path, path) if match: return path else: return False
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值