Batch file 将第二行输出存储在变量中
我正在制作一个小脚本,让用户将公共/外部IP地址发送到cmdBatch file 将第二行输出存储在变量中,batch-file,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:\...>typ
就是这样:
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
使用带有循环forindo的批处理文件,您可以获得所需的:
@echo关闭
对于/f“tokens=2 delims=:”%%A in(
'nslookup myip.opendns.com.resolver1.opendns.com 2^>NUL^|查找“地址:”
)是否设置“ExtIP=%%A”
回显%ExtIP%&暂停
下面是另一个要获取的批处理脚本:
@echo关闭
2017年Hackoo获得的标题(局域网、公共)(IP)和MAC地址
模式con cols=80行=5和颜色9E
回音(&E)(
echo请稍候…正在搜索(LAN、公用)(IP)和MAC地址。。。
设置“日志文件=%~dpn0.txt”
@对于/f“delims=[]令牌=2”%%a in('ping-4-n 1%ComputerName%^ findstr[')do(
设置“LAN_IP=%%a”
)
对于/f“tokens=2 delims=:”%%A in(
'nslookup myip.opendns.com.resolver1.opendns.com 2^>NUL^|查找“地址:”
)是否设置ExtIP=%%A
@对于('getmac/NH/FO表')中的/f%%a,请执行以下操作(
@对于('echo%%a')中的/f%%b,请执行以下操作(
如果/I不是“%%b”=“不适用”(
设置“MY_MAC=%%b”
)
)
)
Cls
回音(
回显我的专用LAN IP:%LAN\U IP%
回显我的外部公共IP:%ExtIP%
echo MAC地址:%MY\u MAC%
(
回显我的专用LAN IP:%LAN\U IP%
回显我的外部公共IP:%ExtIP%
回显MAC地址:%MY\u MAC%
)>%LogFile%
超时/T 5/NoBreak>nul
启动“”%LogFile%”
使用带有循环forindo的批处理文件,您可以得到想要的:
@echo关闭
对于/f“tokens=2 delims=:”%%A in(
'nslookup myip.opendns.com.resolver1.opendns.com 2^>NUL^|查找“地址:”
)是否设置“ExtIP=%%A”
回显%ExtIP%&暂停
下面是另一个要获取的批处理脚本:
@echo关闭
2017年Hackoo获得的标题(局域网、公共)(IP)和MAC地址
模式con cols=80行=5和颜色9E
回音(&E)(
echo请稍候…正在搜索(LAN、公用)(IP)和MAC地址。。。
设置“日志文件=%~dpn0.txt”
@对于/f“delims=[]令牌=2”%%a in('ping-4-n 1%ComputerName%^ findstr[')do(
设置“LAN_IP=%%a”
)
对于/f“tokens=2 delims=:”%%A in(
'nslookup myip.opendns.com.resolver1.opendns.com 2^>NUL^|查找“地址:”
)是否设置ExtIP=%%A
@对于('getmac/NH/FO表')中的/f%%a,请执行以下操作(
@对于('echo%%a')中的/f%%b,请执行以下操作(
如果/I不是“%%b”=“不适用”(
设置“MY_MAC=%%b”
)
)
)
Cls
回音(
回显我的专用LAN IP:%LAN\U IP%
回显我的外部公共IP:%ExtIP%
echo MAC地址:%MY\u MAC%
(
回显我的专用LAN IP:%LAN\U IP%
回显我的外部公共IP:%ExtIP%
回显MAC地址:%MY\u MAC%
)>%LogFile%
超时/T 5/NoBreak>nul
启动“”%LogFile%”
< /代码> 不看到代码的完整输出,您应该考虑只使用“强”> Internet地址< /强>您的搜索字符串。然后可以使用AO命令捕获IP地址。
这可以从命令行运行。如果在批处理文件中需要它,请将每个命令帮助文件的百分比符号增加一倍
FOR /F "tokens=3 delims== " %G IN ('nslookup -debug myip.opendns.com. resolver1.opendns.com 2^>nul^| find /I "internet address"') do echo %G
未看到代码的完整输出,应考虑仅使用“强”> Internet地址< /强>搜索字符串。然后可以使用AO命令捕获IP地址。
这可以从命令行运行。如果在批处理文件中需要它,请将每个命令帮助文件的百分比符号增加一倍
FOR /F "tokens=3 delims== " %G IN ('nslookup -debug myip.opendns.com. resolver1.opendns.com 2^>nul^| find /I "internet address"') do echo %G
@回音
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
如果“%~1”=-h”转到:帮助
如果“%~1”==“-help”转到:help
如果“%~1”==“/?”转到:帮助
键入nul>%temp%\\u W\u\u getpipData.log
键入nul>%temp%\\u W\u\u getpipDataL.log
键入nul>%temp%\\u W\u\u getpipDataN.log
\nslookup.emod myip.opendns.com resolver1.opendns.com |查找/I“地址”>%temp%\\u W\uu\u getpipDataN.log
更多+01“%temp%\\u W\uuu getpipDataN.log”>%temp%\\u W\uu getpipDataL.log”
del/F/Q“%temp%\\u W\u\u getpipDataN.log”
键入“%temp%\\u W\uuuu getpipDataL.log”\124;查找/I“地址”>%temp%\\u W\uuu getpipData.log
del/F/Q“%temp%\\u W\u\u getpipDataL.log”
设置/p GPIPLOG=@echo关闭
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
如果“%~1”=-h”转到:帮助
如果“%~1”==“-help”转到:help
如果“%~1”==“/?”转到:帮助
键入nul>%temp%\\u W\u\u getpipData.log
键入nul>%temp%\\u W\u\u getpipDataL.log
键入nul>%temp%\\u W\u\u getpipDataN.log
\nslookup.emod myip.opendns.com resolver1.opendns.com |查找/I“地址”>%temp%\\u W\uu\u getpipDataN.log
更多+01“%temp%\\u W\uuu getpipDataN.log”>%temp%\\u W\uu getpipDataL.log”
del/F/Q“%temp%\\u W\u\u getpipDataN.log”
键入“%temp%\\u W\uuuu getpipDataL.log”\124;查找/I“地址”>%temp%\\u W\uuu getpipData.log
del/F/Q“%temp%\\u W\u\u getpipDataL.log”
set/p GPIPLOG=您的查找不应该使用internet address
作为搜索字符串。是的,但是可以使用它!我的建议是您告诉我们您希望从初始命令返回的输出,而不是您认为您的命令可能意味着什么!在执行此操作时,请使用从裸命令中,nslookup myip.opendns.com resolver1.opendns.com
。您的代码应该得到3行输出。第一行是dns服务器的ip地址。第二行是internet地址,第三行也是internet地址。为什么要让自己更难。如果您想要internet地址ess在您的查找搜索字符串中使用它。@Squashman,在我的电脑上运行该命令会输出五行可解析的行,其中两行以地址开头:
您的查找不应该使用互联网地址
作为搜索字符串。是的,但可以使用它!我的建议是,您告诉我们您想要从中返回的输出最初的命令,而不是你认为你的命令可能意味着什么