Ruby one-liner to find external IP address across NAT router.
my_ip = (require 'open-uri' ; open("http://myip.dk") { |f| /([0-9]{1,3}\.){3}[0-9]{1,3}/.match(f.read)[0].to_a[0] })
11305 users tagging and storing useful source code snippets
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
my_ip = (require 'open-uri' ; open("http://myip.dk") { |f| /([0-9]{1,3}\.){3}[0-9]{1,3}/.match(f.read)[0].to_a[0] })
#!/usr/bin/env ruby -w if ARGV.empty? puts <<-T Locate IP by haqu usage: ./lip.rb ip|domain ... T exit end require 'net/http' require 'uri' uri = URI.parse('http://www.maxmind.com/app/locate_ip') res = Net::HTTP.post_form(uri, { 'ips' => ARGV.join(' '), 'type' => '', 'u' => '', 'p' => '' } ) fstr = res.body fstr.gsub!("Edition Results<\/span><p>","CHECKPOINT") fstr =~ /CHECKPOINT(.+?)<\/table>/m fields = $1.grep(/<(th|td)>/) fields.each do |f| f.strip! f.gsub!(/<[^>]+>/,"") end (0...13).each do |i| puts ". #{fields[i]}: #{fields[i+13]}" end maplink = "http://maps.google.com/maps?q=#{fields[20]},+#{fields[21]}&iwloc=A&hl=en" puts ". Google Maps URL: #{maplink}"
#!/usr/bin/env ruby # -*- coding: utf-8 -*- # $Hg: range_cidr.rb,v af9566d89389 2007-04-12 20:28 +0400 $ # (C) 2007 under terms of LGPL v2.1 # by Vsevolod S. Balashov <vsevolod@balashov.name> # # backported from perl code # http://www.irbs.net/internet/postfix/0401/att-3032/cidr_range.pl.gz def range_cidr(first, last, &block) if first < last idx1 = 32 idx1 -= 1 while first[idx1] == last[idx1] prefix = first >> idx1+1 << idx1+1 idx2 = 0 idx2 += 1 while idx2 <= idx1 and first[idx2] == 0 and last[idx2] == 1 if idx2 <= idx1 range_cidr(first, prefix | 2**idx1-1, &block) range_cidr(prefix | 1 << idx1, last, &block) else yield prefix, 32-idx2 end else yield first, 32 end end
#!/usr/bin/env ruby # $Hg: range2cidr.rb,v 2142c33ada8b 2007-04-11 23:14 +0400 $ # (C) 2007 under terms of GPL v2 # by Vsevolod S. Balashov <vsevolod@balashov.name> # # example usage of range_cidr.rb require 'lib/range_cidr' require 'ipaddr' require 'socket' if __FILE__ == $0 if ARGV.size == 2 range_cidr(IPAddr.new(ARGV[0]).to_i, IPAddr.new(ARGV[1]).to_i) { |subnet, mask| puts "#{IPAddr.new(subnet, Socket::AF_INET).to_s}/#{mask}" } else puts "usage: range2cidr <first_ip> <last_ip>" puts "example: range2cidr 192.168.1.0 192.168.2.255" end end
$ ruby range2cidr.rb 192.168.1.0 192.168.2.255 192.168.1.0/24 192.168.2.0/24
package org.socketdemo; import javax.microedition.io.Connector; import javax.microedition.io.SocketConnection; import javax.microedition.lcdui.Alert; import javax.microedition.lcdui.AlertType; import javax.microedition.lcdui.Command; import javax.microedition.lcdui.CommandListener; import javax.microedition.lcdui.Display; import javax.microedition.lcdui.Displayable; import javax.microedition.midlet.MIDlet; import javax.microedition.midlet.MIDletStateChangeException; public class SocketDEMO extends MIDlet implements CommandListener { protected SocketDEMO midlet = this; private Alert info; protected void destroyApp(boolean value) throws MIDletStateChangeException { notifyDestroyed(); } protected void pauseApp() { } protected void startApp() throws MIDletStateChangeException { new Thread() { public void run() { SocketConnection socket = null; try { socket = (SocketConnection) Connector.open("socket://193.204.114.233:13"); socket.openInputStream(); info = new Alert("Info", "Current IP: " + socket.getLocalAddress() + "\nPort: " + socket.getLocalPort(), null, AlertType.INFO); info.setTimeout(Alert.FOREVER); info.setCommandListener(midlet); getDisplay().setCurrent(info); } catch(Exception error) { info = new Alert("Info", "Current IP: N/A\nPort: N/A", null, AlertType.INFO); info.setTimeout(Alert.FOREVER); info.setCommandListener(midlet); getDisplay().setCurrent(info); } finally { if(socket != null) { try { socket.close(); } catch(Exception error) { } } } } }.start(); } protected Display getDisplay() { return Display.getDisplay(this); } public void commandAction(Command cmd, Displayable dsp) { if(cmd == Alert.DISMISS_COMMAND) { try { destroyApp(true); } catch(MIDletStateChangeException error) { } } } }
#!/usr/bin/env python __author__="Andrew Pennebaker (andrew.pennebaker@gmail.com)" __date__="21 Dec 2005 - 3 May 2006" __copyright__="Copyright 2006 Andrew Pennebaker" __license__="GPL" __version__="0.3" __URL__="http://snippets.dzone.com/posts/show/3542" import HashFunction class BSD(HashFunction.HashFunction): BLOCK_SIZE=1 DIGEST_SIZE=2 INIT=0x00 SUM_REQ="Sum >= 0" TEST_DATA="abc" TEST_HASH=0x40ac def __init__(self, sum=0x00): self.sum=sum def sumValid(self, sum): return sum>=0 def rotate(self, b): if (b&1)!=0: return (b>>1)+0x8000 return b>>1 def _update(self, b): self.sum=(self.rotate(self.sum)+b)&0xffff def digest(self): return self.sum def format(self, data): return "%05d" % (data) def unformat(self, hash): return int(hash) if __name__=="__main__": HashFunction.main(BSD)
arping -U -I <interface> -s <my_ip> <ip_router>
import pycurl def nullFunc(args): pass try: curl = pycurl.Curl() curl.setopt(pycurl.WRITEFUNCTION, nullFunc) curl.setopt(pycurl.URL, 'http://www.google.it') curl.perform() print "Server SU" except Exception, error: print "Server GIU'"
import urllib url = urllib.URLopener() resp = url.open('http://myip.dk') html = resp.read(114) end = html.find("</title>") start = html.find("IP:") + 3 print html[start:end].strip()
#!/usr/bin/perl -w my @ifconfig = `/sbin/ifconfig eth0`; for( @ifconfig ) { if( /(\d+\.\d+\.\d+\.\d+)/ ) { print "IP : $1\n"; last; } }
import cgi import os print "Content-type: text/html" print "" print cgi.escape(os.environ["REMOTE_ADDR"])