标题: [文本处理] 批处理怎样实现外网ip提取问题 [打印本页]
作者: wh123wh123 时间: 2021-2-24 20:07 标题: 批处理怎样实现外网ip提取问题
看到论坛说http://ip.42.pl/raw 和 https://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- @echo off
- set "URL=http://ip.42.pl/raw"
- >%temp%\download.vbs echo Set objDOM = WScript.GetObject(WScript.Arguments(0))
- >>%temp%\download.vbs echo Do Until objDOM.ReadyState = "complete"
- >>%temp%\download.vbs echo WScript.Sleep 100
- >>%temp%\download.vbs echo Loop
- >>%temp%\download.vbs echo WScript.Echo objDOM.DocumentElement.OuterText
- for /f %%i in ('cscript //nologo "%temp%\download.vbs" "%URL%"') do (
- set "PublicIP=%%i"
- )
- echo,%PublicIP%
- pause
复制代码
公网IP_2.bat
http://bcn.bathome.net/s/tool/index.html?key=wget- @echo off
- set "URL=http://ip.42.pl/raw"
- for /f %%a in ('wget -q "%URL%" -O -') do (
- set "PublicIP=%%a"
- )
- echo,%PublicIP%
- pause
复制代码
公网IP_3.bat
http://bcn.bathome.net/s/tool/index.html?key=curl- @echo off
- set "URL=http://ip.42.pl/raw"
- for /f %%a in ('curl -s "%URL%"') do (
- set "PublicIP=%%a"
- )
- echo,%PublicIP%
- pause
复制代码
公网IP_4.bat- @echo off
- set "url=http://ip.42.pl/raw"
- certutil -urlcache -split -f "%url%" "temp.data" | findstr "0x" || set /p data=<"temp.data"
- echo %data%
- pause
复制代码
公网IP_5-1.bat- @echo off
- powershell "(New-Object System.Net.WebClient).DownLoadString('http://ip.42.pl/raw')"
- pause
复制代码
公网IP_5-2.bat- @echo off
- powershell "(New-Object System.Net.WebClient).DownLoadString('https://ifconfig.me/')"
- pause
复制代码
公网IP_5-3.bat- @echo off
- powershell "(New-Object System.Net.WebClient).DownLoadString('https://api.ip.sb/ip')"
- pause
复制代码
公网IP_5-4.bat- @echo off
- powershell "Invoke-WebRequest -Uri 'https://ifconfig.me/' -UseBasicParsing | ForEach-Object {$_.Content}"
- pause
复制代码
公网IP_6.bat- @echo off
- for /f "delims=:" %%i in ('findstr /n "^:DynamicScript$" "%~f0"') do (
- more +%%i "%~f0" > "%temp%\GetIP.vbs"
- )
- for /f "delims=" %%i in ('cscript //nologo %temp%\GetIP.vbs') do (
- set "PublicIP=%%i"
- )
- echo,%PublicIP%
- pause
- goto :eof
-
- :DynamicScript
- set objDOM = WScript.GetObject("https://ifconfig.me/")
- Do Until objDOM.ReadyState = "complete"
- WScript.Sleep 200
- Loop
- s=objDOM.DocumentElement.InnerText
- set re = New RegExp
- re.Pattern="(\d+)\.(\d+)\.(\d+)\.(\d+)"
- WScript.Echo re.Execute(s)(0)
复制代码
内网IP_1.bat- @echo off
- for /f "tokens=4" %%a in ('route print ^| findstr "0\.0\.0\.0.*0\.0\.0\.0"') do (
- set IP=%%a
- )
- echo %IP%
- pause
复制代码
内网IP_2.bat- @echo off
- for /f "tokens=2 delims=:" %%a in ('ipconfig ^| findstr /c:"IPv4" /c:"IP Address"') do (
- set IP=%%a
- )
- set IP=%IP: =%
- echo %IP%
- 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- @echo off & cd /d "%~dp0"
- REM url
- set "url=http://ip.42.pl/raw"
- REM download
- certutil -urlcache -split -f "%url%" "temp.data" | findstr "0x" || set /p data=<"temp.data"
- REM handle
- echo %data%
- del /f "temp.data" >nul 2>nul
- pause&exit
复制代码
2.bat- @echo off & cd /d "%~dp0"
- REM url
- set "url=https://api.ipify.org/?format=json"
- REM download
- certutil -urlcache -split -f "%url%" "temp.data" | findstr "0x" || set /p data=<"temp.data"
- REM handle
- set "data=%data:{"ip":"=%"
- set "data=%data:"}=%"
- echo %data%
- del /f "temp.data" >nul 2>nul
- pause&exit
复制代码
作者: WHY 时间: 2021-2-25 16:36
- 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]"
复制代码
作者: 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 循环里面的话,可以这样:- @echo off
- for /f %%i in ('PowerShell "(New-Object System.Net.WebClient).DownLoadString('http://ip.42.pl/raw')"') do (
- set "ip=%%i"
- )
- echo;当前外网IP:%ip%
- pause
复制代码
- @echo off
- for /f %%i in ('PowerShell "(New-Object System.Net.WebClient).DownLoadString('https://api.ipify.org/?format=json').Split('""')[-2]"') do (
- echo;当前外网IP:%%i
- )
- pause
复制代码
作者: cc5511 时间: 2021-2-27 02:47
学习了 插个眼收藏下
欢迎光临 批处理之家 (http://bbs.bathome.net/) |
Powered by Discuz! 7.2 |