Advertisement
Guest User

Untitled

a guest
Dec 18th, 2014
153
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 1.92 KB | None | 0 0
  1. #!/bin/sh
  2.  
  3. CREDENTIALS="admin:password"
  4. IP="192.168.1.1"
  5. SESSION_ID=""
  6.  
  7. mode="$1"
  8.  
  9. get_session_id()
  10. {
  11.     local _attempts=5
  12.     local _iter=1
  13.  
  14.     local _response="" local _session_id="" local _test_multiuser="" local _kickout_session_id=""
  15.  
  16.     while [ $_iter -le $_attempts ]; do
  17.         _response=$(curl -s -u $CREDENTIALS http://{$IP}/LED_settings.htm)
  18.         _session_id=$(echo "$_response" | grep -oP "(?<=led_settings.cgi\?id=)[0-9]+")
  19.         if [ -n "$_session_id" ]; then
  20.             echo "$_session_id"
  21.             return 0
  22.         else
  23.             _test_multiuser=$(echo "$_response" | grep -o 'top.location.href = "MNU_access_multiLogin2.htm"')
  24.             if [ -n "$_test_multiuser" ]; then
  25.                 _kickout_session_id=$(curl -s -u $CREDENTIALS "http://${IP}/MNU_access_multiLogin2.htm" | grep -Po "(?<=multi_login.cgi\?id=)[0-9]+")
  26.                 curl -X POST --data "act=yes" -s -u $CREDENTIALS "http://${IP}/multi_login.cgi?id=$_kickout_session_id" >/dev/null
  27.             fi
  28.         fi
  29.         sleep 1
  30.         _iter=$(( $_iter + 1 ))
  31.     done
  32.  
  33.     echo "Failed to get session ID!" 1>&2
  34.     exit 1
  35. }
  36.  
  37. set_mode()
  38. {
  39.     local _mode_value="$1"
  40.     local _response="$(curl -s -X POST -u $CREDENTIALS --data "led_settings=${_mode_value}" "http://${IP}/led_settings.cgi?id=${SESSION_ID}")"
  41.     local _success=$(echo "$_response" | egrep -o "INPUT name=led_now type=hidden value=\"${_mode_value}\" ")
  42.     if [ -z "$_success" ]; then
  43.         echo "LED status change unsuccessful" 1>&2
  44.         exit 1
  45.     fi
  46. }
  47.  
  48. case $mode in
  49.     "off")
  50.         SESSION_ID=$(get_session_id)
  51.         set_mode "turn_off"
  52.         ;;
  53.     "blink")
  54.         SESSION_ID=$(get_session_id)
  55.         set_mode "en_blink"
  56.         ;;
  57.     "noblink")
  58.         SESSION_ID=$(get_session_id)
  59.         set_mode "dis_blink"
  60.         ;;
  61.     *)
  62.         echo "USAGE: $0 <blink|noblink|off>"
  63.         exit 1
  64.         ;;
  65. esac
  66.  
  67. exit 0
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement