Skip to content

Instantly share code, notes, and snippets.

@jittat
Created August 5, 2009 03:10
Show Gist options
  • Save jittat/162470 to your computer and use it in GitHub Desktop.
Save jittat/162470 to your computer and use it in GitHub Desktop.
# Console login script for Kasetsart University Internet login page.
# It uses Curl to load pages and post data.
# To use it, you need ruby (sure!) and curl installed.
def build_hidden_option_list(html)
list = []
tags = html.scan(/<input[^>]*hidden[^>]*>/)
tags.each do |tag|
res = tag.match(/name='([^']*)' .*value='(.*)'/)
list << [res[1],res[2]]
end
return list
end
# taken from HighLine
def raw_no_echo_mode
state = `stty -g`
system "stty -echo"
state
end
def restore_mode(state)
system "stty #{state}"
end
def read_username_and_password
print "Username: "
username = gets.chomp
print "Password: "
oldstate = raw_no_echo_mode
begin
password = gets.chomp
ensure
restore_mode oldstate
end
puts
[username,password]
end
puts "Reading login source.."
system 'curl -D cookies -k https://login1.ku.ac.th/ -A "Mozilla/5.0 (X11; U; Linux i686\
; en-US; rv:1.9.0.3) Gecko/2008092510 Ubuntu/8.04 (hardy) Firefox/3.0.3" > login.html 2> /dev/null'
options = build_hidden_option_list(open("login.html").read)
username, password = read_username_and_password
params = "username=#{username}&password=#{password}&zone=0"
options.each { |key,val| params += "&#{key}=#{val}" }
params += "&submit=Login"
#puts "Param: #{params}"
puts "Logging in.."
system 'curl -b cookies -d "'+params+'" -k -e "https://login1.ku.ac.th/" -i -L -A "Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.9.0.3) Gecko/2008092510 Ubuntu/8.04 (hardy) Firefox/3.0.3" "https://login1.ku.ac.th/index.php?content=login&referer=www.ku.ac.th&action=login" > login.log 2> /dev/null'
puts "Done."
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment