CentOS中使用Python将文本中的IP地址替换为外网地址

CentOS中使用Python将文本中的IP地址替换为外网地址

前言

使用python编写脚本,用来更新服务器的ip地址,脚本主要分为了两部分

  • 获取服务器的外网ip地址
  • 读取配置文件,将旧ip地址 替换成 新的ip地址

获取外网ip

首先需要编写python代码,用于获取外网地址

python 复制代码
# -*- coding:utf-8 -*-
from urllib2 import urlopen
# 获取ip地址
myIp = urlopen('http://ip.42.pl/raw').read()

其实就是访问一个url

bash 复制代码
http://ip.42.pl/raw

然后就会返回本机器的外网地址

最后用变量保存即可

读取配置文件

继续编写代码,读取vue的配置文件,然后取出里面的ip地址

python 复制代码
# 替换ip地址
def replace(file, newStr):
    fileData = ""
    with io.open(file, 'r', encoding='utf-8') as f:
        lines = f.readlines()
        # 查找到ip地址
        for line in lines:
            ipList = re.findall(r"\b(?:[0-9]{1,3}\.){3}[0-9]{1,3}\b", line)
            if not ipList == []:
                oldStr = ipList[0]
                print file, "替换ip地址:", oldStr, "->", newStr
                break
        # 替换ip地址
        for line in lines:
            line = line.replace(oldStr, newStr)
            fileData += line

    with io.open(file,"w",encoding="utf-8") as f:
        f.write(fileData)

我们这里是使用了正则表达式,从文本中取出ip地址,因为re.findall获取到的是一个列表,所以我们只需要判断第一个元素,然后将文本中的旧ip地址,替换成服务器上获取到的,最后再将文本写入到源文件中,完成整个过程

完整代码

完整的 replaceIp.py 脚本如下所示

python 复制代码
# -*- coding:utf-8 -*-
from urllib2 import urlopen
import io
import re
# 获取ip地址
myIp = urlopen('http://ip.42.pl/raw').read()

# 替换ip地址
def replace(file, newStr):
    fileData = ""
    with io.open(file, 'r', encoding='utf-8') as f:
        lines = f.readlines()
        # 查找到ip地址
        for line in lines:
            ipList = re.findall(r"\b(?:[0-9]{1,3}\.){3}[0-9]{1,3}\b", line)
            if not ipList == []:
                oldStr = ipList[0]
                print file, "替换ip地址:", oldStr, "->", newStr
                break
        # 替换ip地址
        for line in lines:
            line = line.replace(oldStr, newStr)
            fileData += line

    with io.open(file,"w",encoding="utf-8") as f:
        f.write(fileData)

replace("../config/vue_admin.env", myIp)
replace("../config/vue_web.env", myIp)

其中 vue_mogu_admin.env 文件内容如下所示【替换前】

bash 复制代码
NODE_ENV=production
ADMIN_API=http://127.0.0.1:8607/admin
PICTURE_API=http://127.0.0.1:8607/picture
WEB_API=http://127.0.0.1:8607/web
Search_API=http://127.0.0.1:8607/search
FILE_API=http://127.0.0.1:8600/
BLOG_WEB_URL=http://127.0.0.1:9527
SOLR_API=http://127.0.0.1:8080/solr
ELASTIC_SEARCH=http://127.0.0.1:5601

然后我们执行下面命令,开始替换ip地址

bash 复制代码
python2 replaceIp.py

最后再次查看 vue_admin.env 文件

bash 复制代码
NODE_ENV=production
ADMIN_API=http://101.132.122.175:8607/admin
PICTURE_API=http://101.132.122.175:8607/picture
WEB_API=http://101.132.122.175:8607/web
Search_API=http://101.132.122.175:8607/search
FILE_API=http://101.132.122.175:8600/
BLOG_WEB_URL=http://101.132.122.175:9527
SOLR_API=http://101.132.122.175:8080/solr
ELASTIC_SEARCH=http://101.132.122.175:5601

ip地址已经成功被替换成了外网的地址了~

相关推荐
应用市场几秒前
TCP网络连接断开检测机制详解——C++实现网络连通性判断与断线类型识别
网络·c++·tcp/ip
宁雨桥1 分钟前
多引擎中英翻译API搭建与使用教程
python·fastapi·翻译
Luke Ewin2 分钟前
基于FunASR开发的可私有化部署的语音转文字接口 | FunASR接口开发 | 语音识别接口私有化部署
人工智能·python·语音识别·fastapi·asr·funasr
龙山云仓5 分钟前
No095:沈括&AI:智能的科学研究与系统思维
开发语言·人工智能·python·机器学习·重构
山风wind5 分钟前
设计模式-模板方法模式详解
python·设计模式·模板方法模式
Bruce_Liuxiaowei11 分钟前
Mac_Linux 查询网站IP地址:4个核心命令详解
linux·tcp/ip·macos
铉铉这波能秀12 分钟前
正则表达式从入门到精通(字符串模式匹配)
java·数据库·python·sql·正则表达式·模式匹配·表格处理
山土成旧客19 分钟前
【Python学习打卡-Day23】从重复到重用:用Pipeline和ColumnTransformer重构你的机器学习工作流
python·学习·重构
棒棒的皮皮24 分钟前
【OpenCV】Python图像处理之平滑处理
图像处理·python·opencv·计算机视觉
Kurbaneli25 分钟前
Python列表推导式保姆级教程
python