Last active
December 31, 2015 20:19
-
-
Save r4um/8039580 to your computer and use it in GitHub Desktop.
ACT Autologin.
This file contains hidden or 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/bin/ruby | |
require 'logger' | |
require 'optparse' | |
require 'net/http' | |
require 'uri' | |
class ACTLogin | |
@@logger = Logger.new(STDOUT) | |
@@logger.level = Logger::INFO | |
def self.login(args) | |
opt = {} | |
opt[:host] = 'portal.acttv.in' | |
opt[:timeout] = 10 | |
opt[:auth] = nil | |
opt[:debug] = false | |
op = OptionParser.new do |op| | |
op.banner = "Usage: #{__FILE__} [options]" | |
op.separator "" | |
op.on("-h", "--host HOST", "Login HOST [#{opt[:host]}]") do |h| | |
opt[:host] = h | |
end | |
op.on("-a", "--auth AUTH", "Login USER:PASSWORD") do |a| | |
opt[:auth] = a | |
end | |
op.on("-t", "--timeout SECONDS", "Timeout for operations [#{opt[:timeout]}]") do |t| | |
opt[:timeout] = t.to_i | |
end | |
op.on("-d", "--debug", "Set log level to debug [#{opt[:debug]}]") do | |
opt[:debug] = true | |
end | |
op.on("-h", "--help", "Show this message") do | |
puts op | |
exit | |
end | |
end | |
begin | |
op.parse!(args) | |
unless opt[:auth] | |
@@logger.error "please provide auth via -a" | |
exit 1 | |
end | |
uri = URI("http://#{opt[:host]}") | |
ua = {'User-Agent' => 'Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1)'} | |
http = Net::HTTP.new(uri.host, uri.port) | |
http.read_timeout = opt[:timeout] | |
if opt[:debug] | |
@@logger.level = Logger::DEBUG | |
end | |
res = http.get('/web/blr/home', ua) | |
cookies = res.get_fields('set-cookie') | |
unless cookies | |
@@logger.error "No cookies recieved" | |
exit 1 | |
end | |
send_cookies = [] | |
cookies.each do |c| | |
send_cookies.push(c.split('; ')[0]) | |
end | |
send_cookies = send_cookies.join("; ") | |
if res.body =~ /You.are.logged.in.as/ | |
@@logger.info "already logged in" | |
exit | |
end | |
post_url = nil | |
if res.body =~ /form action=(.*) method="POST"/ | |
post_url = $1 | |
post_url.gsub!(/"/, '') | |
end | |
user_ip = nil | |
if res.body =~ /value=(.*) name="userIP"/ | |
user_ip = $1 | |
user_ip.gsub!(/"/, '') | |
end | |
@@logger.debug "Got post_url=#{post_url} user_ip=#{user_ip}" | |
user, pass = opt[:auth].split(/:/) | |
req_uri = URI.parse(post_url).request_uri | |
req = Net::HTTP::Post.new(req_uri) | |
req['Cookie'] = send_cookies | |
req['User-Agent'] = ua['User-Agent'] | |
req.set_form_data('uname' => user, 'pword' => pass, 'userIP' => user_ip) | |
res = http.request req | |
if res.body =~ /You.are.logged.in.as/ | |
@@logger.info "login successful" | |
else | |
@@loggerl.info "login failed" | |
end | |
rescue SystemExit | |
exit | |
rescue Exception => e | |
@@logger.error "exception #{e.class} #{e.message}" | |
exit 1 | |
end | |
end | |
end | |
ACTLogin.login(ARGV) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment