001_自定义过滤及添加文件内容脚本(nginx)
2024-10-04 00:49:42
一、工作中遇到sed添加及修改在nginx末尾添加太麻烦了
需求:随意查找添加一条以前不存在的内容加到"}"前一行
实现:
#!/usr/bin/env python
# coding=utf-8
import sys,os
from optparse import OptionParser parse = OptionParser()
parse.add_option("-c", "--context", help="add one line contexts",default="xxxhtxxxtps_status")
parse.add_option("-f", "--file", help="modify file",default="/006_eleallproject/002_surveypro/001_nginxdevops/002_camel-agent-deploy/camel-agent-deploy2.0/nginx.conf") (options, args) = parse.parse_args()
content = options.context
file = os.path.realpath(options.file)
#content="x$https_status"
#file='/006_eleallproject/002_surveypro/001_nginxdevops/002_camel-agent-deploy/camel-agent-deploy2.0/nginx.conf'
print options _list_content=[]
m = 0
count = 0
#获取"}"的位置
try:
fh = open(file, 'rb')
for n in fh.readlines():
m += 1
if (n.find('}') >= 0):
global count
count = m
_list_content.append(n)
else:
_list_content.append(n)
finally:
fh.close() #在"}"位置前添加内容
n = 0
ngconf = open(file, 'rb')
for s in ngconf.readlines():
n += 1
if s.find('}') >= 0 and n >= count-1:
#在以前的列表中有任何可以找到的内容都不会添加
result = any([str(line.strip()).find(content) >=0 for line in _list_content])
if not result:
_list_content.insert(count-1, content+"\n")
ngconf.close() #写入文件
end_nginx_f = open(file, 'wb')
end_nginx_f.writelines(_list_content)
end_nginx_f.close()
最新文章
- 高通vuforia+Unity3D 制作ar app
- Cookie实现商品浏览记录--方式一:Java实现
- 【Android】Android应用安装失败及无法打开
- PHP 实现 一致性哈希 算法(转的)
- acdream 1409 Musical 状压DP
- IT版孔乙己(转)
- ServletContext对象
- poj 1274 The Perfect Stall【匈牙利算法模板题】
- <;object>;元素+svg 绘制图片
- 使用SQLQuery 在Hibernate中使用sql语句
- Shim 与 Polyfill
- 网络协议 15 - P2P 协议:小种子大学问
- 45)django-分页实现
- Linux下修改环境变量,不小心改错,找不到命令解决办法
- HDU 2586 How far away(dfs+邻接表)
- 刘志梅2017710101152.《面向对象程序设计(java)》第一周学习总结
- Java内存泄漏定位
- sublime3 常用快捷键
- JSON中JObject和JArray的修改
- hibernate Validator 6.X 的学习,bean的约束(主要包括的是容器元素的验证)
热门文章
- 4:Python的while循环
- K8S 容器的资源需求、资源限制
- 基于Aspectj 注解实现 spring AOP
- iOS--------获取当前连接的WiFi以及IP地址
- iOS------获取当前时间和当前时间戳
- <;解决方法>;Centos安装使用Chromedriver
- spring Boot 出现:org.springframework.context.ApplicationContextException: Unable to start ServletWebServerApplicationContext due to missing ServletWebServerFactory bean.
- Fiddler做代理服务器时添加X-Forwarder-For转发真实客户端ip
- java后台打开浏览器代码
- ros中自定义消息 报错 ImportError: No module named em