[新手上路]批处理新手入门导读[视频教程]批处理基础视频教程[视频教程]VBS基础视频教程[批处理精品]批处理版照片整理器
[批处理精品]纯批处理备份&还原驱动[批处理精品]CMD命令50条不能说的秘密[在线下载]第三方命令行工具[在线帮助]VBScript / JScript 在线参考
返回列表 发帖

[文本处理] 批处理怎样实现外网ip提取问题

看到论坛说http://ip.42.pl/rawhttps://api.ipify.org/?format=json 分别可以读取到自己外网Ip,测试了一下是真的,问题来了,用批处理咋分别按这2个网址进行分别读取呢?也就是需要2个不同地址的方案进行批处理提取Ip ,

回复 1# wh123wh123


[方案1~5]只返回公网IP,不需要进一步过滤的网址:
https://ifconfig.me/ip
http://ip.42.pl/raw
https://api.ip.sb/ip

[方案6]返回值有其它内容,需要使用正则进行过滤公网IP:
https://ifconfig.me/
http://ip.cn/
http://whatismyip.org
https://ip.sb/

公网IP_1.bat
  1. @echo off
  2. set "URL=http://ip.42.pl/raw"
  3. >%temp%\download.vbs echo Set objDOM = WScript.GetObject(WScript.Arguments(0))
  4. >>%temp%\download.vbs echo Do Until objDOM.ReadyState = "complete"
  5. >>%temp%\download.vbs echo WScript.Sleep 100
  6. >>%temp%\download.vbs echo Loop
  7. >>%temp%\download.vbs echo WScript.Echo objDOM.DocumentElement.OuterText
  8. for /f %%i in ('cscript //nologo "%temp%\download.vbs" "%URL%"') do (
  9.     set "PublicIP=%%i"
  10. )
  11. echo,%PublicIP%
  12. pause
复制代码
公网IP_2.bat
http://bcn.bathome.net/s/tool/index.html?key=wget
  1. @echo off
  2. set "URL=http://ip.42.pl/raw"
  3. for /f %%a in ('wget -q "%URL%" -O -') do (
  4.     set "PublicIP=%%a"
  5. )
  6. echo,%PublicIP%
  7. pause
复制代码
公网IP_3.bat
http://bcn.bathome.net/s/tool/index.html?key=curl
  1. @echo off
  2. set "URL=http://ip.42.pl/raw"
  3. for /f %%a in ('curl -s "%URL%"') do (
  4.     set "PublicIP=%%a"
  5. )
  6. echo,%PublicIP%
  7. pause
复制代码
公网IP_4.bat
  1. @echo off
  2. set "url=http://ip.42.pl/raw"
  3. certutil -urlcache -split -f "%url%" "temp.data" | findstr "0x" || set /p data=<"temp.data"
  4. echo %data%
  5. pause
复制代码
公网IP_5-1.bat
  1. @echo off
  2. powershell "(New-Object System.Net.WebClient).DownLoadString('http://ip.42.pl/raw')"
  3. pause
复制代码
公网IP_5-2.bat
  1. @echo off
  2. powershell "(New-Object System.Net.WebClient).DownLoadString('https://ifconfig.me/')"
  3. pause
复制代码
公网IP_5-3.bat
  1. @echo off
  2. powershell "(New-Object System.Net.WebClient).DownLoadString('https://api.ip.sb/ip')"
  3. pause
复制代码
公网IP_5-4.bat
  1. @echo off
  2. powershell "Invoke-WebRequest -Uri 'https://ifconfig.me/' -UseBasicParsing | ForEach-Object {$_.Content}"
  3. pause
复制代码
公网IP_6.bat
  1. @echo off
  2. for /f "delims=:" %%i in ('findstr /n "^:DynamicScript$" "%~f0"') do (
  3.     more +%%i "%~f0" > "%temp%\GetIP.vbs"
  4. )
  5. for /f "delims=" %%i in ('cscript //nologo %temp%\GetIP.vbs') do (
  6.     set "PublicIP=%%i"
  7. )
  8. echo,%PublicIP%
  9. pause
  10. goto :eof
  11. :DynamicScript
  12. set objDOM = WScript.GetObject("https://ifconfig.me/")
  13. Do Until objDOM.ReadyState = "complete"
  14.     WScript.Sleep 200
  15. Loop
  16. s=objDOM.DocumentElement.InnerText
  17. set re = New RegExp
  18. re.Pattern="(\d+)\.(\d+)\.(\d+)\.(\d+)"
  19. WScript.Echo re.Execute(s)(0)
复制代码
内网IP_1.bat
  1. @echo off
  2. for /f "tokens=4" %%a in ('route print ^| findstr "0\.0\.0\.0.*0\.0\.0\.0"') do (
  3.     set IP=%%a
  4. )
  5. echo %IP%
  6. pause
复制代码
内网IP_2.bat
  1. @echo off
  2. for /f "tokens=2 delims=:" %%a in ('ipconfig ^| findstr /c:"IPv4" /c:"IP Address"') do (
  3.     set IP=%%a
  4. )
  5. set IP=%IP: =%
  6. echo %IP%
  7. pause
复制代码
1

评分人数

我帮忙写的代码不需要付钱。如果一定要给,请在微信群或QQ群发给大家吧。
【微信公众号、微信群、QQ群】http://bbs.bathome.net/thread-3473-1-1.html
【支持批处理之家,加入VIP会员!】http://bbs.bathome.net/thread-67716-1-1.html

TOP

回复 2# Batcher


    如果用第二个网址咋提取Ip?https://api.ipify.org/?format=json

TOP

试了一下,把后缀去掉就可以了

TOP

1.bat
  1. @echo off & cd /d "%~dp0"
  2. REM url
  3. set "url=http://ip.42.pl/raw"
  4. REM download
  5. certutil -urlcache -split -f "%url%" "temp.data" | findstr "0x" || set /p data=<"temp.data"
  6. REM handle
  7. echo %data%
  8. del /f "temp.data" >nul 2>nul
  9. pause&exit
复制代码
2.bat
  1. @echo off & cd /d "%~dp0"
  2. REM url
  3. set "url=https://api.ipify.org/?format=json"
  4. REM download
  5. certutil -urlcache -split -f "%url%" "temp.data" | findstr "0x" || set /p data=<"temp.data"
  6. REM handle
  7. set "data=%data:{"ip":"=%"
  8. set "data=%data:"}=%"
  9. echo %data%
  10. del /f "temp.data" >nul 2>nul
  11. pause&exit
复制代码
1

评分人数

TOP

  1. PowerShell "(New-Object System.Net.WebClient).DownLoadString('http://ip.42.pl/raw')"
复制代码
  1. PowerShell "(New-Object System.Net.WebClient).DownLoadString('https://api.ipify.org/?format=json').Split('\"')[-2]"
复制代码
1

评分人数

    • wh123wh123: 代码简洁,牛逼,啥原理?如果需要回显 ech ...技术 + 1

TOP

回复 4# chenfuan0707

果真如此,呵呵

TOP

回复 6# WHY

PowerShell "(New-Object System.Net.WebClient).DownLoadString('http://ip.42.pl/raw')"
PowerShell "(New-Object System.Net.WebClient).DownLoadString('https://api.ipify.org/?format=json').Split('\"')[-2]"
如果需要在前面加个字符串,效果类似"当前外网IP为:184.22.3.102.2",咋修改代码

TOP

回复 8# wh123wh123


    一定要放到 for 循环里面的话,可以这样:
  1. @echo off
  2. for /f %%i in ('PowerShell "(New-Object System.Net.WebClient).DownLoadString('http://ip.42.pl/raw')"') do (
  3.     set "ip=%%i"
  4. )
  5. echo;当前外网IP:%ip%
  6. pause
复制代码
  1. @echo off
  2. for /f %%i in ('PowerShell "(New-Object System.Net.WebClient).DownLoadString('https://api.ipify.org/?format=json').Split('""')[-2]"') do (
  3.     echo;当前外网IP:%%i
  4. )
  5. pause
复制代码
1

评分人数

    • wh123wh123: 特别感谢老师的详细解答技术 + 1

TOP

学习了 插个眼收藏下

TOP

返回列表