首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >程序,而不是pinging生成的IPS,只是通过它们运行。

问程序,而不是pinging生成的IPS,只是通过它们运行。
EN

Stack Overflow用户
提问于 2016-03-25 14:39:12
回答 1查看 22关注 0票数 0

我有一个程序,产生随机的IP地址,并试图平他们和添加端口到他们,我将使用这个工具进行笔试。我让它在某一点上工作,并决定为了找到使用哪种ping方法,程序首先会发现用户正在运行什么操作系统,并以这种方式确定ping方法:

代码语言:javascript
复制
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

现在,这确实找到了正确的OS (我肯定有更好的方法可以这样做,如果您知道其中一个,请让我知道),但我关心的是,在程序全面运行时,当提示它时,它实际上并不使用@ping命令。相反,它所做的只是不断循环,而不是平IP地址。(应该一直循环到找到大约3500个IP为止)

源代码:

代码语言:javascript
复制
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不能正常运行的地方,我做了什么错事,以及如何修复它?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2016-03-25 17:25:35

实际上,您并没有执行ping命令。在本部分:

代码语言:javascript
复制
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中执行该命令。您需要使用backticks或系统方法system @ping来实现这一点。这将在shell中执行@ping字符串。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/36222041

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档