0

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
Albert F D
  • 529
  • 2
  • 13
  • 33
  • Please, get one thing straight immediately: Batch scripts **are not** MS-DOS. Just because they're white text on a black background doesn't mean it's DOS. DOS hasn't been included with Windows for several generations now. Batch files run under the *command interpreter*. – Jonathon Reinhart Mar 13 '15 at 17:59
  • Thanks, I've been labouring under that false idea for a while now. I have edited my question accordingly. I will do the same in my profile. – Albert F D Mar 13 '15 at 18:02
  • It's a common misconception, so don't feel bad. But it very much so needs to be broken. – Jonathon Reinhart Mar 13 '15 at 18:03
  • I agree, I'm glad you cleared that up, however it wouldn't be wrong of me to think that cmd.exe maintains most of the same commands and some compatibility with DOS, right? – Albert F D Mar 13 '15 at 18:06
  • 1
    The vast majority of DOS commands also exist in batch. 64-bit operating systems aren't capable of using the `debug` command, and there are several new commands in batch that aren't in DOS (various flags for the `for` command, for example), but by and large they're mostly the same. – SomethingDark Mar 13 '15 at 18:31
  • 2
    Also, ping shouldn't return a MAC address. Are you sure it's not just an IPv6 value? Try `ping -4` and see if that produces a result you're looking for. – SomethingDark Mar 13 '15 at 18:44
  • Thanks! It works! ...will post the working script ASAP. Thanks again SomethingDark. (I voted your comment as useful, for lack of marking it as the answer option). – Albert F D Mar 13 '15 at 19:24

1 Answers1

0

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!

Albert F D
  • 529
  • 2
  • 13
  • 33
  • It's also worth mentioning (for any novices like me, who may encounter similar issues). I discovered that by disabling IPv6 in my wireless network adapter settings (the means by which I'm connected to my LAN), I no longer get an IPv6 address when pinging a computer-name but the desired IPv4. – Albert F D Mar 14 '15 at 07:11