Find IP of computer, find IPs of all computers on LAN

16,494

Solution 1

You need to change the 10.0.1.% to what your IP set would be.

ie. this script won't work a 192.168.1.% network as is. For this set use:

updated

FOR /L %i IN (1,1,254) DO ping -n 1 10.0.1.%i | FIND /i "Reply">> c:\lanipaddresses.txt

Solution 2

For Linux machines, how about good old Nmap:

nmap -sP 192.168.2.* 

Solution 3

For Windows machines, how about good old arp:
arp -a

Obviously the question was FOR /L %Windows IN (3,1, 10)

Share:
16,494
JShoe
Author by

JShoe

Updated on June 22, 2022

Comments

  • JShoe
    JShoe almost 2 years

    I know how to find a computer's IP address, but how do I make a variable set as the IP address of that computer, or save it to a text file, all in a batch?

    Also, I found a line of code on the internet that would ping every possible IP address of a given server and list the IPs successfully pinged, but it didn't work; they all timed out. What would be wrong with it? Is there a better way to do it? So here's the code for that:

    FOR /L %i IN (1,1,254) DO ping --a --n 1 10.0.1.%i | FIND /I "Reply">> c:\lanipaddresses.txt
    

    Thanks!

  • JShoe
    JShoe about 13 years
    I'm really sorry, I forgot to mention I would enter the IP address each time and it still wouldn't work, using the correct IP.
  • RDL
    RDL about 13 years
    Try this one: FOR /L %i IN (1,1,254) DO ping -n 1 10.0.1.%i | FIND /i "Reply">> c:\ipaddresses.txt
  • JShoe
    JShoe about 13 years
    I'm seeing some problems already... I tried it and all the IP's in the list were of my IP address. So if my IP address is 0.0.0.27, they all listed as 0.0.0.27, but only the 27th one got a successful ping. I don't know what it would look like on a multiple IP server, as I am currently the only one online.
  • RDL
    RDL about 13 years
    I got the same thing. If it didn't get a response it would show my computers ip address, however when it found one it would show the corresponding IP for that computer. Here's a snippet of mine: Reply from 192.168.15.1: bytes=32 time=1ms TTL=64 Reply from 192.168.15.101: Destination host unreachable. Reply from 192.168.15.101: Destination host unreachable. ..snip.. Reply from 192.168.15.101: Destination host unreachable. Reply from 192.168.15.16: bytes=32 time=2ms TTL=128 Reply from 192.168.15.101: Destination host unreachable.
  • JShoe
    JShoe about 13 years
    Okay I got it almost completely working, I just need some more advice. 1. Is it normal for the x.x.x.1 to be active? Is that a device or just always active? 2. Is it possible to add some code that automatically finds and imports my IP address (minus the final seg.) and runs that as the reference? Or possibly saves it to a separate document and runs it from there?
  • RDL
    RDL about 13 years
    yes, x.x.x.1 is typically your router (it is in my case). I don't see why not. Perhaps running ipconfig and grabbing the IP from there however it would depend on how many network interfaces are on the computer (ie. might grab the wrong one).
  • Ben Voigt
    Ben Voigt about 13 years
    Depending on your netmask, neighbor computer IP address can vary from yours in more than just the last octet.
  • JShoe
    JShoe about 13 years
    Okay so how would I grab the IP from IPCONFIG using a batch? And Ben, what is the chance of it varying that much? Say, out of 100?
  • JShoe
    JShoe about 13 years
    Also: Why won't that line of code run correctly in a batch file?
  • JShoe
    JShoe about 13 years
    It says "IPi was unexpected at this time."
  • JShoe
    JShoe about 13 years
    This is my code: 'SET /P ip=Enter the first 3 segmants of your IP address? (x.x.x) FOR /L %i IN (1,1,254) DO ping -n 1 -w 1000 %ip%.%i | FIND /i "Reply">> f:\IPAddresses.txt' The problem has something to do with the %ip%.%i part.
  • JShoe
    JShoe about 13 years
    Hmm. I tried running the command like my question was originally answered alone in a batch file: '@echo off FOR /L %i IN (1,1,254) DO ping -n 1 10.0.1.%i | FIND /i "Reply">> c:\lanipaddresses.txt' and I got an "i was unexpected at this time error." Why would that be?
  • RDL
    RDL about 13 years
    Curious, what is the batch file for (why do you need it in a batch file)?
  • JShoe
    JShoe about 13 years
    Oh because I have it entered into another program that gives me information about my network, and I wanted this to be an option. So why would it work in CMD on not batch, and how do I make it work in batch?
  • RDL
    RDL about 13 years
    Go here: computing.net/answers/programming/… about half way down you will see a batch script someone put together. Might not work out of the box as noted, but should help give you direction.
  • RDL
    RDL about 13 years
    Just looks like you need to double up some of your '%' signs for escaping them to get a literal '%'.
  • JShoe
    JShoe about 13 years
    Thanks!!! It works great! Final question: To shorten the amount of time this takes, what should the -w be? to keep it safe.
  • RDL
    RDL about 13 years
    That can depend on a lot of things. I would leave it at the default for now. You could try shortening it but risk the chance of it not getting a response back quick enough for an IP that does in fact have something on it.