Pastebin launched a little side project called VERYVIRAL.com, check it out ;-) Want more features on Pastebin? Sign Up, it's FREE!
Guest

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

By: a guest on Jun 6th, 2010  |  syntax: Python  |  size: 1.97 KB  |  views: 247  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
This paste has a previous version, view the difference. Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  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.  
  12. def get_next_form(lines, page,mimic_button_field,action_line):
  13.     page="\n".join(lines)
  14.     soup2= BeautifulSoup.BeautifulSoup(page)
  15.     formulaire2 = soup2.find("form",{'name':"form_contents"})
  16.     dico={}
  17.     for input in formulaire2.findAll("input",{"name":re.compile("")}):
  18.         dico[input['name']] = input['value']
  19.     action_form=lines[action_line].split('"')[1]
  20.     dico["mimic_button_field"] = mimic_button_field
  21.     dico2 = urllib.urlencode(dict([k, v.encode('utf-8')] for k, v in dico.items()))
  22.     f = urllib2.urlopen(url + action_form, dico2)
  23.     return f.readlines()
  24.  
  25. #recup de pages  formulaire
  26. page_content = urllib.urlopen(url).read()
  27. soup=BeautifulSoup.BeautifulSoup(page_content)
  28. form= soup.find("form",{"name":"form_contents"})
  29.  
  30. #parsage formulaire, en dur a cause du passage oblige vers un hash md5
  31. dico={}
  32. for input in form.findAll("input",{"name":re.compile("")}):
  33.     dico[input['name']] = input['value']
  34. password_input_name=soup.find("input",{"name":re.compile("password_")})["name"] #paresseux...
  35. action_form=form["action"] # destination...
  36. dico["md5_pass"]=hashlib.md5(passe+dico["auth_key"]).hexdigest() # kung fu!
  37. dico[password_input_name]=passe
  38. dico["user_name"]=login
  39. encoded_form=urllib.urlencode(dico)
  40. f = urllib2.urlopen(url + action_form, encoded_form)
  41.  
  42. lines = f.readlines()
  43.  
  44.  
  45.  
  46.  
  47. lines = get_next_form(lines = lines,page="\n".join(lines),mimic_button_field="sidebar: lb_sidebar_basic_main..",action_line = 29 )
  48. lines = get_next_form(lines = lines,page="\n".join(lines),mimic_button_field="sidebar: lb_sidebar_basic_erase_reboot..",action_line = 29 )
  49. get_next_form(lines = lines,page="\n".join(lines),mimic_button_field="submit_button_reboot: ..",action_line = 23 )
  50. print "felicitation, vous etes deconnecte"