Guest User

Redemarrer un routeur FAST3304-V2 depuis un script python. Necessite BeautifulSoup.

a guest
Jun 6th, 2010
359
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. # -*- coding: latin-1 -*-
  2.  
  3. #imports
  4. import hashlib,urllib,urllib2, BeautifulSoup,re
  5.  
  6. #constantes
  7. login="Menara" #changez le login...
  8. passe="Menara" #le mot de passe...
  9. url = "http://192.168.1.1" #et l'url du routeur...
  10.  
  11. def unicode_urlencode(params):
  12.     if isinstance(params, dict):
  13.         params = params.items()
  14.         return urllib.urlencode([(k, isinstance(v, unicode) and v.encode('utf-8') or v)])
  15.  
  16. def get_next_form(lines, page,mimic_button_field,action_line):
  17.     page="\n".join(lines)
  18.     soup2= BeautifulSoup.BeautifulSoup(page)
  19.     formulaire2 = soup2.find("form",{'name':"form_contents"})
  20.     dico={}
  21.     for input in formulaire2.findAll("input",{"name":re.compile("")}):
  22.         dico[input['name']] = input['value']
  23.     action_form=lines[action_line].split('"')[1]
  24.     dico["mimic_button_field"] = mimic_button_field
  25.     dico2 = urllib.urlencode(dict([k, v.encode('utf-8')] for k, v in dico.items()))
  26.     f = urllib2.urlopen(url + action_form, dico2)
  27.     return f.readlines()
  28.  
  29. #recup de pages  formulaire
  30. page_content = urllib.urlopen(url).read()
  31. soup=BeautifulSoup.BeautifulSoup(page_content)
  32. form= soup.find("form",{"name":"form_contents"})
  33.  
  34. #parsage formulaire, en dur a cause du passage oblige vers un hash md5
  35. dico={}
  36. for input in form.findAll("input",{"name":re.compile("")}):
  37.     dico[input['name']] = input['value']
  38. password_input_name=soup.find("input",{"name":re.compile("password_")})["name"] #paresseux...
  39. action_form=form["action"] # destination...
  40. dico["md5_pass"]=hashlib.md5(passe+dico["auth_key"]).hexdigest() # kung fu!
  41. dico[password_input_name]=passe
  42. dico["user_name"]=login
  43. encoded_form=urllib.urlencode(dico)
  44. f = urllib2.urlopen(url + action_form, encoded_form)
  45.  
  46. lines = f.readlines()
  47.  
  48.  
  49.  
  50.  
  51. lines = get_next_form(lines = lines,page="\n".join(lines),mimic_button_field="sidebar: lb_sidebar_basic_main..",action_line = 29 )
  52. lines = get_next_form(lines = lines,page="\n".join(lines),mimic_button_field="sidebar: lb_sidebar_basic_erase_reboot..",action_line = 29 )
  53. get_next_form(lines = lines,page="\n".join(lines),mimic_button_field="submit_button_reboot: ..",action_line = 23 )
  54. print "felicitation, vous etes deconnecte"
RAW Paste Data

Adblocker detected! Please consider disabling it...

We've detected AdBlock Plus or some other adblocking software preventing Pastebin.com from fully loading.

We don't have any obnoxious sound, or popup ads, we actively block these annoying types of ads!

Please add Pastebin.com to your ad blocker whitelist or disable your adblocking software.

×