Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

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.

share|improve this question
    
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

3 Answers 3

up vote 7 down vote accepted

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

Use ping -a localhost to find the machine name.

-a Resolve addresses to hostnames.

share|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 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.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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