-3

I'm creating a program that reads lines from a file and tries to connect to a randomly generated "proxy." What I'm trying to do is read the lines, if the connection errors out, either;

  • Save them to a file called proxies_to_check.txt
  • Or save them to a file called bad_proxies.txt

It works how it's suppose to, it actually works pretty well I'm kind of impressed with myself. However, while it's saving to the file it saves the ip, with the port, like so:

143.54.67.231:6543
143.23.567.23:3452
9.234.21.124:5432

What I want to do is just save the ip to the file like so:

143.54.67.231
143.23.567.23
9.234.21.124

I've tried a few things, like using a regex, and striping the line (I looked up striping and it doesn't do what I thought it did), how can I go about grabbing all the digits and periods before the semi-colon?

Source:

def check_possibles
  puts "Testing possible proxies, this will take awhile..".green.bold
  IO.read("possible_proxies.txt").each_line do |proxy|
    begin
      Timeout::timeout(6) do
        begin
          open("http://#{proxy.chomp}")
          end
          File.open("true_proxies.txt", "a+") {|s| s.puts(proxy)}
        end
        rescue Errno::ENETUNREACH, Errno::EADDRNOTAVAIL
          File.open("bad_proxies.txt", "a+"){|s| s.puts("Bad IP => #{proxy}")}
    rescue Timeout::Error, Errno::ECONNREFUSED
      File.open("proxies_to_check.txt", "a+") {|s| s.puts(proxy)}
      next
    end
  end
end
13aal
  • 1,634
  • 1
  • 21
  • 47

1 Answers1

0
 "143.54.67.231:6543".split(":")[0..-2].join

OR

"143.54.67.231:6543".split(":").first

OR maybe this will helps you if you need RGX

Is it a valid Regular expression for IP address

Community
  • 1
  • 1
Lukas Baliak
  • 2,849
  • 2
  • 23
  • 26