2 # -*- coding: utf-8 -*-
4 # Connect and restart a Freebox
5 # Copyright (C) 2011 Vincent-Xavier JUMEL
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.
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.
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/>.
21 import mechanize,netrc
22 from BeautifulSoup import BeautifulSoup
25 from optparse import OptionParser
27 usage = u"Redémarrer sa freebox v6 en moulant depuis le terminal"
28 parser = OptionParser(usage=usage)
30 parser.add_option("-i","--interactive", dest="interactive",help=u"Description de la tâche", action="store_true",default=False)
32 (options, args) = parser.parse_args()
34 secrets = netrc.netrc()
35 password = secrets.authenticators('192.168.0.254')[2]
38 p = mechanize.Browser()
39 p.set_handle_robots(False)
40 p.open('http://192.168.0.254/')
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]
56 print("Redémarrage de la Freebox\n")
57 fbx.open("http://192.168.0.254/settings.php?page=misc_system")
63 posix.system("sleep 1")
65 sys.stdout.write('\n')
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':
76 while count_tentative < 5:
77 if count_tentative == 0:
79 elif count_tentative < 4:
81 if get_fbx_state(fbx) == 'down':