Monday, May 30, 2011

How to “flush tor circuit”

tor already switches the route roughly every 10 minutes. Sometimes you may need to get new tor ip or new route programatically. Here is how to do it in ruby.


What we are doing is "Send a SIGHUP to Tor. According to its man page this should make Tor fetch a new directory".
We can send a SIGHUP to tor get new route, thus a new tor ip.



require 'rubygems'
require 'sys/proctable' 
include Sys



  Sys::ProcTable.ps.each { |ps|
    if ps.name.downcase == "tor"      
      Process.kill('SIGHUP', ps.pid)
    end
  }

you can schedule this script to run periodically, say every 1 minutes. Then you will be getting new tor ip every minutes.


https://svn.torproject.org/svn/incognito/trunk/root_overlay/etc/NetworkManager/dispatcher.d/50-tor-sighup.sh

Kill process by his name ruby


require 'rubygems'
require 'sys/proctable'
include Sys



Sys::ProcTable.ps.each { |ps|
    if ps.name.downcase == "process name"    
      Process.kill('KILL', ps.pid)
    end
  }

#Run script as root user

A Ruby interface for gathering process information on your operating system

Ruby task schedular

Exit from rufus-scheduler ruby

you can use "scheduler.stop" to stop the rufus/scheduler execution.
 require 'rubygems'
require 'rufus/scheduler'

scheduler = Rufus::Scheduler.start_new

scheduler.in '1m' do
puts "hhhhhhhhhhhaaaaaaaaaaaaiiiiiiiiiiiiiiii"
if (some condition)
scheduler.stop
end
end

scheduler.join

Parse media:content tag using Nokogiri

I need to extract the URL from this tag:



"media:content url="http://video.ted.com/talk/podcast/2011/None/MikeMatas_2011.mp4" fileSize="15533795" type="video/mp4" />"


doc = Nokogiri::XML(open("http://www.ted.com/talks/rss"))
doc.css('item').each do |item|
   puts item.at_xpath('media:content')['url']
end

Thursday, May 26, 2011

Protect Your Web Server From Spambots

Grouping array ruby

Create arrays inside an array.



require 'enumerator'

>> a = (0..12).to_a
=> [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]
>> b = a.enum_for(:each_slice,4).to_a
=> [[0, 1, 2, 3], [4, 5, 6, 7], [8, 9, 10, 11], [12]]
below is how to iteration
>> b.each do |c|
?> c.each do |value|
?> puts value
>> end
>> end

Redis and Resque example application ruby

Sent email using gmail smtp in ruby


require 'pony'
Pony.mail(:to => 'real_mail@yahoo.com', :via => :smtp, :via_options => {
   
:address => 'smtp.gmail.com',
   
:port => '587',
   
:enable_starttls_auto => true,
   
:user_name => 'id_gmail',
   
:password => 'parola_gmail',
   
:authentication => :plain,
   
:domain => "HELO",
},
:subject => 'Test email', :body => 'Test for Pony email through gmail SMTP server.')

Using Redis as a Key-Value Store

Lists and Sets in Redis

Redis hash usage

User signup and login using redis

file utility methods for copying, moving, removing, etc in ruby

Creates a directory and all its parent directories ruby

require 'fileutils'


Creates a directory and all its parent directories. For example,
FileUtils.mkdir_p '/usr/local/lib/ruby'
causes to make following directories, if it does not exist.
* /usr
* /usr/local
* /usr/local/lib
* /usr/local/lib/ruby
You can pass several directories at a time in a list.