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

相关文章
相关标签/搜索