Pingsweeps

A few code snippets to perform ping sweeps:

Windows cmd:

for /L %I IN (1, 1, 10) do start /b /i ping -n 1 192.168.1.%I | 
find "Reply from" | 
find /v "unreachable"

Windows Powershell:

1..20 | 
% {"192.168.1.$($_): $(Test-Connection -count 1 -comp 192.168.1.$($_) -quiet)"}

Rference:
https://gallery.technet.microsoft.com/scriptcenter/Fast-asynchronous-ping-IP-d0a5cf0e

Linux bash:

#!/bin/bash
 
prefix=192.168.1
start=1
end=10
 
for ip in $(seq $start $end); do
ping $prefix.$ip -c 1 | grep "bytes from" | cut -d " " -f4 | cut -d":" -f1 &
done

python:

#!/usr/bin/python2
 
import multiprocessing
import subprocess
import os
 
start = 1
end = 10
prefix = "192.168.1."
 
def pinger( job_q, results_q ):
    DEVNULL = open(os.devnull,'w')
    while True:
        ip = job_q.get()
        if ip is None: break
 
        try:
            subprocess.check_call(['ping','-c1',ip],
                                  stdout=DEVNULL)
            results_q.put(ip)
        except:
            pass
 
if __name__ == '__main__':
    pool_size = 255
 
    jobs = multiprocessing.Queue()
    results = multiprocessing.Queue()
 
    pool = [ multiprocessing.Process(target=pinger, args=(jobs,results))
             for i in range(pool_size) ]
 
    for p in pool:
        p.start()
 
    for i in range(start,end):
        jobs.put(prefix + '{0}'.format(i))
 
    for p in pool:
        jobs.put(None)
 
    for p in pool:
        p.join()
 
    while not results.empty():
        ip = results.get()
        print(ip)

perl:

#!/usr/bin/perl
#use strict;
use Net::Ping;
 
@hosts=(1..10);
 
 
$net = Net::Ping->new('syn');
foreach $host (@hosts) {
  $net->ping('192.168.1.'. $host);
}
while (($host, $rtt, $ip) = $net->ack( )) {
  printf "%s\n",  $ip;
}

ruby:

require 'timeout'
require "socket"
 
module Ping
 
  def pingecho(host, timeout=0.01, service="echo")
    begin
      timeout(timeout) do
    s = TCPSocket.new(host, service)
    s.close
      end
    rescue Errno::ECONNREFUSED
      return true
    rescue Timeout::Error, StandardError
      return false
    end
    return true
  end
  module_function :pingecho
end
 
 
#Needed IP utilities
class IP
  def initialize(ip)
    @ip = ip
  end
 
  #convert ip address to string
  def to_s
    @ip
  end
 
  #compare two ip addresses
  def==(other)
    to_s==other.to_s
  end
 
  #generate ips
  def succ
    return @ip if @ip == "255.255.255.255"
    parts = @ip.split('.').reverse
    parts.each_with_index do |part,i|
      if part.to_i < 255
        part.succ!
        break
      elsif part == "255"
        part.replace("0") unless i == 3
      else
        raise ArgumentError, "Invalid number #{part} in IP address"
      end
    end
    parts.reverse.join('.')
  end
 
  def succ!
    @ip.replace(succ)
  end
end
 
print "Input Starting IP Address: "
start_ip = gets.strip 
 
print "Input Ending IP Address: "
end_ip = gets.strip
 
i = IP.new(start_ip)
 
ip_string = ""
print "Working"
ip_string = i.to_s + ", " if Ping::pingecho i.to_s
begin 
i.succ!
ip_string = ip_string + i.to_s + ", " if Ping::pingecho i.to_s
print "." 
end until i == end_ip
 
puts ip_string.class
 
puts "Finished found hosts at: #{ip_string}"

nmap:

nmap -sn 192.168.1.1-10