获取本机ip替换指定文件的指定内容,半成品
这是一个未完成的代码,查了好久也没弄明白,现在我已经固定本机ip了,所以这个也不是特别着急了,只是想着要是能解决这个问题也挺好的,期待有缘人回复解答!
说一下需求吧,由于easymock官方服务器不稳定,在本地搭建了一个easymock服务,里面有个default.json配置文件,需要获取本机ip,之前是路由动态分配ip,所以ip地址每天都会变,所以我就想着能不能写个批处理自动改一下这个配置文件,由于这个配置文件中存在两个host,只想修改0.0.0.0这个ip,之前看了“依梦琴瑶”在网上发的贴子,修改固定内容是好用的,可留作参考,代码如下:
1 @echo off 2 setlocal enabledelayedexpansion 3 for /f "delims=" %%a in (file.conf) do ( 4 set "str=%%~a" 5 echo !str:192.168.0.16=192.168.0.6!>>conf.$ 6 ) 7 move conf.$ file.conf
default.json
文件代码如下:
1 { 2 "port": 7300, 3 "host": "0.0.0.0", 4 "pageSize": 30, 5 "proxy": false, 6 "db": "mongodb://127.0.0.1/easy-mock", 7 "unsplashClientId": "", 8 "redis": { 9 "keyPrefix": "[Easy Mock]", 10 "port": 6379, 11 "host": "127.0.0.1", 12 "password": "foobared", 13 "db": 0 14 }, 15 "blackList": { 16 "projects": [], 17 "ips": [] 18 }, 19 "rateLimit": { 20 "max": 1000, 21 "duration": 1000 22 }, 23 "jwt": { 24 "expire": "14 days", 25 "secret": "shared-secret" 26 }, 27 "upload": { 28 "types": [".jpg", ".jpeg", ".png", ".gif", ".json", ".yml", ".yaml"], 29 "size": 5242880, 30 "dir": "../public/upload", 31 "expire": { 32 "types": [".json", ".yml", ".yaml"], 33 "day": -1 34 } 35 }, 36 "ldap": { 37 "server": "", 38 "bindDN": "", 39 "password": "", 40 "filter": { 41 "base": "", 42 "attributeName": "" 43 } 44 }, 45 "fe": { 46 "copyright": "", 47 "storageNamespace": "easy-mock_", 48 "timeout": 25000, 49 "publicPath": "/dist/" 50 } 51 }
以下代码只有获取本机ip是对的,其它是我乱写的。
@echo off
setlocal enabledelayedexpansion
for /f "delims=: tokens=2" %%i in ('ipconfig ^| find /i "IPv4"') do set ip=%%i
echo %ip%
set "file=D:\code\easy-mock\config\default.json"
for /f "tokens=1,2 delims=:" %%a in (%file%) do (
if "%%a"=="host" (echo %%b="%ip%">>$)
)
move $ %file%
pause>nul