Python 简易web日志查看工具&可改装为命令行工具

效果图

原理

利用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, client def 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 False def 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

代码详见Python-webLogSight

Python 简易web日志查看工具&可改装为命令行工具的更多相关文章

  1. Expo大作战(六)--expo开发模式,expo中exp命令行工具,expo中如何查看日志log,expo中的调试方式

    简要:本系列文章讲会对expo进行全面的介绍,本人从2017年6月份接触expo以来,对expo的研究断断续续,一路走来将近10个月,废话不多说,接下来你看到内容,将全部来与官网 我猜去全部机翻+个人 ...

  2. x264源代码简单分析:x264命令行工具(x264.exe)

    ===================================================== H.264源代码分析文章列表: [编码 - x264] x264源代码简单分析:概述 x26 ...

  3. Scrapy命令行工具简介

    Windows 10家庭中文版,Python 3.6.4,virtualenv 16.0.0,Scrapy 1.5.0, 在最初使用Scrapy时,使用编辑器或IDE手动编写模块来创建爬虫(Spide ...

  4. OpenStack命令行工具与API

    Openstack命令行工具 我们推荐Openstack命令行工具和Openstack的Dashboard两者结合使用.一些用户由于使用过其他云技术背景的,可能会使用EC2兼容的API,相对于我们需要 ...

  5. 2019-8-31-HttpRepl-互操作的-RESTful-HTTP-服务调试命令行工具

    title author date CreateTime categories HttpRepl 互操作的 RESTful HTTP 服务调试命令行工具 lindexi 2019-08-31 16:5 ...

  6. HttpRepl 互操作的 RESTful HTTP 服务调试命令行工具

    今天早上曽根セイラ告诉我一个好用的工具 HttpRepl 这是一个可以在命令行里面对 RESTful 的 HTTP 服务进行路由跳转和访问的命令行工具.可以使用 cd 这个命令和像文件跳转已经跳转到下 ...

  7. 使用.Net Core编写命令行工具(CLI)

    命令行工具(CLI) 命令行工具(CLI)是在图形用户界面得到普及之前使用最为广泛的用户界面,它通常不支持鼠标,用户通过键盘输入指令,计算机接收到指令后,予以执行. 通常认为,命令行工具(CLI)没有 ...

  8. 吻逗死(windows)系统下自动部署脚本(for java spring*)及linux命令行工具

    转载请注明出处:https://www.cnblogs.com/funnyzpc/p/10051647.html (^^)(^^)自動部署腳本原本在上個公司就在使用,由於近期同事需要手動部署一個Spr ...

  9. MySQL命令行工具各功能说明(转)

    MySQL 服务器端使用工具程序 mysqld - SQL 后台程序(即 MySQL 服务器进程).该程序必须启动运行,才能连接服务器来访问数据库. mysqld_safe - 服务器启动脚本,可以通 ...

随机推荐

  1. 密度峰值聚类算法(DPC)

    密度峰值聚类算法(DPC) 凯鲁嘎吉 - 博客园 http://www.cnblogs.com/kailugaji/ 1. 简介 基于密度峰值的聚类算法全称为基于快速搜索和发现密度峰值的聚类算法(cl ...

  2. 在Rust中,cargo使用国内镜像源

    一个编程语言依赖包管理的普通问题. cargo解决得比较优雅. 一,新建$HOME/.cargo/config文件 [source.crates-io] registry = "https: ...

  3. HTML与CSS学习笔记(6)

    1.flex弹性盒模型 在父容器中添加display:flex;把父容器变为弹性盒模型 作用(写)在flex容器上 作用(写)在flex子项上 flex-direction order flex-wr ...

  4. mysql high severity error 缺少根元素

    high severity error 缺少根元素: C:\Users\cf.yu\AppData\Roaming\Oracle\MySQL Notifier里的settings.config.删除他 ...

  5. 数据驱动表格| 根据json数据,自动生成合并式table

    1.数据驱动表格 2.数据驱动表格(2)

  6. ini_set()

    ini_set ( string $varname , string $newvalue ) : string 设置指定配置选项的值.这个选项会在脚本运行时保持新的值,并在脚本结束时恢复. 参数 va ...

  7. QPushButton 一组中凸显选中的一个,且只能选中一个。

    QButtonGroup * buttonGroup = new QButtonGroup(this); buttonGroup->setExclusive(true); ui->push ...

  8. Java实现字符串反转【Leetcode】

    Write a function that reverses a string. The input string is given as an array of characters char[]. ...

  9. Docker Hub镜像加速器

    国内从 Docker Hub 拉取镜像有时会遇到困难,此时可以配置镜像加速器.Docker 官方和国内很多云服务商都提供了国内加速器服务. 1.配置加速地址 vim /etc/docker/daemo ...

  10. Docker 指定数据储存目录

    Docker 指定储存目录(原参数 --graph 已经失效 .) 方法一 一.在 Docker 配置文件中配置 /etc/docker/daemon.json 下面的命令是在 daemon.json ...