Skip to content

Instantly share code, notes, and snippets.

@r4um
Created April 18, 2012 13:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save r4um/2413568 to your computer and use it in GitHub Desktop.
Save r4um/2413568 to your computer and use it in GitHub Desktop.
Reliance broadband autologin (suitable for run in cron)
#!/usr/bin/ruby
require 'optparse'
require 'net/http'
require 'uri'
class RelianceLogin
def self.login(args)
opt = {}
opt[:host]='220.224.142.229'
opt[:timeout]=10
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") do |t|
opt[:timeout] = t.to_i
end
op.on_tail("-h", "--help", "Show this message") do
puts op
exit
end
end
begin
op.parse!(args)
raise OptionParser::MissingArgument, "-a, no auth given."\
if not opt[:auth]
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]
res = http.get('/reliance/startportal_isg.do?CPURL=http://www.google.com', ua)
cookies = res.get_fields('set-cookie')
send_cookies = []
cookies.each do |c|
send_cookies.push(c.split('; ')[0])
end
send_cookies = send_cookies.join("; ")
if res.body =~ /logout.do/
puts "#{Time.now.to_s} Already logged in"
exit
end
user, pass = opt[:auth].split(/:/)
req = Net::HTTP::Post.new('/reliance/login.do?action=doLoginSubmit')
req['Cookie'] = send_cookies
req['User-Agent'] = ua['User-Agent']
req.set_form_data('userId' => user, 'password' => pass)
res = http.request req
if res.body =~ /logout.do/
puts "#{Time.now.to_s} Login successful"
else
puts "#{Time.now.to_s} Login failed"
end
rescue SystemExit
exit
rescue Exception => e
puts "#{Time.now.to_s} Exception #{e.class} #{e.message}"
exit
end
end
end
RelianceLogin.login(ARGV)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment