BASH   101

iitgnetauth

Guest on 19th August 2022 12:35:46 AM

  1. # IITG Fortinet Firewall Authentication Script For linux (Version: 4)
  2. # ==================================================================
  3. # The authentication  script  has  been  redesigned  based  on  the
  4. # IITG  Fortinet  Firewall  specification and it's  login workflow,
  5. # to meet  the  requirement. This  script  allows  users  to  login
  6. # to  the  IITG  Fortinet Firewall from the  command line  and  get
  7. # connected (via keep alive mode) until  explicitly  stop/exit (Ctrl+C)
  8. # from the script. In this version of the script, user has to explicitly
  9. # specify his/her username and password(used for internet accesing)
  10. # in the standard input.
  11.  
  12. #  If you have some system proxy defined,then please disable that one.
  13.  
  14. #  * Dependency is the "bash" shell and "curl" which most of the linux
  15. #    system has by default.
  16.  
  17. #  * Give execute permission to the script
  18. #    chmod 755 iitgnetauth.sh
  19. #    You can also give only root to have read/write/execute permission
  20.  
  21. #  * Make sure that your /tmp/temp.html file writable by the user
  22.  
  23. #  * Run the executable
  24. #    ./iitgnetauth.sh
  25.  
  26. #  * While running, the script will prompted for username and password
  27. #    enter username: (username used for internet access)
  28. #    enter password: (password used for internet access)
  29.  
  30. #  * Auto login feature available in this script to keep
  31. #    the login session active
  32.  
  33. #  (Developed and Re-designed by: Sanjoy Das,CCC, IITG)
  34.  
  35. #  Permission is hereby granted, free of charge, to any person obtaining a copy
  36. #  of this software and associated documentation files (the "Software"), to deal
  37. #  in the Software without restriction, including without limitation the rights
  38. #  to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  39. #  copies of the Software, and to permit persons to whom the Software is
  40. #  furnished to do so, subject to the following conditions:
  41.  
  42. #  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  43. #  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  44. #  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  45. #  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  46. #  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  47. #  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  48. #  SOFTWARE.
  49.  
  50.  
  51.  
  52. #!/bin/bash
  53.  
  54. url="https://agnigarh.iitg.ac.in:1442/logout?"
  55. req_url="${url:0:33}";
  56.  
  57. rm /tmp/temp.html  > /dev/null 2>&1;
  58.  
  59. #trap '(echo "Exiting....." && echo "Logged Out" && curl  -k -o /tmp/temp.html "$url"  > /dev/null 2>&1 ); exit 0;' SIGINT
  60. trap '(echo "Exiting....." && echo "Logged Out" && ps -ef | grep "iitgnetauth" | grep -v grep | awk "{print $2}" | xargs -r kill -9  && curl  -k -o /tmp/temp.html "$url"  > /dev/null 2>&1 ); exit 0;' SIGINT
  61.  
  62. while true; do 
  63.         if [ -z  $logged ]; then
  64.                 #user=""; # Specify username here
  65.                 #pass=""; # Specify password here
  66.                 echo -n "Enter Username:";
  67.                 read  user;
  68.                 echo -n "Enter Password:";
  69.                 read -s pass;
  70.                 echo "";
  71.         fi     
  72.  
  73.         # Checking login parameter validation
  74.         if [ -z  $user ]; then
  75.                 echo "Please specify username !!";
  76.                 exit 1;
  77.         fi
  78.         if [ -z  $pass ]; then
  79.                 echo "Please specify password !!";
  80.                 exit 1;
  81.         fi
  82.  
  83.         re_url=$(curl -Lsk -o /dev/null -w %{url_effective} $url);
  84.         #echo "redirect url: $re_url";
  85.  
  86.         until $(curl  -k -o /tmp/temp.html "$re_url"  > /dev/null 2>&1); do
  87.                 echo "Connecting.....";
  88.                 sleep 5;
  89.         done
  90.         echo "Connected.....";
  91.  
  92.         magic=$(cat /tmp/temp.html | grep -o "magic.*>" | grep -o "=.*>" |tr -d '\">=');
  93.         #echo "Magic Value: $magic";           
  94.  
  95.         tredir=$(cat /tmp/temp.html | grep -o "4Tredir.*>" | grep -o "=.*>" |tr -d '\">=');
  96.         #echo "4Tredir Value: $tredir";        
  97.  
  98.         until $(curl -k -L -o /tmp/temp.html -d "4Tredir=$tredir" -d "username=$user" -d 'submit=Continue' -d "password=$pass" -d "magic=$magic" "$req_url"  > /dev/null 2>&1); do
  99.                 echo "Logging In.....";
  100.         done
  101.  
  102.         ka_url=$(cat /tmp/temp.html | grep -o "location.href=.*;" | grep -o "\"[^\"]*\"" | head -n1  | tr -d '"' );    
  103.         #echo "KeepAlive URL Value: $ka_url";
  104.  
  105.  
  106.         if [ ! -z  $ka_url ]; then
  107.                 echo "Logged In";
  108.                 logged="1";
  109.         else
  110.                 echo "Login Failed";
  111.                 continue;
  112.         fi
  113.  
  114.         while true; do
  115.                 sleep 110; # after every 110 second, active the keep alive session
  116.                 if $(curl -k -o /tmp/temp.html $ka_url  > /dev/null 2>&1); then
  117.                         echo "Keeping.....Alive";
  118.                         continue;
  119.                 fi
  120.                 break;
  121.         done
  122.  
  123. done

Raw Paste


Login or Register to edit or fork this paste. It's free.