root@ubuntu:~# vi check_ip.rb require 'rubygems' index = 1 max = 20 while (max-index) >= 0 puts index name = %x(ip route | head -n 1 | awk '{printf $5}') ips = %x(ip address | grep #{name} | awk '{print $2}').split("\n").map {|a| a.split("/").first} max = ips.size-1 ip = ips[index] route = %x(ip route | head -n 1) if route =~ /via/ cmd = "ip route change default via #{route.split(' ')[2]} dev #{name} src #{ip}" cmd = system cmd else cmd = "ip route change default dev #{name} src #{ip}" cmd = system cmd end puts "change ip: #{cmd}"
if cmd result = system 'curl --connect-timeout 10 http://119.28.20.165:8777/.json ' File.open("./result.txt","a") do |file| file.puts "#{ip}:#{result}" end index += 1 end end puts "done" ----------------------------------------------------------------------------------------------------- centos: yum -y install ruby ubuntu:sudo apt-get install ruby root@ubuntu:~# ruby check_ip.rb 用途:检测机器中所有IP段是否能够访问一个网站。结果显示在result.txt里面 ubuntu 已经测试可用
检测是否已经安装了ruby
rpm -qa | grep ruby yum list | grep ruby
|
请发表评论