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

相关推荐
XIE39240 分钟前
Browser-use使用教程
python
酷爱码2 小时前
如何通过python连接hive,并对里面的表进行增删改查操作
开发语言·hive·python
蹦蹦跳跳真可爱5892 小时前
Python----深度学习(基于深度学习Pytroch簇分类,圆环分类,月牙分类)
人工智能·pytorch·python·深度学习·分类
简单.is.good3 小时前
【计算机网络】IP地址
网络·tcp/ip·计算机网络
MinggeQingchun5 小时前
Python - 爬虫-网页解析数据-库lxml(支持XPath)
爬虫·python·xpath·lxml
Python自动化办公社区6 小时前
Python 3.14:探索新版本的魅力与革新
开发语言·python
老六ip加速器6 小时前
国内ip地址怎么改?详细教程
网络·tcp/ip·智能路由器
weixin_贾7 小时前
最新AI-Python机器学习与深度学习技术在植被参数反演中的核心技术应用
python·机器学习·植被参数·遥感反演
张槊哲7 小时前
函数的定义与使用(python)
开发语言·python
船长@Quant7 小时前
文档构建:Sphinx全面使用指南 — 实战篇
python·markdown·sphinx·文档构建