• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 2455
  • Last Modified:

Ping from (192.168.1.1 - 192.168.1.254) without using IPlist.txt and output the result with Hostnames..

Hi,

I want to ping all computers in the range of (192.168.1.1 - 192.168.1.254) without using the "IPlist.txt".

When done; the output - I want to see is only the PCs that (0% loss) .. with the PC's Hostname.

Any PCs that "100% loss", I don't want to see.

Thanks

Here is the output, I want it looks like on the screen.

Hostnames are availables in your LAN:
192.168.1.1      --> MyPC1          Packets: Sent = 1, Received = 1, Lost = 0 (0% loss),

192.168.1.254  --> MyPC254       Packets: Sent = 1, Received = 1, Lost = 1 (0% loss),

//////////////////////////////////////////////////////////////////////

@echo off

for /f %%p in (IPlist.txt) do (
   echo %%p
ping -n 1 -w 100 -a %%p |find /i "Packet"

)

//////////////////////////////////////////////////////////////////////

192.168.1.1
    Packets: Sent = 1, Received = 1, Lost = 0 (0% loss),
192.168.1.2
    Packets: Sent = 1, Received = 0, Lost = 1 (100% loss),
...
...
...

192.168.1.254
    Packets: Sent = 1, Received = 1, Lost = 1 (0% loss),
0
cunoc
Asked:
cunoc
  • 6
  • 5
  • 3
  • +1
9 Solutions
 
AmazingTechCommented:
Try this.
@echo off

for /l %%p in (1,1,254) do (
   echo %%p
ping -n 1 -w 100 -a %%p |find /i "Packet"

)

Open in new window

0
 
AmazingTechCommented:
Opps.
@echo off 
 
for /l %%p in (1,1,254) do ( 
   echo %%p 
ping -n 1 -w 100 -a 192.168.1.%%p |find /i "Packet" 
 
)

Open in new window

0
 
Bill PrewCommented:
> AT,

I think you forgot the 192.168.1 part of the IP addess :-).

~bp
0
Nothing ever in the clear!

This technical paper will help you implement VMware’s VM encryption as well as implement Veeam encryption which together will achieve the nothing ever in the clear goal. If a bad guy steals VMs, backups or traffic they get nothing.

 
AmazingTechCommented:
Thanks billprew.

For the (0% loss) only.
@echo off  
  
for /l %%p in (1,1,254) do (  
   echo %%p  
ping -n 1 -w 100 -a 192.168.1.%%p |find /i "Packet" |find /i "(0%% loss)" 
  
)

Open in new window

0
 
jazzIIIloveCommented:
Hi there;

I know it is batch scripting, but for a side note, in linux bash scripting, you can do this with the following.

The result is preserved in results.txt

Best regards.
sudo fping -a -q -g 192.168.1.0/24 > results.txt

Open in new window

0
 
cunocAuthor Commented:
Hi all,
Here is the result I got, I don't see the IPs and Hostnames ...


@echo off  
 
for /l %%p in (1,1,40) do (  
   echo %%p  
ping -n 1 -w 100 -a 192.168.1.%%p |find /i "Packet"  
 
)
PAUSE ..

1
    Packets: Sent = 1, Received = 1, Lost = 0 (0% loss),
2
    Packets: Sent = 1, Received = 0, Lost = 1 (100% loss),
...
...
254
     Packets: Sent = 1, Received = 0, Lost = 1 (100% loss),
0
 
Bill PrewCommented:
Give this a try.

@echo off
setlocal EnableDelayedExpansion
for /l %%A in (1,1,254) do (
  for /f "tokens=1-10 delims==, " %%B in ('ping -n 1 -w 100 -a 192.168.1.%%A') do (
    if "%%B"=="Pinging" set Host=%%C
    if "%%B"=="Packets:" if "%%I"=="(0%%" echo 192.168.1.%%A, !Host!
  )
)

~bp
0
 
cunocAuthor Commented:
Hi,

@echo off  
   
for /l %%p in (1,1,20) do (  
   echo 192.168.1.%%p  
ping -n 1 -w 50 -a 192.168.1.%%p |find /i "Packet" |find /i "(0%% loss)"  
   
)
PAUSE ..

//////////////////////////////////////

192.168.1.1                              <---------------------(I need to have the Hostnames for the output...)
    Packets: Sent = 1, Received = 1, Lost = 0 (0% loss),
192.168.1.2
192.168.1.3
192.168.1.4
192.168.1.5
192.168.1.6
192.168.1.7
192.168.1.8
192.168.1.9
192.168.1.10
192.168.1.11
192.168.1.12
192.168.1.13
192.168.1.14
192.168.1.15
192.168.1.16
192.168.1.17
192.168.1.18
192.168.1.19
192.168.1.20
Press any key to continue . . .
0
 
cunocAuthor Commented:
Hi, billprew:
Here is the result, when I executed your code ...

@echo off
setlocal EnableDelayedExpansion
for /l %%A in (1,1,50) do (
  for /f "tokens=1-10 delims==, " %%B in ('ping -n 1 -w 100 -a 192.168.1.%%A') do (
    if "%%B"=="Pinging" set Host=%%C
    if "%%B"=="Packets:" if "%%I"=="(0%%" echo 192.168.1.%%A, !Host!
  )
)
PAUSE ..

192.168.1.1, 192.168.1.1         (<------------------------What wrong in this part)
192.168.1.33, PC-641788.linksys.com
192.168.1.35, 192.168.1.35      (<------------------------What wrong in this part)
Press any key to continue . . .
0
 
Bill PrewCommented:
In the cases where the IP shows as the host, that is because PING did not resolve the IP to a hostname.

Try a ping against one of those IPs and notice the no hostname is showed.

~bp
0
 
cunocAuthor Commented:
Hi, billprew:
I got the result, when I executed your code ...

@echo off
setlocal EnableDelayedExpansion
for /l %%A in (1,1,35) do (
  for /f "tokens=1-10 delims==, " %%B in ('ping -n 1 -w 100 -a 192.168.1.%%A') do (
    if "%%B"=="Pinging" set Host=%%C
    if "%%B"=="Packets:" if "%%I"=="(0%%" echo 192.168.1.%%A, !Host!
  )
)
PAUSE ..

192.168.1.1, 192.168.1.1         (<------------------------What wrong in this part)
192.168.1.33, PC-641788.linksys.com
192.168.1.35, 192.168.1.35      (<------------------------What wrong in this part)
Press any key to continue . . .
0
 
Bill PrewCommented:
Yes, I understand that.  But all my code is doing is interpreting the output from the PING command done against each IP address in sequence.  So if the PING command doesn't resolve the IP to a hostname there is nothing the script can do about that.  In the first line returned from PING, it will typically look like:

Pinging 192.168.1.1 with 32 bytes of data:

If PING is able to resolve the IP address to an IP address it will look like:

Pinging PC-641788.linksys.com with 32 bytes of data:

I can't really tell you why PING has trouble resolving the IP address to a name for some addresses, but did not the following in the documentation for PING.

You can use ping to test both the computer name and the IP address of the computer. If pinging the IP address is successful, but pinging the computer name is not, you might have a name resolution problem. In this case, ensure that the computer name you are specifying can be resolved through the local Hosts file, by using Domain Name System (DNS) queries, or through NetBIOS name resolution techniques.
~bp
0
 
jazzIIIloveCommented:
Hi there;

You may find it primitive, nevertheless, here is my humble batch file:)

Best regards.
FOR /L %%a IN (1, 1, 254) DO PING -n 1 -w 60 192.168.1.%%a >> "c:\\ping-result.txt"

Open in new window

0
 
cunocAuthor Commented:
Hi billprew:
You are 100% right.....,
/////////////////////////////////////////////////////
@echo off
setlocal EnableDelayedExpansion
for /l %%A in (1,1,60) do (
  for /f "tokens=1-10 delims==, " %%B in ('ping -n 1 -w 100 -a 209.191.93.%%A') do (
    if "%%B"=="Pinging" set Host=%%C
    if "%%B"=="Packets:" if "%%I"=="(0%%" echo 192.168.1.%%A, !Host!
  )
)
PAUSE ..
/////////////////////////////////////////////////////
192.168.1.3, dhfedc3.data.mud.yahoo.com
192.168.1.4, dhfedc4.data.mud.yahoo.com
192.168.1.5, dhfedc5.data.mud.yahoo.com
192.168.1.12, web236.address.pim.mud.yahoo.com
192.168.1.13, web237.address.pim.mud.yahoo.com
192.168.1.14, web238.address.pim.mud.yahoo.com
192.168.1.19, rl1.yosmsg.vip.mud.yahoo.com
192.168.1.21, m-qa5.media.mud.yahoo.com
0
 
cunocAuthor Commented:
Hi,
Thank you all of your work.
0
 
Bill PrewCommented:
I'm a bit confused.  The proposed accepted solution is just a cut and paste of my script by the OP saying it worked.  Seems like my post of 26173590 should have been the accepted solution.  If the OP wants to assign some assists they are welcome too, just seems contrary to EE protocol the way it is being closed.

~bp
0

Featured Post

Free learning courses: Active Directory Deep Dive

Get a firm grasp on your IT environment when you learn Active Directory best practices with Veeam! Watch all, or choose any amount, of this three-part webinar series to improve your skills. From the basics to virtualization and backup, we got you covered.

  • 6
  • 5
  • 3
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now