章工运维 章工运维
首页
  • linux
  • windows
  • 中间件
  • 监控
  • 网络
  • 存储
  • 安全
  • 防火墙
  • 数据库
  • 系统
  • docker
  • 运维工具
  • other
  • elk
  • K8S
  • ansible
  • Jenkins
  • GitLabCI_CD
  • 随笔
  • 面试
  • 工具
  • 收藏夹
  • Shell
  • python
  • golang
友链
  • 索引

    • 分类
    • 标签
    • 归档
    • 首页 (opens new window)
    • 关于我 (opens new window)
    • 图床 (opens new window)
    • 评论 (opens new window)
    • 导航栏 (opens new window)
周刊
GitHub (opens new window)

章工运维

业精于勤,荒于嬉
首页
  • linux
  • windows
  • 中间件
  • 监控
  • 网络
  • 存储
  • 安全
  • 防火墙
  • 数据库
  • 系统
  • docker
  • 运维工具
  • other
  • elk
  • K8S
  • ansible
  • Jenkins
  • GitLabCI_CD
  • 随笔
  • 面试
  • 工具
  • 收藏夹
  • Shell
  • python
  • golang
友链
  • 索引

    • 分类
    • 标签
    • 归档
    • 首页 (opens new window)
    • 关于我 (opens new window)
    • 图床 (opens new window)
    • 评论 (opens new window)
    • 导航栏 (opens new window)
周刊
GitHub (opens new window)
  • python

    • python基础

      • python基础知识
      • python基础较难的15个知识点
    • FastAPI

    • python每日练习脚本

    • python3给防火墙添加放行
    • python生成部署脚本
    • python将多个文件内容输出到一个文件中
    • 使用 Aligo 定时备份服务器文件
    • python监控日志文件并发送钉钉告警
    • python监控数据库脚本并发送钉钉告警
    • 使用python编写自动化发布脚本
    • 查询redis列表某个元素
    • centos7安装python3
    • python环境管理工具介绍
    • conda安装和镜像源配置
    • pip更换国内源
    • python爬虫
    • python环境启动服务报错缺少glibc库版本
    • 监控目录或文件变化
    • 批量更改文件
    • python引用数据库
  • shell

  • go

  • 编程
  • python
章工运维
2022-12-14

python3给防火墙添加放行

脚本如下

from http.server import BaseHTTPRequestHandler
from urllib import parse
import subprocess

class GetHandler(BaseHTTPRequestHandler):

    secret_key = r'Hi93d4cfa5863770f9a0c87d8,b85a4ebc7'
    #secret_key = r'4AULgSBxBESAt3IaItH6C227ik4,fW5o6xr+1!7CfjJSCTf$5jxV)+w7iImrx@'
    #secret_key = r'4AULgSBxBESAt3IaItH6C227ik4,fW5o6xr+1!7CfjJSCTf$5jxV)+w7iImrx@'
    #commands = "iptables -A INPUT -s {}/32 -p tcp -m state --state NEW -m tcp --dport 443 -j ACCEPT && service iptables save"
    commands = r"""firewall-cmd --add-rich-rule="rule family="ipv4" source address="{}" port port="3306" protocol="tcp" accept" --zone=public --permanent && firew
all-cmd --reload """
    def process_command(self,client_ip):
        commands = self.commands.format(client_ip)
        try:
            process_result = subprocess.run(commands,stdout=subprocess.PIPE, shell=True, check=True)
            re_stdout = ''
            re_stderr = ''
            if process_result.stdout:
                re_stdout = str(process_result.stdout,encoding='utf-8')
            if process_result.stderr:
                re_stderr = str(process_result.stderr,encoding='utf-8')
            return (re_stdout, re_stderr)
        except subprocess.CalledProcessError as e:
            return "Process Command Error."

        #return subprocess.run(self.commands)

    def do_GET(self):
       # parsed_path = parse.urlparse(self.path)
        #parsed_header = parse.urlparse(self.headers)
        client_ip = self.address_string()
        #client_ip = self.headers['X-Real-IP']
        client_request_path = self.path[1:]
        if client_request_path != self.secret_key:
            self.send_error(403)

        r_result = self.process_command(client_ip)
        message = ''.join(r_result)
        self.send_response(200)
        self.send_header('Content-Type',
                         'text/plain; charset=utf-8')
        self.end_headers()
        self.wfile.write(message.encode('utf-8'))

if __name__ == '__main__':
    from http.server import HTTPServer
    server = HTTPServer(('0.0.0.0', 29999), GetHandler)
    print('Starting server, use <Ctrl-C> to stop')
    server.serve_forever()
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
微信 支付宝
上次更新: 2022/12/15, 12:14:09

← 自动化数据库备份和恢复 python生成部署脚本→

最近更新
01
shell脚本模块集合
05-13
02
生活小技巧(认知版)
04-29
03
生活小技巧(防骗版)
04-29
更多文章>
Theme by Vdoing | Copyright © 2019-2025 | 点击查看十年之约 | 鄂ICP备2024072800号
  • 跟随系统
  • 浅色模式
  • 深色模式
  • 阅读模式