使用不同的端口号重试

Retrying with different port number

我有一个程序可以 ping 一个 IP 地址,然后在 ping 成功后将 IP 记录到一个文件中:

Invalid IP: 128.201.166.30
Proxy created: 66.25.173.128:80
Invalid IP: 225.195.111.59
Invalid IP: 249.133.221.70
Invalid IP: 40.21.11.99
Invalid IP: 201.27.136.108
Invalid IP: 152.77.109.45
Invalid IP: 120.76.159.122
Invalid IP: 108.244.67.42
Invalid IP: 73.231.16.193
Proxy created: 146.134.102.95:3128
Invalid IP: 133.216.1.59
Proxy created: 118.75.196.75:3128

现在我想要做的是让 "good" IP 地址使用以下数组中的每个端口:port = %w(80 3128 8080 8090 8888 8898 9999),例如:

Proxy created: 66.25.173.128:80
Proxy created: 66.25.173.128:3128
Proxy created: 66.25.173.128:8080
Proxy created: 66.25.173.128:8090
Proxy created: 66.25.173.128:8888
Proxy created: 66.25.173.128:8898
Proxy created: 66.25.173.128:9999
#Creates an IP with a port extension with each port number

我想我对如何做到这一点有一个大概的了解:

File.open("example.txt", "a+"){
  |s| s.puts("#{ip}:#{port[0]}", 
             "#{ip}:#{port[1]}",
             "#{ip}:#{port[2]}"
             #etc...
                              )}

我不完全确定这是否会像我期望的那样工作,即使是这样,我也 100% 确定有更好的方法,对此有任何帮助非常感谢,谢谢。

来源:

require 'colored'
require 'timeout'

def create_possibles
  port = %w(80 3128 8080 8090 8888 8898 9999).each do |port|
    10.times do 
      ip = Array.new(4){rand(256)}.join('.')
      Timeout::timeout(5) do
        ping = `ping -n 1 #{ip}` 
        if ping =~ /Received = 1/ 
          proxy = "#{ip}:#{port}"
          puts "Proxy created: #{proxy}".green.bold
          File.open("proxies.txt", "a+") {|s| s.puts(proxy)}
        else
          puts "Invalid IP: #{ip}".red.bold
          next
        end
      end
    end
  end
end

create_possibles

编辑:

我尝试了关于如何执行此操作的一般想法:

require 'colored'
require 'timeout'

def create_possibles
  w%(80 3128 8080 8090 8888 8898 9999).each do |port|
    1.times do 
      ip = Array.new(4){rand(256)}.join('.')
      Timeout::timeout(5) do
        ping = `ping -n 1 #{ip}` 
        if ping =~ /Received = 1/ 
#          proxy = "#{ip}:#{port}"           
          puts "[SUCCESS]Proxy created for IP: #{ip}".green.bold
          File.open("proxies.txt", "a+") {|s| s.puts("#{ip}:#{port[0]}",
                                                     "#{ip}:#{port[1]}",
                                                     "#{ip}:#{port[2]}",
                                                     "#{ip}:#{port[3]}",
                                                     "#{ip}:#{port[4]}",
                                                     "#{ip}:#{port[5]}",
                                                     "#{ip}:#{port[6]}",
                                                     "#{ip}:#{port[7]}")}
        else
          puts "[ERROR]IP failed to ping: #{ip}".red.bold
          next
        end
      end
    end
  end
end

create_possibles

当运行:

[ERROR]IP failed to ping: 185.105.73.104
[ERROR]IP failed to ping: 93.182.117.11
[ERROR]IP failed to ping: 112.210.73.187
[ERROR]IP failed to ping: 111.109.127.178
[SUCCESS]Proxy created for IP: 201.153.205.131
[ERROR]IP failed to ping: 128.236.57.123
[ERROR]IP failed to ping: 248.84.17.31

它最终输出如下所示的信息:

201.153.205.131:0
201.153.205.131:0
201.153.205.131:0
201.153.205.131:1
201.153.205.131:1
201.153.205.131:1
201.153.205.131:0
201.153.205.131:1

我想通了!问题与 .each do |ports|;

有关

如果我这样取出来:

require 'colored'
require 'timeout'

def create_possibles
  ports = %w(80 3128 8080 8090 8888 8898 9999)
    5.times do 
      ip = Array.new(4){rand(256)}.join('.')
      Timeout::timeout(5) do
        ping = `ping -n 1 #{ip}` 
        if ping =~ /Received = 1/ 
#          proxy = "#{ip}:#{port}"           
          puts "[SUCCESS]Proxy created for IP: #{ip}".green.bold
          File.open("proxies.txt", "a+") {|s| s.puts("#{ip}:#{ports[0]}",
                                                     "#{ip}:#{ports[1]}",
                                                     "#{ip}:#{ports[2]}",
                                                     "#{ip}:#{ports[3]}",
                                                     "#{ip}:#{ports[4]}",
                                                     "#{ip}:#{ports[5]}",
                                                     "#{ip}:#{ports[6]}")}
        else
          puts "[ERROR]IP failed to ping: #{ip}".red.bold
          next
        end
      end
    end
  end

create_possibles

然后运行照原样输出:

[ERROR]IP failed to ping: 111.20.77.200
[SUCCESS]Proxy created for IP: 217.252.149.35
[ERROR]IP failed to ping: 49.214.128.47
[ERROR]IP failed to ping: 116.101.28.115
[ERROR]IP failed to ping: 75.49.120.242

并将记录:

217.252.149.35:80
217.252.149.35:3128
217.252.149.35:8080
217.252.149.35:8090
217.252.149.35:8888
217.252.149.35:8898
217.252.149.35:9999