1.抓取百度新歌Top100的歌名及歌手名單
require "open-uri"
require "hpricot"
doc = open("http://list.mp3.baidu.com/list/newhits.html?id=1#top1") { |f| Hpricot(f) }
doc.search(".border").each do |table|
table.search("a").each do |link|
print link.inner_html
end
puts
end
2.多線程端口掃描器
require 'socket'
include Socket::Constants
ports = (1..1024).to_a
threads = []
time1 = Time.now
for port in ports
threads << Thread.new(port) do |theport|
begin
sock = Socket.new(AF_INET, SOCK_STREAM, 0)
sockaddr = Socket.pack_sockaddr_in(theport, 'localhost')
sock.connect(sockaddr)
puts "Port:#{theport} is Opend! "
sock.close
rescue
#...
end
end
end
threads.each {|thr| thr.join}
puts " 共耗時:#{Time.now - time1}秒"
3.查看外網IP地址
require 'socket'
require 'open-uri'
inner_ip = Socket.getaddrinfo(Socket.gethostname, Socket::AF_INET)[0][3]
html = URI.parse("http://www.baidu.com").read
outer_ip = html.scan(/(([0-9]{1,3}.?){4})/).flatten.first
puts "內網IP地址:#{inner_ip}"
puts "外網IP地址:#{outer_ip}"
4.多線程下載
#多線程批量下載http://www.milw0rm.com/上的exploits,自動存放。
require "open-uri"
if $*[0]==nil or $*[1]==nil or $*[2]==nil
abort "用法示例:ruby #$0 開始數 結束數 存放的目錄 EX:如ruby #$0 200 300 c:\\1 "
end
time1 = Time.now
threads = []
for i in $*[0]..$*[1]
exploits= "http://www.milw0rm.com/exploits/"+i.to_s
threads << Thread.new(i) do |thei|
begin
data=open(exploits){|f|f.read}
open("#{$*[2]}\\#{thei}.htm","wb"){|f|f.write(data)}
print thei,"-"
rescue
#...如果沒有這個url,顯示404不去管它,讓它沒有錯誤回顯
end
end
end
threads.each {|thr| thr.join}
puts " 下載完成,共耗時:#{Time.now - time1}秒"
4.模擬登錄頁面
#coding: utf-8 #登錄出現中文才需要使用
require "win32ole" #包含庫
ie = WIN32OLE.new('internetExplorer.Application')
ie.visible = true #這個時候就可以看到一個ie的界面出來了
ie.navigate('http://localhost/login.php') #轉到這個頁面
sleep(0.1) until ie.busy == false #sleep 直到ie.busy為false 頁面完全載入為止
ie.Document.getElementById("username").value = "admin" #輸入賬戶名
ie.Document.getElementById("password").value = "123456" #輸入密碼
ie.Document.getElementById("submit").click #登錄按鈕的id是btn1 模擬點擊一下