#!/usr/bin/env python2 # -*- coding: utf-8 -*- # # Copyright (C) 2009-2012 Gioacchino Mazzurco # # This program is free software: you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation, either version 3 of the License, or # (at your option) any later version. # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License # along with this file. If not, see . # import mechanize import time import os import datetime insd = 0 u = "ignoto" loginlist = open('/etc/serralogin/loginlist', 'r') log = open('/var/log/serralogin.log', 'a') log.write("Start\n") log.close() userpass = [] for line in loginlist: userpass.append(line.replace("\n", "").split(" ", 1)) while True: if(insd%5==0): agentHeader = [("User agent","(X11; U; Linux x86_64; en-US; rv:1.8.1.14) Gecko/20080418 Ubuntu/8.04 (hardy) Firefox/2.0.0.14")] elif(insd%5==1): agentHeader = [("User agent","Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1)")] elif(insd%5==2): agentHeader = [("User agent","Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; SV1)")] else: agentHeader = [("User agent","Mozilla/5.0 (compatible; Konqueror/3.5; Linux) KHTML/3.5.9 (like Gecko) (Kubuntu)")] connection = mechanize.Browser() connection.addheaders = agentHeader connection.set_handle_robots(False) while True: t1 = (datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")) t2 = (datetime.datetime.now().strftime("%H:%M:%S")) try: response = connection.open("http://lasek.altervista.org/test.html") if(response.read() == "connected\n"): log = open('/var/log/serralogin.log', 'a') a = " OK, i=" print t2,a,insd," u=",u,"\n" log.write(t2) log.write(a) dsni = str(insd) log.write(dsni) log.write(" u=") log.write(u) log.write("\n") log.close() break else: # insd += 1 log = open('/var/log/serralogin.log', 'a') a = " Connection Test Failed, Retrying to login using the same login: insd=" print t1,a,insd,"\n" log.write(t1) log.write(a) dsni = str(insd) log.write(dsni) log.write("\n") log.close() try: connection.open("https://auth1.unipi.it/auth/perfigo_weblogin.jsp") connection.select_form(nr=0) connection.form["username"] = userpass[insd%len(userpass)][0] connection.form["password"] = userpass[insd%len(userpass)][1] connection.submit() response = connection.open("http://lasek.altervista.org/test.html") if(response.read() == "connected\n"): log = open('/var/log/serralogin.log', 'a') u = str(userpass[insd%len(userpass)][0]) a = " Successfully Connected with login insd=" print t1,a,insd," utente=",u,"\n" log.write(t1) log.write(a) dsni = str(insd) log.write(dsni) log.write(" utente=") log.write(u) log.write("\n") log.close() break log = open('/var/log/serralogin.log', 'a') a = " Login Failed 1, trying next login. Current was insd=" print t1,a,insd,"\n" log.write(t1) log.write(a) dsni = str(insd) log.write(dsni) log.write("\n") log.close() insd += 1 break except: log = open('/var/log/serralogin.log', 'a') a = " Login Failed 2, retry in 20 seconds with next login. Current was insd=" print t1,a,insd,"\n" log.write(t1) log.write(a) dsni = str(insd) log.write(dsni) log.write("\n") log.close() time.sleep(15) insd += 1 except: log = open('/var/log/serralogin.log', 'a') a = " Serra Connection seems not working, retry in assai a caso seconds, isnd=" print t1,a,insd,"\n" log.write(t1) log.write(a) dsni = str(insd) log.write(dsni) log.write("\n") log.close() insd += 1 os.system('arping -c10 -q 131.114.250.101') os.system('arping -c10 -q 131.114.250.1') # time.sleep(5) # insd += 1 time.sleep(5)