Retrying with different port number
<p>I have a program that will ping an IP address, then log the IP to a file if it pings successfully:</p>

<pre><code>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
</code></pre>

<p>Now what I would like to do is have the "good" IP address use each port in the following array: <code>port = %w(80 3128 8080 8090 8888 8898 9999)</code>, for example:</p>

<pre><code>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
</code></pre>

<p>I think I have a general idea on how I could do this:</p>

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

<p>I'm not entirely sure if that would work the way I'm expecting it to though, and even if it does, I'm 100% sure there's a better way to do, any help with this would be greatly appreciated, thank you.</p>

<p>Source:</p>

<pre><code>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
</code></pre>

<hr>

<p><strong>EDIT:</strong></p>

<p>I attempted my general idea of how to do this:</p>

<pre><code>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
</code></pre>

<p>When run:</p>

<pre><code>[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
</code></pre>

<p>It ends up outputting information that looks like this:</p>

<pre><code>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
</code></pre>