Skip to content

Instantly share code, notes, and snippets.

@kshivang
Last active April 14, 2017 19:24
Show Gist options
  • Save kshivang/01f0d1d2f7ef4fc3eafe2e8e3066be2c to your computer and use it in GitHub Desktop.
Save kshivang/01f0d1d2f7ef4fc3eafe2e8e3066be2c to your computer and use it in GitHub Desktop.
This is python script can be used to login to iitk fortigate ip configured server.
#!/usr/bin/env python
import requests
import time
import re
import sys
import getpass
from bs4 import BeautifulSoup
def liveSession( html ):
a = 0
while True:
a = a + 1
for i in range(50):
time.sleep(1)
sys.stdout.write("\r%d times refreshed! Next Refresh in %2d seconds" % (a, 50-i))
sys.stdout.flush()
soup = BeautifulSoup(html, "html.parser")
script = soup.script.string
arrayString = re.findall('"([^"]*)"', script)
url = arrayString[0]
html = requests.get(url).text
return
def login():
soup = BeautifulSoup(requests.get('http://google.com').text , "html.parser")
Magic = soup.find("input", {'name': "magic"}).attrs['value']
Tredir = soup.find("input", {'name': "4Tredir"}).attrs['value']
UserName = raw_input('IITK user name:')
Password = getpass.getpass('Password:')
payload = {'4Tredir': Tredir, 'magic': Magic, 'username': UserName, 'password': Password }
f = requests.post('https://gateway.iitk.ac.in:1003', data = payload);
soup = BeautifulSoup(f.text , "html.parser")
if soup.h2.string is not None:
str = soup.h2.string
print str
substr = 'again'
if substr in str:
return True
print "Fortinet Loggined!"
liveSession(f.text)
return False
requestLogout = requests.get('https://gateway.iitk.ac.in:1003/logout?0501030a00253727')
time.sleep(1)
if re.match('(200)|(303)', str(requestLogout.status_code)) is not None :
while login():
print "Try Again!"
else:
print "Internet connection Problem!"
@kshivang
Copy link
Author

kshivang commented Jul 8, 2016

You may have to install beautiful soup python library to run this. You can install it by

pip install beautifulsoup4

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment