I would like to be able to ping whatever machine name I am in without first having to do:


Can I do something like the following?

ping %localhost%

When I do:

ping localhost

the IP number is which is not what I want. I want the IP number assigned by my router.

  • Related: superuser.com/q/414050/79358 – Diogo May 9 '12 at 11:35
  • What are you actually trying to achieve by doing this? – Harry Johnston May 10 '12 at 21:55
  • @HarryJohnston: what it achieves is it reveals the Computer Name and the IP Address of the current machine in one command line statement. – CJ7 May 10 '12 at 22:10
  • "ipconfig /all" is the most reliable way of doing that. But "ping %COMPUTERNAME%" will work most of the time. – Harry Johnston May 10 '12 at 23:22
  • @HarryJohnston: I don't like the way ipconfig /all runs off the default sized cmd window and so you have to scroll up to get what you need. When do think ping %computername% would not work? – CJ7 May 11 '12 at 4:06

You could use %COMPUTERNAME% on Windows.

But it really should not matter, since the packets will never be sent over the network. When you ping the computer's own address – any address, whether loopback or not – Windows recognizes this and loops back the packets inside the OS. It would simply be impractical for it to do otherwise.

On Windows, you can confirm this by reading the route table from route print – pay attention to the "Gateway" column:

C:\>ipconfig | findstr "Address"
        IP Address. . . . . . . . . . . . :
        IP Address. . . . . . . . . . . . : fe80::202:2dff:fe6b:c71c%6

C:\>route print | findstr "Netmask"
Network Destination        Netmask          Gateway       Interface  Metric       1       30
| improve this answer | |
  • When I do ping %COMPUTERNAME% it shows the IP address assigned by the router. So you are correct in suggesting to do that, but I cannot accept your answer unless you take out the bit where you say it will never be sent over the network. – CJ7 May 10 '12 at 0:57
  • 1
    It isn't being sent over the network. What makes you think that it is? – Harry Johnston May 10 '12 at 2:04
  • @HarryJohnston: because it's showing the router-assigned IP number and not – CJ7 May 10 '12 at 4:43
  • And ...? The computer remembers the IP address it is given, it doesn't have to contact the router each time to remind itself. – Harry Johnston May 10 '12 at 4:51
  • @HarryJohnston: can you explain the difference, then, between doing ping localhost which returns and ping %computername% which returns the router-assigned IP number? – CJ7 May 10 '12 at 8:42

Use ipconfig to find the IP address assigned to you by your router:

Windows IP Configuration

Ethernet adapter Local Area Connection:

Connection-specific DNS Suffix . : <domain>.local
Link-local IPv6 Address . . . . . : <IPv6 address>
IPv4 Address. . . . . . . . . . . : <IPv4 address>
Subnet Mask . . . . . . . . . . . :
Default Gateway . . . . . . . . . : <IP Address>

Use ping -a localhost to find the machine name.

-a Resolve addresses to hostnames.

| improve this answer | |
  • I was hoping for a way to do it in one statement on the command line. – CJ7 May 10 '12 at 0:55
  • @CraigJ - you could take the approach in grawity's answer and pipe the results through findstr. – ChrisF May 10 '12 at 7:54

ping localhost

the IP number is which is not what I want. I want the IP number assigned by my router.

localhost is defined to be

If you want to ping yourself over the Internet, visit a website like http://www.whatismyip.com/ and type ping ip-address where ip-address is the address you read from the website.

| improve this answer | |

To do this in a single command run nbtstat -n

nbtstat is a windows NetBIOS tool. I used to use it to get the computername of other computers from their IP using netbios -A <IPAddress>

| improve this answer | |
  • Could you edit your answer to include some example output? – Burgi May 5 '16 at 1:47
  • While this may answer the question, it would be a better answer if you could provide some explanation why it does so - as to me it does not seem to answer the question which is about ping. – DavidPostill May 5 '16 at 8:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.