docker部署python工程获取公司公网ip

前言

实时获取公司变动的公网ip

python工程代码
目录结构
]# tree -aCg .
.
├── [root    ]  data
│   ├── [root    ]  get_ip.py
│   ├── [root    ]  ip_db.py
├── [root    ]  docker-compose.yml
└── [root    ]  root

1 directories, 4 files
python与sqlite3数据库的代码与创建数据表及插入数据
cat > /data/ip_db.py <<-EOF
#!/usr/bin/env python3

import sqlite3


class CreateDb():

    def __init__(self, company_public_ip, database_filename="/data/ip.db", database_table="company_public_ip"):
        self.company_public_ip = company_public_ip
        self.database_table = database_table
        self.conn = sqlite3.connect(database_filename)
        self.cur = self.conn.cursor()

    def createTable(self):
        create_table = "create table {0} (company_public_ip char(20));".format(self.database_table)
        self.cur.execute(create_table)
        self.conn.commit()

    def insertTableData(self):
        insert_table_data= "insert into {0} values('{1}')".format(self.database_table, self.company_public_ip)
        self.cur.execute(insert_table_data)
        self.conn.commit()

    def updateTableData(self):
        update_table_data = "update {0} set {0} = '{1}'".format(self.database_table, self.company_public_ip)
        self.cur.execute(update_table_data)
        self.conn.commit()

    def selectTableData(self):
        select_table_data = "select * from {0}".format(self.database_table)
        self.cur.execute(select_table_data)
        self.conn.commit()
        get_table_data = self.cur.fetchone()
        return get_table_data[0]

    def closeDatabase(self):
        self.cur.close()
        self.conn.close()


if __name__ == "__main__":
    use_sqlite = CreateDb("0.0.0.0")
    use_sqlite.createTable()
    use_sqlite.insertTableData()
    use_sqlite.selectTableData()
    use_sqlite.closeDatabase()
EOF
python获取公司公网ip
cat > /data/get_ip.py <<-EOF
#!/usr/bin/env python3

import sh
import re
import requests
import json
import sys
import sqlite3
from ip_db import CreateDb

class GetIp():
    
    # 自行从钉钉上获取机器人的Webhook的url
    def __init__(self, address="http://myip.ipip.net", post_url="https://oapi.dingtalk.com/robot/send?access_token=xxx"):	
        self.address = address
        self.post_url = post_url

    def getIp(self):
        get_company_public_ip = re.findall("\d{1,3}.\d{1,3}.\d{1,3}.\d{1,3}", str(sh.curl(self.address)))[0]
        return get_company_public_ip

    def getMonitorIp(self):
        get_CurrentIp = GetIp().getIp()
        user_database = CreateDb(get_CurrentIp)
        get_Database_Ip = user_database.selectTableData()
        if get_CurrentIp == get_Database_Ip:
            sys.exit()
        user_database.updateTableData()
        user_database.closeDatabase()
        return get_CurrentIp

    def sendIpToDingding(self):
        headers = {'Content-Type': 'application/json'}
        sendData = "公司公网ip --> {0}".format(GetIp().getMonitorIp())
        data = {
            "msgtype": "text",
            "text": {
                "content": sendData
            }
        }
        requests.post(self.post_url, data=json.dumps(data), headers=headers)         


if __name__ == "__main__":
    GetIp().sendIpToDingding()
EOF
定时任务
cat > root <<-EOF
# do daily/weekly/monthly maintenance
# min   hour    day     month   weekday command
*/15    *       *       *       *       run-parts /etc/periodic/15min
0       *       *       *       *       run-parts /etc/periodic/hourly
0       2       *       *       *       run-parts /etc/periodic/daily
0       3       *       *       6       run-parts /etc/periodic/weekly
0       5       1       *       *       run-parts /etc/periodic/monthly
*/1       *       *       *       *       /data/get_ip.py &> /dev/null
EOF
docker-compose启动应用服务文件
cat > docker-compose.yml <<-EOF
version: "3.5"
services:
  getip:
    image: techsharearea/apline:python3.8
    container_name: get-ip
    hostname: get-ip
    volumes:
      - ./data:/data:Z
      - ./root:/etc/crontabs/root:ro
    restart: always
    tty: true
 EOF
部署服务
chmod +x /data/ip_db.py
chmod +x /data/get_ip.py
docker-compose up -d
docker exec get-ip /data/ip_db.py
docker exec get-ip crond
结语

… …

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值