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.

CJ7
  • 1,139
  • 15
  • 43
  • 57
  • Related: http://superuser.com/q/414050/79358 – Diogo May 09 '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 04:06
  • It will probably always work (barring really oddball cases, such as COMPUTERNAME being set to something other than the computer's name) but it won't tell you if there is more than one IP address, and it might not always give you the one you were after. For example, on my box, it shows me the IPv6 address, not the IPv4 address. – Harry Johnston May 11 '12 at 05:13
  • @Harry `ping -4 %COMPUTERNAME%` – Luke Oct 29 '12 at 21:59

4 Answers4

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
u1686_grawity
  • 426,297
  • 64
  • 894
  • 966
  • 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 00:57
  • 1
    It isn't being sent over the network. What makes you think that it is? – Harry Johnston May 10 '12 at 02:04
  • @HarryJohnston: because it's showing the router-assigned IP number and not `127.0.0.1`. – CJ7 May 10 '12 at 04: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 04: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 08:42
  • @CraigJ: None. (As you can see from the route table, both addresses go over the loopback interface.) Unless *you* have noticed any unusual differences before posting this question, although again, there shouldn't be any. – u1686_grawity May 10 '12 at 09:03
  • @CraigJ: a single physical interface can have more than one logical IP address. When you ping a particular address, the interface uses that address to reply; so if you ping 127.0.0.1, the reply comes from 127.0.0.1; if you ping 127.1.2.3 the reply comes from 127.1.2.3; if you ping the router-assigned address, the reply comes from that address. – Harry Johnston May 10 '12 at 21:50
  • Names are associated with specific addresses. In particular, the name localhost always means 127.0.0.1, and the name of the computer is translated to the computer's primary IP address, which in this case is the address assigned by the router. – Harry Johnston May 10 '12 at 21:51
  • There is very obviously a difference in the output of `ping localhost` and `ping %computername%`. I'm not 100% sure, but is it possible the difference is that with `ping %computername%` windows first uses NetBIOS to resolve the hostname to an IP address (check `nbtstat -n`) and then pings that, whereas `localhost` goes straight to the hosts file / DNS cache and finds 127.0.0.1 as the IP address to ping. Combined with Harry's answer 2 above mine this would explain the difference in output of the commands. – Luke Oct 29 '12 at 22:13
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.

ChrisF
  • 41,278
  • 17
  • 101
  • 154
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.

SecurityMatt
  • 3,140
  • 16
  • 21
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>

Jesse
  • 46
  • 1
  • Could you [edit] your answer to include some example output? – Burgi May 05 '16 at 01: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 05 '16 at 08:29