将第二行输出存储在变量中

时间:2017-11-28 17:53:11

标签: batch-file

我正在创建一个小脚本来将用户的公共/外部IP地址提供给cmd!
所以它是这样的:

C:\...>nslookup myip.opendns.com resolver1.opendns.com | find /I "Address" > %temp%\_W___1.log

C:\...>set /p GPIPLOG=<%temp%\_W___1.log

C:\...>echo %gpiplog%
Address:  208.67.222.222

C:\...>type %temp%\_W___1.log
Address:  208.67.222.222
Address:  X.X.X.X

C:\...>echo %gpiplog:~2,50%
dress:  208.67.222.222

我认为echo %gpiplog:~2,50%会跳到第二行并显示所有内容,直到它达到50个字符,但我错了!

我想要的:< / p>

C:\...>echo %gpiplog:~2,10,50%
X.X.X.X

3 个答案:

答案 0 :(得分:0)

使用带有循环forindo的批处理文件,您可以获得所需内容:

@echo off
for /f "tokens=2 delims=: " %%A in (
  'nslookup myip.opendns.com. resolver1.opendns.com 2^>NUL^|find "Address:"'
) Do set "ExtIP=%%A"

Echo %ExtIP% & pause

这是另一个要获取的批处理脚本:

  1. 专用LAN IP(ipv4)

  2. 外部公共知识产权

  3. MAC地址

  4. @echo off
    Title Get (LAN ,Public) (IP) and MAC Addresses by Hackoo 2017
    mode con cols=80 lines=5 & Color 9E
    echo( & echo(
    echo   Please Wait a While ... Searching for (LAN ,Public)(IP) and MAC addresses ...
    Set "LogFile=%~dpn0.txt"
    @for /f "delims=[] tokens=2" %%a in ('ping -4 -n 1 %ComputerName% ^| findstr [') do (
        set "LAN_IP=%%a"
    )
    
    for /f "tokens=2 delims=: " %%A in (
      'nslookup myip.opendns.com. resolver1.opendns.com 2^>NUL^|find "Address:"'
    ) Do set ExtIP=%%A
    
    
    @For /f %%a in ('getmac /NH /FO Table') do  (
        @For /f %%b in ('echo %%a') do (
            If /I NOT "%%b"=="N/A" (
                Set "MY_MAC=%%b"
            )
        )
    )
        Cls 
        echo(
        echo                My Private LAN IP       : %LAN_IP%
        echo                My External Public IP   : %ExtIP%
        echo                MAC Addres              : %MY_MAC%
    
    (
        echo My Private LAN IP      : %LAN_IP%
        echo My External Public IP  : %ExtIP%
        echo MAC Address            : %MY_MAC%
    
    )>"%LogFile%"
    Timeout /T 5 /NoBreak>nul
    Start "" "%LogFile%"
    

答案 1 :(得分:0)

如果没有看到代码的完整输出,您应该考虑使用互联网地址作为搜索字符串。然后,您可以使用FOR命令捕获IP地址。

这可以从命令行运行。如果在批处理文件中需要它,请将每个FOR命令帮助文件的百分比符号加倍。

FOR /F "tokens=3 delims== " %G IN ('nslookup -debug myip.opendns.com. resolver1.opendns.com 2^>nul^| find /I "internet address"') do echo %G

答案 2 :(得分:0)

:: getpip
:: Nurudin Imsirovic Copyright
:: Much thanks OpenDNS ;)
@echo off
cd %~dp0

if "%~1"=="-h" goto :help
if "%~1"=="-help" goto :help
if "%~1"=="/?" goto :help

type nul > %temp%\_W___getpipData.log
type nul > %temp%\_W___getpipDataL.log
type nul > %temp%\_W___getpipDataN.log

.\nslookup.emod myip.opendns.com resolver1.opendns.com | find /I "Address" > "%temp%\_W___getpipDataN.log"
more +01 "%temp%\_W___getpipDataN.log" > "%temp%\_W___getpipDataL.log"
del /F /Q "%temp%\_W___getpipDataN.log"
type "%temp%\_W___getpipDataL.log" | find /I "Address" > "%temp%\_W___getpipData.log"
del /F /Q "%temp%\_W___getpipDataL.log"
set /p GPIPLOG=<"%temp%\_W___getpipData.log"
echo %GPIPLOG:~10,50%
set /p GPIPLOG=
del /F /Q "%temp%\_W___getpipData.log"
exit /B

:help
echo.getpip - Get Public IP
echo.
echo.Usage: getpip
exit /B

感谢https://stackoverflow.com/users/7090116/nurudin-imsirovic代码!