<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DZone Snippets: ip code</title>
    <link>http://snippets.dzone.com/posts</link>
    <pubDate>Wed, 21 May 2008 15:12:21 GMT</pubDate>
    <description>DZone Snippets: ip code</description>
    <item>
      <title>IP Catcher</title>
      <link>http://snippets.dzone.com/posts/show/5430</link>
      <description>--Howto use--&lt;br /&gt;the command line:&lt;br /&gt;   ruby /path/to/ipcatcher.rb /path/to/filename&lt;br /&gt;the program will print all the ip addresses which is inside the file.&lt;br /&gt; the program doesn't print the same ip address twice. with no duplications.&lt;br /&gt;Done by &lt;a href="http://www.amerj.info"&gt;Amer Jazaerly&lt;/a&gt;.there is no copyright.&lt;br /&gt;have fun ;)&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;#!/usr/bin/ruby&lt;br /&gt;&lt;br /&gt;def get_ips(file)&lt;br /&gt;  ips = []&lt;br /&gt;  File.read(file).to_a.each do |place|&lt;br /&gt;    sf = 0&lt;br /&gt;    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)&lt;br /&gt;      sf = sfn + 3&lt;br /&gt;      ips &lt;&lt; $&amp;&lt;br /&gt;    end&lt;br /&gt;  end&lt;br /&gt;  return ips&lt;br /&gt;end&lt;br /&gt;&lt;br /&gt;get_ips(ARGV[0]).uniq.each { |ip| puts ip }&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Sat, 26 Apr 2008 08:18:04 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/5430</guid>
      <author>amer (Amer Jazaerly)</author>
    </item>
    <item>
      <title>Finding your WAN IP address</title>
      <link>http://snippets.dzone.com/posts/show/5346</link>
      <description>My server sits behind a NAT router, so finding out my public IP address is a non-trivial task. I can use curl to poll checkip.dyndns.org for my current address:&lt;br /&gt;&lt;code&gt;&lt;br /&gt;curl -s checkip.dyndns.org&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;The current IP check returns the information in this format: &lt;html&gt;&lt;head&gt;&lt;title&gt;Current IP Check&lt;/title&gt;&lt;/head&gt;&lt;body&gt;Current IP Address: 216.239.39.99&lt;/body&gt;&lt;/html&gt;&lt;br /&gt;&lt;br /&gt;Using cut, I can extract just the information that I need:&lt;br /&gt;&lt;code&gt;&lt;br /&gt;curl -s checkip.dyndns.org|cut -d ":" -f2|cut -d "&lt;" -f1&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;That produces something a bit more readable: 216.239.39.99&lt;br /&gt;&lt;br /&gt;-------------------------&lt;br /&gt;This article snippet was copied from &lt;a href="http://www.linux.com/articles/52552"&gt;My sysadmin toolbox&lt;/a&gt; [linux.com] while I was googling for 'apt-cache search dyndns'.</description>
      <pubDate>Fri, 11 Apr 2008 09:14:36 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/5346</guid>
      <author>jrobertson (James Robertson)</author>
    </item>
    <item>
      <title>Convert an integer to IP string</title>
      <link>http://snippets.dzone.com/posts/show/5323</link>
      <description>Convert an integer to IP. I don't think you can do this with IPAddr, but I could be wrong&lt;br /&gt;&lt;br /&gt;Edit, the original didn't work for low ranges.&lt;br /&gt;&lt;br /&gt;And I was wrong about the IPAddr thing. I should have google'd better.&lt;br /&gt;&lt;br /&gt;IPAddr.new(16909060, Socket::AF_INET).to_s&lt;br /&gt;</description>
      <pubDate>Fri, 04 Apr 2008 21:30:36 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/5323</guid>
      <author>markpercival (Mark Percival)</author>
    </item>
    <item>
      <title>Ping a range of IP addresses</title>
      <link>http://snippets.dzone.com/posts/show/5210</link>
      <description>&lt;code&gt;&lt;br /&gt;(1..254).each {|i| puts ": found 192.168.1.#{i}" if  `ping 192.168.1.#{i} -c 1 -w 1`.match(/ttl/) }&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Tue, 11 Mar 2008 09:06:19 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/5210</guid>
      <author>jrobertson (James Robertson)</author>
    </item>
    <item>
      <title>Getting a client's IP address with EventMachine</title>
      <link>http://snippets.dzone.com/posts/show/5098</link>
      <description>This is from, and by: &lt;a href="http://nhw.pl/wp/2007/12/07/eventmachine-how-to-get-clients-ip-address/"&gt;http://nhw.pl/wp/2007/12/07/eventmachine-how-to-get-clients-ip-address/&lt;/a&gt; .. I just wanted a more permanent reference as it's cool code.&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;EventMachine::run {&lt;br /&gt;   EventMachine::open_datagram_socket $conf[:address],&lt;br /&gt;                                 $conf[:udp_port], CustomServer&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;module CustomServer&lt;br /&gt;   def receive_data d&lt;br /&gt;      pp get_peername[2,6].unpack "nC4"&lt;br /&gt;   end&lt;br /&gt;end&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Sun, 03 Feb 2008 20:12:09 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/5098</guid>
      <author>peter (Peter Cooperx)</author>
    </item>
    <item>
      <title>Format IPv4 address in octal binary format and vice versa (Ruby / Rails)</title>
      <link>http://snippets.dzone.com/posts/show/4931</link>
      <description>Format an IPv4 address like 192.168.1.1 in dotted binary format like 11000000.10101000.00000001.00000001&lt;br /&gt;You also need this class: http://snippets.dzone.com/posts/show/2472&lt;br /&gt;For Rails: Put this in your controller!&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;  # convert a dotted decimal IPv4 address to dotted binary format&lt;br /&gt;  def ipv4_to_binary(ipv4addr)&lt;br /&gt;    ia = ipv4addr.to_s.split('.')&lt;br /&gt;    if ia.size != 4&lt;br /&gt;      return "0.0.0.0"&lt;br /&gt;    end&lt;br /&gt;    output = ""&lt;br /&gt;    i = 1&lt;br /&gt;    for octett in ia&lt;br /&gt;      output = output + octett.to_i.to_s(2).using("########","0",true)&lt;br /&gt;      if i &lt; 4&lt;br /&gt;        output = output + "."&lt;br /&gt;      end&lt;br /&gt;      i += 1&lt;br /&gt;    end&lt;br /&gt;    return output&lt;br /&gt;  end&lt;br /&gt;  &lt;br /&gt;  &lt;br /&gt;  # convert a IPv4 adress in binary dotted format to a dotted IPv4 address&lt;br /&gt;  def binary_to_ipv4(ipv4addr)&lt;br /&gt;    ia = ipv4addr.to_s.split('.')&lt;br /&gt;    if ia.size != 4&lt;br /&gt;      return "0.0.0.0"&lt;br /&gt;    end&lt;br /&gt;    output = ""&lt;br /&gt;    i = 1&lt;br /&gt;    for octett in ia&lt;br /&gt;      output = output + octett.to_s.to_i(2).to_s&lt;br /&gt;      if i &lt; 4&lt;br /&gt;        output = output + "."&lt;br /&gt;      end&lt;br /&gt;      i += 1&lt;br /&gt;    end&lt;br /&gt;    return output&lt;br /&gt;  end&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Fri, 28 Dec 2007 14:12:12 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/4931</guid>
      <author>MichaelWhi (Michael Whittaker)</author>
    </item>
    <item>
      <title>External IP</title>
      <link>http://snippets.dzone.com/posts/show/4863</link>
      <description>I use this to find the external ip&lt;br /&gt;&lt;br /&gt;aBe&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;#!/usr/bin/env ruby&lt;br /&gt; require 'win32ole'&lt;br /&gt; require 'open-uri'&lt;br /&gt; echocmd = "IP: "&lt;br /&gt; ip = ( open("http://myip.dk") {|f| /([0-9]{1,3}\.){3}[0-9]&lt;br /&gt;&lt;br /&gt;{1,3}/.match(f.read)[0].to_a[0]})&lt;br /&gt;&lt;br /&gt;puts echocmd + ip&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Fri, 07 Dec 2007 21:15:47 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/4863</guid>
      <author>abe238 (Abe Diaz)</author>
    </item>
    <item>
      <title>Send custom UDP packets in Ruby</title>
      <link>http://snippets.dzone.com/posts/show/4541</link>
      <description>&lt;br /&gt;From: http://www.ruby-forum.com/topic/124159&lt;br /&gt;Author: Bill Kelly&lt;br /&gt;&lt;br /&gt;For yet another nifty UDP snippet see &lt;a href="http://www.rubyinside.com/skype-style-firewall-busting-with-ruby-and-udp-399.html"&gt;Skype-Style Firewall Busting with Ruby and UDP&lt;/a&gt;.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;&lt;br /&gt;require 'socket'&lt;br /&gt;&lt;br /&gt;#abort "Usage: server_addr, server_port, cmd_str" unless ARGV.length == 3&lt;br /&gt;&lt;br /&gt;UDP_RECV_TIMEOUT = 3  # seconds&lt;br /&gt;&lt;br /&gt;def q2cmd(server_addr, server_port, cmd_str)&lt;br /&gt;  resp, sock = nil, nil&lt;br /&gt;  begin&lt;br /&gt;   cmd = "\377\377\377\377#{cmd_str}\0"&lt;br /&gt;    sock = UDPSocket.open&lt;br /&gt;    sock.send(cmd, 0, server_addr, server_port)&lt;br /&gt;    resp = if select([sock], nil, nil, UDP_RECV_TIMEOUT)&lt;br /&gt;      sock.recvfrom(65536)&lt;br /&gt;    end&lt;br /&gt;    if resp&lt;br /&gt;      resp[0] = resp[0][4..-1]  # trim leading 0xffffffff&lt;br /&gt;    end&lt;br /&gt;  rescue IOError, SystemCallError&lt;br /&gt;  ensure&lt;br /&gt;    sock.close if sock&lt;br /&gt;  end&lt;br /&gt;  resp ? resp[0] : nil&lt;br /&gt;end&lt;br /&gt;&lt;br /&gt;# your firewall has to allow communication with IP address 67.19.248.74 (port 27912)&lt;br /&gt;#server, port, cmd = *ARGV&lt;br /&gt;server = "tastyspleen.net"&lt;br /&gt;port = 27912&lt;br /&gt;cmd = "status"&lt;br /&gt;&lt;br /&gt;result = q2cmd(server, port, cmd)&lt;br /&gt;puts result&lt;br /&gt;&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;</description>
      <pubDate>Fri, 14 Sep 2007 11:19:36 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/4541</guid>
      <author>ntk ()</author>
    </item>
    <item>
      <title>Server name/IP converter</title>
      <link>http://snippets.dzone.com/posts/show/4246</link>
      <description>When passed either an IPv4 address or the name of a domain or server, this script will return either a name or an IP, respectively.&lt;br /&gt;&lt;code&gt;&lt;br /&gt;#!/usr/bin/perl -w&lt;br /&gt;use strict;&lt;br /&gt;use Socket;&lt;br /&gt;my $arg = shift;&lt;br /&gt;if ($arg =~ /^(\d+\.){3}\d+$/) {&lt;br /&gt; print scalar gethostbyaddr(inet_aton($arg), AF_INET), "\n"&lt;br /&gt;} else { printf "%vd\n", scalar gethostbyname $arg }&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Thu, 05 Jul 2007 02:24:29 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/4246</guid>
      <author>Minimiscience (Guildorn Tanaleth)</author>
    </item>
    <item>
      <title>Get external IP address</title>
      <link>http://snippets.dzone.com/posts/show/3952</link>
      <description>Another snippet code goes here:http://snippets.dzone.com/posts/show/3947 by ttmrichter&lt;br /&gt;&lt;br /&gt;enjoy:)&lt;br /&gt;&lt;code&gt;&lt;br /&gt;require 'open-uri'&lt;br /&gt;require 'rubygems'&lt;br /&gt;require 'hpricot'&lt;br /&gt;&lt;br /&gt;puts (Hpricot(open('http://myip.dk'))/"//td/font/b")[0].inner_html&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Thu, 03 May 2007 03:51:37 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/3952</guid>
      <author>martinx (martin)</author>
    </item>
  </channel>
</rss>
