batch-fileipcomputer-name

Batch - Find network IP address of computer using computer name


I am a BEGINNER STUDENT of Batch scripting. The problem I have is that I am able to retrieve a computer name from my LAN by pinging its IP address (see code below). However, I'm struggling to find an equally simple way of getting the IP address of a computer on my network when using the computer name. As far as I can see pinging the computer name only provides a MAC ID but no IP.

The reason I require this function is for a small batch script I'm writing which prompts the user for either an IP OR computer name for messaging (msg.exe) purposes. Subsequent configurations (TTL checks and registry key changes) explicitly require one or the other.

@echo off
for /f "tokens=1,2" %%a in ('ping -a 192.168.1.33  ^| find "Pinging"') do set compname=%%b >nul
echo Computer name is %compname%
echo.
pause

Solution

  • Thanks to SomethingDark for the solution:

    @echo off
    for /f "tokens=1,2 delims=[]" %%a in ('ping -4 COMPNAME  ^| find "Pinging"') do set ipaddress=%%b >nul
    echo COMPNAME IP address is %ipaddress%
    echo.
    pause
    

    In fact by showing me the option:

     ping -4
    

    I can get away with finding "TTL=" state, by pinging the computer-name in this way. Hence I no longer need to convert computer-name to IP address, this will make my final script shorter. Thanks!