Board logo

标题: [文本处理] 批处理怎样实现外网ip提取问题 [打印本页]

作者: wh123wh123    时间: 2021-2-24 20:07     标题: 批处理怎样实现外网ip提取问题

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

回复 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
复制代码

作者: wh123wh123    时间: 2021-2-25 01:08

回复 2# Batcher


    如果用第二个网址咋提取Ip?https://api.ipify.org/?format=json
作者: chenfuan0707    时间: 2021-2-25 09:34

试了一下,把后缀去掉就可以了
作者: went    时间: 2021-2-25 10:34

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
复制代码

作者: WHY    时间: 2021-2-25 16:36

  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]"
复制代码

作者: wh123wh123    时间: 2021-2-25 17:00

回复 4# chenfuan0707

果真如此,呵呵
作者: wh123wh123    时间: 2021-2-25 17:19

回复 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",咋修改代码
作者: WHY    时间: 2021-2-25 17:41

回复 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
复制代码

作者: cc5511    时间: 2021-2-27 02:47

学习了 插个眼收藏下




欢迎光临 批处理之家 (http://www.bathome.net/) Powered by Discuz! 7.2