Question

This is probably an easy one but I couldn't find the answer and I haven't really done (obviously) much batch programming recently. All I want is to convert an hostname to its IP in a batch file.

Here is my script:

set /p ip="IP: " %=%
start "" "C:\Program Files (x86)\CtrlDistance SCCM\cmRcViewer.exe" %ip% \\sccm 
exit

Nothing really fancy, this .bat file just starts SCCM after the user entered the IP. However, SCCM in command line doesn't accept hostnames as a paramater, that's why I would like to automatically convert %ip% before sending it to the application.

Thank you.

Was it helpful?

Solution

example with localhost (not sure about ipv6 output):

@echo off
setlocal
    set "ipadr="
    ping -n 1 localhost 2>nul 1>&2 && (
        for /f "tokens=2 delims=[]" %%h in ('ping -n 1 localhost') do (
            set "ipadr=%%h"
            goto :break_loop
        )
    )
    :break_loop
    if defined ipadr ( 
        echo %ipadr% 
        rem you can start SCCM here
    ) else (
        echo host does not exist
    )
endlocal

OTHER TIPS

Final code:

@echo off
setlocal

if "%1"=="" (
    set /p host="IP: " %=%
) else (
    set host=%1
)

set "ip="
ping -n 1 %host% 2>nul 1>&2 && (
    for /f "tokens=2 delims=[]" %%h in ('ping -n 1 %host%') do (
        set "ip=%%h"
        goto :break_loop
    )
:break_loop
    if not defined ip ( set ip=%host% )
)

if defined ip ( 
    start "" "C:\Program Files (x86)\CtrlDistance SCCM\cmRcViewer.exe" %ip% \\sccm
) else (
    echo no_ping
    pause
)

endlocal
exit
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top