author | Rodrigo Campos
<rodrigo@sdfg.com.ar> 2013-09-16 18:44:38 UTC |
committer | Rodrigo Campos
<rodrigo@sdfg.com.ar> 2013-09-16 20:20:26 UTC |
README.rst | +24 | -0 |
batrat | +47 | -0 |
crous-login.py | +76 | -0 |
notifyme | +22 | -0 |
diff --git a/README.rst b/README.rst new file mode 100644 index 0000000..7447ad6 --- /dev/null +++ b/README.rst @@ -0,0 +1,24 @@ +This is just a collection of scripts that I might have found useful at some +moment. All of them are relased under the BOLA_ license. + +.. _BOLA: http://blitiri.com.ar/p/bola/ + +If you have any comments, patches or suggestions let me know at: +rodrigo@sdfg.com.ar. Please keep in mind that most should be a 10 minutes hack +(or even less :D), so the code might not be the prettiest one :-) + +Here is a list of some of the scripts you can find: + + * ``notifyme``: Run any command and open an X window to notify when it + finishes. You can also snooze for 1 minute. + + * ``crous-login``: The CROUS_ students residence force you to fill a form every + time you want to login and every N hours. This scripts just fills the forms + for you whenever needed. + + * ``batrat``: Check if the system is running on AC or batteries and print to + stdout whenever the system switches from AC to BAT or viceversa. Uses inotify + to detect switches. + + +.. _CROUS: http://www.crous-strasbourg.fr diff --git a/batrat b/batrat new file mode 100755 index 0000000..590c843 --- /dev/null +++ b/batrat @@ -0,0 +1,47 @@ +#!/usr/bin/env sh +# +# batrat - Detect switches from AC to BAT or viceversa and print them to stdout +# Rodrigo Campos <rodrigo@sdfg.com.ar> + +# Creates the var BATS with a list of the paths to the BAT dir in +# /sys/class/power_supply/ and also creates the var ADPS with a list of the +# paths to the ADP dir in /sys/class/power_supply/ +load_power_supplies() { + for i in /sys/class/power_supply/*/type; do + + i_dir=$(dirname $i) + i_type=$(cat $i) + + if [ "$i_type"x = "Batteryx" ]; then + BATS="$BATS $i_dir" + fi + if [ "$i_type"x = "Mainsx" ]; then + ADPS="$ADPS $i_dir" + fi + done +} + +# sets running_on +detect_curr_supply() { + load_power_supplies + running_on="BAT" + + for ac in $ADPS; do + if cat ${ac}/online | grep -q 1; then + running_on="AC" + return + fi + done +} + +supply_change() { + detect_curr_supply + echo $running_on + + while inotifywait -qq -e access /sys/class/power_supply/*/online; do + detect_curr_supply + echo $running_on + done +} + +supply_change diff --git a/crous-login.py b/crous-login.py new file mode 100755 index 0000000..5272f3a --- /dev/null +++ b/crous-login.py @@ -0,0 +1,76 @@ +#!/usr/bin/env python + +""" +crous-login: Fill the forms needed to login to the crous osiris network. +Rodrigo Campos <rodrigo@sdfg.com.ar> +""" + +import requests +import time +import subprocess + +USER="XXX" +PASS="YYY" + + +def login(user, password): + + #subprocess.check_call("killall dhclient ; dhclient -v eth0", shell=True) + time.sleep(1) + + r = requests.get("http://google.com") + if not "/webauth/loginprocess" in r.text: + print "Not the expected form. Already conected ?" + return + + print "Got the form..." + + payload = { "user": user, "pass": password } + r = requests.post("http://google.com/webauth/loginprocess", data=payload) + + # Print just in case it says some wierd error + print r.text + + # Wait till the access is granted + while 'granted' not in r.text.lower(): + # Two seconds is the time used in the META header to refresh, so we wait + # the same (and if they change it, fuck it :D) + time.sleep(2) + r = requests.get("http://google.com/webauth/statusprocess") + + print "granted! Waiting to activate..." + time.sleep(10) + #subprocess.check_call("killall dhclient ; dhclient -v eth0", shell=True) + +def connect(use, password): + + while True: + try: + login(USER, PASS) + except: + print "Error while trying to login, trying again" + time.sleep(2) + continue + + break + +def are_connected(): + + try: + subprocess.check_call("ping -c 2 8.8.8.8 > /dev/null", shell=True) + return True + except: + return False + +if __name__ == "__main__": + + while True: + if not are_connected(): + print "Not connected! Trying to connect..." + connect(USER, PASS) + + try: + time.sleep(60) + except KeyboardInterrupt: + print "Checking connection again..." + diff --git a/notifyme b/notifyme new file mode 100755 index 0000000..5b12383 --- /dev/null +++ b/notifyme @@ -0,0 +1,22 @@ +#!/usr/bin/env sh +# +# notifyme - Run any command and open an X window to notify when it finishes +# Rodrigo Campos <rodrigo@sdfg.com.ar> +# +# $1..$N: the command you want to run and its arguments +# +# For example, run like this: +# notifyme make +# +# And it will open an X window when the command finishes. You can also click on +# snooze to be notified again in 1 minute. +# +# It uses xmessage, present in x11-utils, so you probably already have it. + +eval "$@" +ret=$? + +notify_cmd="xmessage -nearmouse -buttons okay:0,snooze:2 Finished with retcode $ret when running: $@" +until $notify_cmd; do + sleep 60 +done