How to grab IPV4 variable in CMD and Ping in new C

2019-05-20 05:46发布

问题:

So here is the idea, I am trying to grab a variable from Cmd such as IPv4 and be able to ping it in a new window.

What would the logic be by running a command and grabbing a specific part of that command and be able to input it as a new vairable in a new command line so it can perform an action.

Another example :

  1. Find computer name.
  2. Run command to a number at the end of the Computer name.

I am currently downloading Visual Basics software as it looks like it may be the language i may need but very confused.

Does anyone have any basic idea as to what i can use or have an example of this ?

回答1:

You can try something like that :

@Echo off
Color 0A & Mode con cols=80 lines=15
Title How to grab IPv4
::**********************************
::Method1
::**********************************
echo( 
echo Method 1 with arp -a
echo( 
For /f "tokens=2 delims= " %%a in ('arp -a ^|findstr /i "Interface"') do (set IP=%%a)
Echo The Local IPv4 adress is : %IP%
pause
Cls
Call:PingHost %IP%
pause
::**********************************
::Method2
::**********************************
cls
echo( 
echo Method 2 with ipconfig
for /f "tokens=1-2 delims=:" %%a in ('ipconfig^|find "IPv4"') do (
  set ip=%%b
)
set ip=%ip:~1%
echo( 
echo The Local IPv4 adress is : %ip%
pause
Cls
Call:PingHost %IP%
pause
::**********************************
::Method3 by Stephan
::**********************************
cls
echo( 
echo Method 3 with %%computername%% = %computername%
for /f "tokens=2 delims=[]" %%i in ('ping -4 -n 1 %computername%') do set ip=%%i
echo( 
echo The Local IPv4 adress is : %ip%
pause
Cls
Call:PingHost %IP%
Pause
exit
::**********************************
:Pinghost
Start "%1" Ping %1
exit /b
::**********************************