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地址已经成功被替换成了外网的地址了~

相关推荐
别讲概念13 分钟前
JavaEE(6)TCP_IP
网络·tcp/ip·java-ee
2601_9679791213 分钟前
python中str函数怎么用?使用方法与注意事项详解
python·使用方法·字符串转换·注意事项·str函数
青少儿编程课堂15 分钟前
蓝桥杯青少组图形化+信息素养+电子学会Scratch考级:校园口算闯关解析
c++·python·算法·bfs·信息学竞赛
fanruitian40 分钟前
python 机器学习 交叉验证train_test_split 网格搜索GridSearchCV
开发语言·python·机器学习
Novproxy_Z10 小时前
商用代理避坑:如何识别商家拿机房 IP 冒充住宅 IP
java·网络·tcp/ip
垆边人似月.10 小时前
RDMA 文件传输踩坑(五):READY 发送成功后,为什么出现 Work Request Flushed Error?
服务器·网络·tcp/ip
前端精髓10 小时前
结构化输出with_structured_output(抽卡)
python·langchain
qetfw11 小时前
UOS DHCP Relay:跨网段分配与 IP、DNS、网关验证
linux·服务器·网络·tcp/ip·uos
尹人入圣11 小时前
目前热门的IP驱动产业新场景新工具
网络·python·网络协议·tcp/ip
Nuanyt12 小时前
Agent 学习笔记(下篇三):上线工程、进阶选择与操作界面(Stage 7 / 7.5 / 8 + 延伸路线)
笔记·python·学习·memory·aiagent·harness