问题描述
我无法使用这个简单的代码来显示 <p>
/</p>
HTML 标记之间的 IP 地址。
if /i %opt%==1 ipconfig |findstr "IPv4">>"..\LOGS\%NAME%\%serial% %MAC%".txt
下面的代码是我想嵌入 CMD 代码的地方,但它只是忽略代码或以纯文本形式打印,具体取决于我如何修改它;它需要转义字符吗?
^<td width="38%%" class="table-border-left"^>^<p^>IP Address^</p^>^</td^>
>> "..\LOGS\NAME%\%serial% %MAC%".html
有什么建议吗?
解决方法
您的问题本质上是“我如何从 ipconfig.exe 获取 IPv4 地址并使用它”?
batch-file 示例使用您的 ipconfig 命令,(结果返回为 %%J
):
For /F "Delims=" %%G In ('%SystemRoot%\System32\ipconfig.exe
^| %SystemRoot%\System32\findstr.exe /R /C:"^[ ][ ]*IPv4[ ]"'
) Do For /F "Tokens=1,* Delims=:" %%H In ("%%G") Do For /F %%J In ("%%I"
) Do Echo ^<td width="38%%" class="table-border-left"^>^<p^>%%J^</p^>^</td^>
以上是单行命令,已拆分工作,无需修改,但仍保持可读性。如果你想将它作为一行运行,那么它看起来像这样:
For /F "Delims=" %%G In ('%SystemRoot%\System32\ipconfig.exe ^| %SystemRoot%\System32\findstr.exe /R /C:"^[ ][ ]*IPv4[ ]"') Do For /F "Tokens=1,* Delims=:" %%H In ("%%G") Do For /F %%J In ("%%I") Do Echo ^<td width="38%%" class="table-border-left"^>^<p^>%%J^</p^>^</td^>
请注意可能有多个适配器绑定到 TCP/IP,因此您可能会看到多个返回结果
,要创建有效的 HTML,我建议您使用 xidel 之类的工具,而不是摆弄 Batch(和大量转义字符)。
ipconfig | xidel -s --xquery "<td width='38%' class='table-border-left'><p>{extract($raw,'IPv4.+: (.+)',1)}</p></td>" --printed-node-format=html
或者,您可以使用 system()
从查询中调用 ipconfig
,而不是将 xidel
管道化到 ipconfig
。您还可以使用 serialize()
代替 --printed-node-format=html
。
xidel -s --xquery "serialize(<td width='38%' class='table-border-left'><p>{extract(system('cmd /c ipconfig'),1)}</p></td>)"