程序没有ping生成的IPS只是通过它们运行

时间:2016-03-25 14:39:13

标签: ruby ping

我有一个程序可以生成随机IP地址并尝试ping它们并为它们添加端口,我将使用此工具进行笔测试。我已经让它在某一点工作,并决定为了找到将使用哪种ping方法,程序将首先发现用户正在运行的操作系统并以这种方式确定ping方法;有了这个:

def self.windows?
  return File.exist? "c:/WINDOWS" if RUBY_PLATFORM == 'java'
  RUBY_PLATFORM =~ /mingw32/ || RUBY_PLATFORM =~ /mswin32/
end

def self.linux?
  return File.exist? "/usr" if RUBY_PLATFORM == 'java'
  RUBY_PLATFORM =~ /linux/
end

def self.os
  return :linux if self.linux?
  return :windows if self.windows?
  nil
end

def check_os
  if windows?
    @ping = `ping -n 1 #{@ip}`
    check_file
  elsif linux?
    @ping = `ping -c 1 #{@ip}`
    check_file
  else
    @ping = `ping -c 1 #{@ip}`
    check_file
  end
end

现在这确实找出了正确的操作系统(我确定有更好的方法来做到这一点,如果你知道一个让我知道),但我关心的是那个在对程序进行完整运行时提示它时,它实际上并没有使用@ping命令。相反它所做的只是连续循环而不是ping IP地址。 (它假设循环直到大约3500个IP')

源代码:

require 'colored'
require 'timeout'

def self.windows?
  return File.exist? "c:/WINDOWS" if RUBY_PLATFORM == 'java'
  RUBY_PLATFORM =~ /mingw32/ || RUBY_PLATFORM =~ /mswin32/
end

def self.linux?
  return File.exist? "/usr" if RUBY_PLATFORM == 'java'
  RUBY_PLATFORM =~ /linux/
end

def self.os
  return :linux if self.linux?
  return :windows if self.windows?
  nil
end

def check_os
  if windows?
    @ping = `ping -n 1 #{@ip}`
    check_file
  elsif linux?
    @ping = `ping -c 1 #{@ip}`
    check_file
  else
    @ping = `ping -c 1 #{@ip}`
    check_file
  end
end

def check_file
  if File.exist?("proxies.txt")
    File.truncate("proxies.txt", 0)
    puts "\n[NOTICE]File exists in system, resuming process.\n".blue.bold
    create_possibles
  else 
    puts "\n[NOTICE]File proxies.txt created successfully.\n".blue.bold
    File.new("proxies.txt")
    create_possibles
  end
end

def create_possibles
  puts "\n[NOTICE]Attempting to ping generated IP addresses.\n".blue.bold


  ports = %w(80 443 1935 2222 3128 3130 7808 8080 8081 8085 8089 
             8090 8102 8104 8106 8118 8119 8123 8888 8898 9000 
             9090 9797 9999 10000 10052 10053 10059 10088 12345 
             18000 18001 18008 37564 40080 55336 59998
            )
  100.times do
    @ip = Array.new(4){rand(256)}.join('.')
    begin
      Timeout::timeout(5) do 
        @ping
        if @ping =~ /Received = 1/ 
          puts "[SUCCESS]Possible proxies created for IP: #{@ip}".green.bold
          File.open("proxies.txt", "a+") do |proxy|
            ports.each { |port| proxy.puts("#{@ip}:#{port}") }
          end
        else
          puts "[ERROR]IP failed to ping: #{@ip}".red.bold
        end
      end
    rescue Timeout::Error, Errno::ENOBUFS
      puts "[WARNING]IP timed out: #{IP}".yellow.bold
      next
    end
  end
  check_for_amount_of_proxies
end

def check_for_amount_of_proxies
  if File.size("proxies.txt") >= 71769
    puts "\n[NOTICE]Proxies created, attempting connection\n".blue.bold
    system("proxies-scanner -f proxies.txt")
    print "[NOTICE]Truncating file: proxies.txt".blue.bold
  else
    puts "\n[NOTICE]File doesn't contain enough proxies, restarting IP finding proccess.\n".blue.bold
    create_possibles
  end
end

所以我的问题是,我在@ping赢得正常运行的地方做错了什么,以及如何解决?

1 个答案:

答案 0 :(得分:1)

您实际上并未执行ping命令。在这一部分:

Timeout::timeout(5) do 
    @ping # this isn't doing anything
    if @ping =~ /Received = 1/ 
      puts "[SUCCESS]Possible proxies created for IP: #{@ip}".green.bold
      File.open("proxies.txt", "a+") do |proxy|
        ports.each { |port| proxy.puts("#{@ip}:#{port}") }
      end
    else
      puts "[ERROR]IP failed to ping: #{@ip}".red.bold
    end
  end

我假设你期望带有@ping变量的行在shell中执行命令。您需要使用反引号或系统方法system @ping来完成此操作。这将在shell中执行@ping字符串。