Never been to DZone Snippets before?

Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

IP Catcher (See related posts)

--Howto use--
the command line:
ruby /path/to/ipcatcher.rb /path/to/filename
the program will print all the ip addresses which is inside the file.
the program doesn't print the same ip address twice. with no duplications.
Done by Amer Jazaerly.there is no copyright.
have fun ;)

   1  
   2  #!/usr/bin/ruby
   3  
   4  def get_ips(file)
   5    ips = []
   6    File.read(file).to_a.each do |place|
   7      sf = 0
   8      while sfn = place.index(/(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)/,sf)
   9        sf = sfn + 3
  10        ips << $&
  11      end
  12    end
  13    return ips
  14  end
  15  
  16  get_ips(ARGV[0]).uniq.each { |ip| puts ip }

You need to create an account or log in to post comments to this site.


Click here to browse all 5545 code snippets

Related Posts