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

About this user

Vsevolod Balashov

« Newer Snippets
Older Snippets »
Showing 1-2 of 2 total  RSS 

Ruby port of range2cidr. Convert IP ranges to set of CIDR.

See rfc1878 for more detail about CIDR

   1  
   2  #!/usr/bin/env ruby
   3  # -*- coding: utf-8 -*-
   4  # $Hg: range_cidr.rb,v af9566d89389 2007-04-12 20:28 +0400 $
   5  # (C) 2007 under terms of LGPL v2.1
   6  # by Vsevolod S. Balashov <vsevolod@balashov.name>
   7  #
   8  # backported from perl code
   9  # http://www.irbs.net/internet/postfix/0401/att-3032/cidr_range.pl.gz
  10  
  11  def range_cidr(first, last, &block)
  12    if first < last
  13      idx1 = 32
  14      idx1 -= 1 while first[idx1] == last[idx1]
  15      prefix = first >> idx1+1 << idx1+1
  16  
  17      idx2 = 0
  18      idx2 += 1 while idx2 <= idx1 and first[idx2] == 0 and last[idx2] == 1
  19  
  20      if idx2 <= idx1
  21        range_cidr(first, prefix | 2**idx1-1, &block)
  22        range_cidr(prefix | 1 << idx1, last, &block)
  23      else
  24        yield prefix, 32-idx2
  25      end
  26    else
  27      yield first, 32
  28    end
  29  end


Example Usage

   1  
   2  #!/usr/bin/env ruby
   3  # $Hg: range2cidr.rb,v 2142c33ada8b 2007-04-11 23:14 +0400 $
   4  # (C) 2007 under terms of GPL v2
   5  # by Vsevolod S. Balashov <vsevolod@balashov.name>
   6  #
   7  # example usage of range_cidr.rb
   8  
   9  require 'lib/range_cidr'
  10  require 'ipaddr'
  11  require 'socket'
  12  
  13  if __FILE__ == $0
  14    if ARGV.size == 2
  15      range_cidr(IPAddr.new(ARGV[0]).to_i, IPAddr.new(ARGV[1]).to_i) { |subnet, mask|
  16        puts "#{IPAddr.new(subnet, Socket::AF_INET).to_s}/#{mask}"  }
  17    else
  18      puts "usage: range2cidr <first_ip> <last_ip>"
  19      puts "example: range2cidr 192.168.1.0 192.168.2.255"
  20    end
  21  end


Type in terminal and look result

   1  
   2  $ ruby range2cidr.rb 192.168.1.0 192.168.2.255
   3  192.168.1.0/24
   4  192.168.2.0/24


I hate perl.

Google PageRank Ruby Checker

   1  
   2  #!/usr/bin/env ruby
   3  # -*- coding: utf-8 -*-
   4  # $Id: google-pr.rb,v b205c14e4ef5 2007-01-15 13:53 +0300 $
   5  # (C) 2006-2007 under terms of LGPL v2.1 
   6  # by Vsevolod S. Balashov <vsevolod@balashov.name>
   7  # based on 3rd party code snippets (see comments)
   8  
   9  require 'uri'
  10  require 'open-uri'
  11  
  12  module SEO
  13  
  14    # http://blog.outer-court.com/archive/2004_06_27_index.html#108834386239051706
  15    class GooglePR
  16  
  17      def initialize(uri)
  18        @uri = uri
  19      end
  20  
  21      M=0x100000000 # modulo for unsigned int 32bit(4byte)
  22  
  23      def m1(a,b,c,d)
  24        (((a+(M-b)+(M-c))%M)^(d%M))%M # mix/power mod
  25      end
  26  
  27      def i2c(i)
  28        [i&0xff, i>>8&0xff, i>>16&0xff, i>>24&0xff]
  29      end
  30  
  31      def c2i(s,k=0)
  32        ((s[k+3].to_i*0x100+s[k+2].to_i)*0x100+s[k+1].to_i)*0x100+s[k].to_i
  33      end
  34  
  35      def mix(a,b,c)
  36        a = a%M; b = b%M; c = c%M
  37        a = m1(a,b,c, c >> 13); b = m1(b,c,a, a <<  8); c = m1(c,a,b, b >> 13)
  38        a = m1(a,b,c, c >> 12); b = m1(b,c,a, a << 16); c = m1(c,a,b, b >>  5)
  39        a = m1(a,b,c, c >>  3); b = m1(b,c,a, a << 10); c = m1(c,a,b, b >> 15)
  40        [a, b, c]
  41      end
  42  
  43      def old_cn(iurl = 'info:' + @uri)
  44        a = 0x9E3779B9; b = 0x9E3779B9; c = 0xE6359A60
  45        len = iurl.size 
  46        k = 0
  47        while (len >= k + 12) do
  48          a += c2i(iurl,k); b += c2i(iurl,k+4); c += c2i(iurl,k+8)
  49          a, b, c = mix(a, b, c)
  50          k = k + 12
  51        end
  52        a += c2i(iurl,k); b += c2i(iurl,k+4); c += (c2i(iurl,k+8) << 8) + len
  53        a,b,c = mix(a,b,c)
  54        return c
  55      end
  56  
  57      def cn
  58        ch = old_cn
  59        ch = ((ch/7) << 2) | ((ch-(ch/13).floor*13)&7)
  60        new_url = []
  61        20.times { i2c(ch).each { |i| new_url << i }; ch -= 9 }
  62        ('6' + old_cn(new_url).to_s).to_i
  63      end
  64  
  65      def request_uri
  66        # http://www.bigbold.com/snippets/posts/show/1260 + _ -> %5F
  67        "http://toolbarqueries.google.com/search?client=navclient-auto&hl=en&ch=#{cn}&ie=UTF-8&oe=UTF-8&features=Rank&q=info:#{URI.escape(@uri, /[^-.!~*'()a-zA-Z\d]/)}"
  68      end
  69   
  70      def page_rank(uri = @uri)
  71        @uri = uri if uri != @uri
  72        open(request_uri) { |f| return $1.to_i if f.string =~ /Rank_1:\d:(\d+)/ }
  73        nil
  74      end
  75  
  76      private :m1, :i2c, :c2i, :mix, :old_cn
  77      attr_accessor :uri
  78    end
  79  
  80  end
  81  
  82  if __FILE__ == $0 and 1 == ARGV.size
  83    puts SEO::GooglePR.new(ARGV[0]).page_rank
  84  end
« Newer Snippets
Older Snippets »
Showing 1-2 of 2 total  RSS