Exemple de fichiers de conf
[scripts] / freebox / freebox.py
1 #!/usr/bin/python
2 # -*- coding: utf-8 -*-
3
4 #    Connect and restart a Freebox
5 #    Copyright (C) 2011 Vincent-Xavier JUMEL
6 #
7 #    This program is free software: you can redistribute it and/or modify
8 #    it under the terms of the GNU General Public License as published by
9 #    the Free Software Foundation, either version 3 of the License, or
10 #    (at your option) any later version.
11 #
12 #    This program is distributed in the hope that it will be useful,
13 #    but WITHOUT ANY WARRANTY; without even the implied warranty of
14 #    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 #    GNU General Public License for more details.
16 #
17 #    You should have received a copy of the GNU General Public License
18 #    along with this program.  If not, see <http://www.gnu.org/licenses/>.
19  
20
21 import mechanize,netrc
22 from BeautifulSoup import BeautifulSoup
23 import sys,posix
24
25 from optparse import OptionParser
26
27 usage = u"Redémarrer sa freebox v6 en moulant depuis le terminal"
28 parser = OptionParser(usage=usage)
29
30 parser.add_option("-i","--interactive", dest="interactive",help=u"Description de la tâche", action="store_true",default=False)
31
32 (options, args) = parser.parse_args()
33
34 secrets = netrc.netrc()
35 password = secrets.authenticators('192.168.0.254')[2]
36
37 def open_fbx():
38         p = mechanize.Browser()
39         p.set_handle_robots(False)
40         p.open('http://192.168.0.254/')
41         p.select_form(nr=0)
42         p["passwd"]=password
43         p.submit()
44         return p
45
46
47 fbx = open_fbx()
48
49 def get_fbx_state(fbx):
50         soup = BeautifulSoup(fbx.open("http://192.168.0.254/settings.php").read())
51         state = soup.find('span',attrs={'id':'conn_state'})
52         print state.contents[0]
53         return state['val']
54
55 def restart_fbx(fbx):
56         print("Redémarrage de la Freebox\n")
57         fbx.open("http://192.168.0.254/settings.php?page=misc_system")
58         fbx.select_form(nr=0)
59         fbx.submit()
60         i=0
61         while i<45:
62                 sys.stdout.write('.')
63                 posix.system("sleep 1")
64                 i+=1
65         sys.stdout.write('\n')
66
67 if get_fbx_state(fbx) == 'down':
68         if options.interactive == True:
69                 print(u'Redémarrer ?\n')
70                 if sys.stdin.readline() == 'n\n':
71                         exit(0)
72                 else:
73                         restart_fbx(fbx)
74         else:
75                 count_tentative = 0
76                 while count_tentative < 5:
77                         if count_tentative == 0:
78                                 restart_fbx(fbx)
79                         elif count_tentative < 4:
80                                 fbx = open_fbx()
81                                 if get_fbx_state(fbx) == 'down':
82                                         restart_fbx(fbx)
83                                 else:
84                                         exit(0)
85                         else:
86                                 exit(1)
87                         count_tentative+=1