class NetscanCalife::IpScan

Public Class Methods

format_result(data,fmt='txt') click to toggle source
Print scan result

end

# File lib/netscan_calife/ip_scan.rb, line 80
def self.format_result(data,fmt='txt')

  case fmt
      
  when 'txt'
    printf "%s\n","-----------------------------------------"
    printf "|\t%s\t\t|\t%s\t|\n","HOST","FOUND"      
    printf "%s\n","-----------------------------------------"
    data.each { |host,found|
      printf "|\t%s\t|\t%s\t|\n","#{host}","#{found}"
    }
    printf "%s\n","-----------------------------------------"
    
  when 'html'
    printf "%s\n","<table>"
    printf "%s\n","<tr>"
    printf "%s\n","<th>"
    printf "%s\n","HOST"
    printf "%s\n","</th>"
    printf "%s\n","<th>"
    printf "%s\n","FOUND"
    printf "%s\n","</th>"        
    printf "%s\n","</tr>"
    data.each { |host,found|
      printf "%s\n","<tr>"
      printf "%s\n","<td>"
      printf "%s\n","#{host}"
      printf "%s\n","</td>"
      printf "%s\n","<td>"
      printf "%s\n","#{found}"
      printf "%s\n","</td>"          
      printf "%s\n","</tr>"
    }
    printf "%s\n","</table>"
  else
    stderr "Format not supported"
  end
  
end
from_yaml(filename) click to toggle source

Read data from yaml file

  • Args :

    • ++ -> filename

  • Returns : -

TODO: handle exceptions

# File lib/netscan_calife/ip_scan.rb, line 69
def self.from_yaml(filename)

  File.open(filename) { |fileobj|
    YAML.load(fileobj)
  }
  
end
scan(network,netmask) click to toggle source

Ping network scan

# File lib/netscan_calife/ip_scan.rb, line 14
def self.scan(network,netmask)

    net1 = IPAddr.new(network).mask(netmask)

    statuses=net1.to_range.map do |host|

      pid=fork {
        exec("ping", "-c 3", "#{host}",:out=>"/dev/null",err: :out)
      }

      [pid,host.to_s]
      
    end
    
    # statuses.each { |p,h|
    #   Process.wait(p)
    #   printf("%s\n","Host found #{h}") if $?.exitstatus.zero?
    # }

    statuses.map! { |p,h|
      Process.wait(p)
      [h,$?.exitstatus.zero?]
    }
     
    return statuses
    
end
to_yaml(data,filename) click to toggle source

Serialize scan result to yaml file

  • Args :

    • ++ -> data

  • Returns :

    • true on success

# File lib/netscan_calife/ip_scan.rb, line 50
def self.to_yaml(data,filename)

  File.open(filename,"w") { |fileobj|
    YAML.dump(data,fileobj)
  }

  return true
  
end