我有一个程序,产生随机的IP地址,并试图平他们和添加端口到他们,我将使用这个工具进行笔试。我让它在某一点上工作,并决定为了找到使用哪种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现在,这确实找到了正确的OS (我肯定有更好的方法可以这样做,如果您知道其中一个,请让我知道),但我关心的是,在程序全面运行时,当提示它时,它实际上并不使用@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不能正常运行的地方,我做了什么错事,以及如何修复它?
发布于 2016-03-25 17:25:35
实际上,您并没有执行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中执行该命令。您需要使用backticks或系统方法system @ping来实现这一点。这将在shell中执行@ping字符串。
https://stackoverflow.com/questions/36222041
复制相似问题