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

« Newer Snippets
Older Snippets »
Showing 1-10 of 22 total  RSS 

IP Catcher

--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 ;)

#!/usr/bin/ruby

def get_ips(file)
  ips = []
  File.read(file).to_a.each do |place|
    sf = 0
    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)
      sf = sfn + 3
      ips << $&
    end
  end
  return ips
end

get_ips(ARGV[0]).uniq.each { |ip| puts ip }

Finding your WAN IP address

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:
curl -s checkip.dyndns.org

The current IP check returns the information in this format: <html><head><title>Current IP Check</title></head><body>Current IP Address: 216.239.39.99</body></html>

Using cut, I can extract just the information that I need:
curl -s checkip.dyndns.org|cut -d ":" -f2|cut -d "<" -f1

That produces something a bit more readable: 216.239.39.99

-------------------------
This article snippet was copied from My sysadmin toolbox [linux.com] while I was googling for 'apt-cache search dyndns'.

Convert an integer to IP string

Convert an integer to IP. I don't think you can do this with IPAddr, but I could be wrong

Edit, the original didn't work for low ranges.

And I was wrong about the IPAddr thing. I should have google'd better.

IPAddr.new(16909060, Socket::AF_INET).to_s

Ping a range of IP addresses

(1..254).each {|i| puts ": found 192.168.1.#{i}" if  `ping 192.168.1.#{i} -c 1 -w 1`.match(/ttl/) }

Getting a client's IP address with EventMachine

This is from, and by: http://nhw.pl/wp/2007/12/07/eventmachine-how-to-get-clients-ip-address/ .. I just wanted a more permanent reference as it's cool code.

EventMachine::run {
   EventMachine::open_datagram_socket $conf[:address],
                                 $conf[:udp_port], CustomServer
}

module CustomServer
   def receive_data d
      pp get_peername[2,6].unpack "nC4"
   end
end

Format IPv4 address in octal binary format and vice versa (Ruby / Rails)

Format an IPv4 address like 192.168.1.1 in dotted binary format like 11000000.10101000.00000001.00000001
You also need this class: http://snippets.dzone.com/posts/show/2472
For Rails: Put this in your controller!


  # convert a dotted decimal IPv4 address to dotted binary format
  def ipv4_to_binary(ipv4addr)
    ia = ipv4addr.to_s.split('.')
    if ia.size != 4
      return "0.0.0.0"
    end
    output = ""
    i = 1
    for octett in ia
      output = output + octett.to_i.to_s(2).using("########","0",true)
      if i < 4
        output = output + "."
      end
      i += 1
    end
    return output
  end
  
  
  # convert a IPv4 adress in binary dotted format to a dotted IPv4 address
  def binary_to_ipv4(ipv4addr)
    ia = ipv4addr.to_s.split('.')
    if ia.size != 4
      return "0.0.0.0"
    end
    output = ""
    i = 1
    for octett in ia
      output = output + octett.to_s.to_i(2).to_s
      if i < 4
        output = output + "."
      end
      i += 1
    end
    return output
  end

External IP

I use this to find the external ip

aBe

#!/usr/bin/env ruby
 require 'win32ole'
 require 'open-uri'
 echocmd = "IP: "
 ip = ( open("http://myip.dk") {|f| /([0-9]{1,3}\.){3}[0-9]

{1,3}/.match(f.read)[0].to_a[0]})

puts echocmd + ip

Send custom UDP packets in Ruby


From: http://www.ruby-forum.com/topic/124159
Author: Bill Kelly

For yet another nifty UDP snippet see Skype-Style Firewall Busting with Ruby and UDP.



require 'socket'

#abort "Usage: server_addr, server_port, cmd_str" unless ARGV.length == 3

UDP_RECV_TIMEOUT = 3  # seconds

def q2cmd(server_addr, server_port, cmd_str)
  resp, sock = nil, nil
  begin
   cmd = "\377\377\377\377#{cmd_str}\0"
    sock = UDPSocket.open
    sock.send(cmd, 0, server_addr, server_port)
    resp = if select([sock], nil, nil, UDP_RECV_TIMEOUT)
      sock.recvfrom(65536)
    end
    if resp
      resp[0] = resp[0][4..-1]  # trim leading 0xffffffff
    end
  rescue IOError, SystemCallError
  ensure
    sock.close if sock
  end
  resp ? resp[0] : nil
end

# your firewall has to allow communication with IP address 67.19.248.74 (port 27912)
#server, port, cmd = *ARGV
server = "tastyspleen.net"
port = 27912
cmd = "status"

result = q2cmd(server, port, cmd)
puts result


Server name/IP converter

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.
#!/usr/bin/perl -w
use strict;
use Socket;
my $arg = shift;
if ($arg =~ /^(\d+\.){3}\d+$/) {
 print scalar gethostbyaddr(inet_aton($arg), AF_INET), "\n"
} else { printf "%vd\n", scalar gethostbyname $arg }

Get external IP address

Another snippet code goes here:http://snippets.dzone.com/posts/show/3947 by ttmrichter

enjoy:)
require 'open-uri'
require 'rubygems'
require 'hpricot'

puts (Hpricot(open('http://myip.dk'))/"//td/font/b")[0].inner_html
« Newer Snippets
Older Snippets »
Showing 1-10 of 22 total  RSS