- 论坛徽章:
- 0
|
ruby实例
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 模拟点击一下
复制代码 |
|