3

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

localhost

Can I do something like the following?

ping %localhost%

When I do:

ping localhost

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

7
  • Related: superuser.com/q/414050/79358
    – Diogo
    May 9, 2012 at 11:35
  • What are you actually trying to achieve by doing this? May 10, 2012 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, 2012 at 22:10
  • "ipconfig /all" is the most reliable way of doing that. But "ping %COMPUTERNAME%" will work most of the time. May 10, 2012 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, 2012 at 4:06

4 Answers 4

7

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. . . . . . . . . . . . : 192.168.1.223
        IP Address. . . . . . . . . . . . : fe80::202:2dff:fe6b:c71c%6

C:\>route print | findstr "Netmask 127.0.0.1"
Network Destination        Netmask          Gateway       Interface  Metric
        127.0.0.0        255.0.0.0        127.0.0.1       127.0.0.1       1
    192.168.1.223  255.255.255.255        127.0.0.1       127.0.0.1       30
9
  • 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, 2012 at 0:57
  • 1
    It isn't being sent over the network. What makes you think that it is? May 10, 2012 at 2:04
  • @HarryJohnston: because it's showing the router-assigned IP number and not 127.0.0.1.
    – CJ7
    May 10, 2012 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. May 10, 2012 at 4:51
  • @HarryJohnston: can you explain the difference, then, between doing ping localhost which returns 127.0.0.1 and ping %computername% which returns the router-assigned IP number?
    – CJ7
    May 10, 2012 at 8:42
1

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 . . . . . . . . . . . : 255.255.255.0
Default Gateway . . . . . . . . . : <IP Address>

Use ping -a localhost to find the machine name.

-a Resolve addresses to hostnames.

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

ping localhost

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

localhost is defined to be 127.0.0.1.

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.

0

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>

2
  • Could you edit your answer to include some example output?
    – Burgi
    May 5, 2016 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, 2016 at 8:29

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .