Created
September 22, 2016 15:20
-
-
Save evianzhow/dd8cf0cfe5fed3a89cebc242c4146408 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/local/env ruby | |
require 'open-uri' | |
require 'uri' | |
require 'csv' | |
files = open('pw.txt').read | |
csv = [] | |
csv << ['name', 'url', 'username', 'password'] | |
files.each_line do |line| | |
line = line[0..-1] # Remove trailing "\n" | |
components = line.split(':') | |
if components.count > 3 | |
# try to rescue | |
if components[-2].to_i != 0 | |
# similar to 'username:url:port:password' format | |
components = [ | |
components[0..-4].join(':'), | |
components[-3..-2].join(':'), | |
components[-1] | |
] | |
elsif | |
# similar to 'username:suffix:url:password' format | |
components = [ | |
components[0..-3].join(':'), | |
components[-2], | |
components[-1] | |
] | |
end | |
elsif components.count < 3 | |
# malformed data | |
next | |
end | |
username = components[0] | |
begin | |
url = URI('http://' + components[1]) | |
rescue Exception => e | |
next | |
end | |
password = components[2] | |
csv << [url.host, url, username, password] | |
end | |
csv_string = CSV.generate do |c| | |
csv.each do |i| | |
c << i | |
end | |
end | |
File.write('pw.csv', csv_string) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment