Union site: www.cn-dos.net Forum site: www.cn-dos.net/forum
DOS stands for freedom, openness and progress. Let us work hard, learn from the openness and GNU spirit of FreeDOS and Linux, and together build and grow a free GNU GPL world!
Credits 55 Posts 29 Joined 2006-04-11 16:40 20-year member UID 53669 Gender Male From 亚洲
Status Offline
@echo off&setlocal enabledelayedexpansion
for /f "usebackq tokens=2 delims=:" %%i in (`ipconfig ^| find "Default Gateway"`) do set gw=%%i
for /f "usebackq tokens=2 delims= " %%i in (`arp -a ^| findstr "\<!gw!\>"`) do echo %%i
pause
Why can getting the MAC address of the gateway work when using `findstr "\<10.0.0.1\>"`
but not when using the delayed variable !gw!?
Credits 1,039 Posts 897 Joined 2009-03-01 15:34 17-year member UID 140302 Gender Male From 在地狱中仰望天堂
Status Offline
Look at my place http://www.cn-dos.net/forum/viewthread.php?tid=52933&fpage=2
The explanation on floor 11!~
It's because you have already brought the space in when setting gw=%%i
The result is "space + Default Gateway"
The correct way should be written like this:
@echo off
for /f "tokens=2 delims=:" %%i in (`ipconfig ^| find "Default Gateway"`) do set gw=%%i
for /f "tokens=2 delims= " %%i in (`arp -a ^| findstr "\<%gw:~1%\>"`) do echo %%i
pause
%gw:~1% is used to remove the leading space that was brought in
Recent Ratings for This Post
( 1 in total)Click for details
Credits 55 Posts 29 Joined 2006-04-11 16:40 20-year member UID 53669 Gender Male From 亚洲
Status Offline
Originally posted by Hanyeguxing at 2010-12-6 15:12:
Check the explanation on floor 11 of http://www.cn-dos.net/forum/viewthread.php?tid=52933&fpage=2!~
You already included the space when you did set gw=%%i
The result is "space + Default Gateway ...
Hmm, inserting call can also be used to call, learned~
Credits 55 Posts 29 Joined 2006-04-11 16:40 20-year member UID 53669 Gender Male From 亚洲
Status Offline
for /f "usebackq tokens=2 delims= " %%i in (`arp -a ^| findstr "\<call !gw!\>"`) do echo %%i
for /f "usebackq tokens=2 delims= " %%i in (`arp -a ^| findstr "\<!gw:~1!\>"`) do echo %%i