ruby實例(不斷增長中...)

1.抓取百度新歌Top100的歌名及歌手名單php

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
endhtml

2.多線程端口掃描器ruby

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}"socket

4.多線程下載ui

#多線程批量下載http://www.milw0rm.com/上的exploits,自動存放。url

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.模擬登陸頁面htm

#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 模擬點擊一下ip

相關文章
相關標籤/搜索