summaryrefslogtreecommitdiff
path: root/tcosmonitor/extensions/wakeonlan.py
blob: 0f827f8b84f5495de754c6364667291555509db2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# -*- coding: UTF-8 -*-
#    TcosMonitor version __VERSION__
#
# Copyright (c) 2006-2011 Mario Izquierdo <mariodebian@gmail.com>
#
# This package 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 2 of the License, or
# (at your option) any later version.
#
#
# This package 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 program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
""" template extension """

from gettext import gettext as _
import sys

from tcosmonitor import shared
from tcosmonitor.TcosExtensions import TcosExtension

from tcosmonitor.WakeOnLan import WakeOnLan

def print_debug(txt):
    if shared.debug:
        print >> sys.stderr, "%s::%s" % (__name__, txt)
        #print("%s::%s" % (__name__, txt), file=sys.stderr)


class WOL(TcosExtension):
    def register(self):
        self.main.menus.register_simple( _("Boot client (WakeOnLan)") , "menu_wol.png", 0, self.wol_one, "wakeonlan")
        self.main.menus.register_all( _("Boot All clients (WakeOnLan)") , "menu_wol.png", 0, self.wol_all, "wakeonlan")
        

    def wol_one(self, widget, ip):
        if not self.get_client():
            print_debug("wol_one() no client")
            return
        if self.main.config.GetVar("scan_network_method") != "static":
            msg=(_("Wake On Lan only works with static list.\n\nEnable scan method \"static\" in Preferences\nand (wake on lan) support in bios of clients." ))
            shared.info_msg ( msg )
            return
                    
        msg=_( _("Do you want boot %s client?" %ip))
        if shared.ask_msg ( msg ):
            data=[]
            hostslist=self.main.config.GetVar("statichosts")
            #eth=self.main.config.GetVar("network_interface")
            if hostslist == "":
                return
            data=hostslist.split("#")
            data=data[:-1]
            for host in data:
                mip, mac=host.split("|")
                print_debug("wol_one() ip=%s mac=%s" %(mip, mac) )
                if mip == self.main.selected_ip:
                    if mac == "":
                        self.main.write_into_statusbar(_("No register MAC address for ip: \"%s\"")%ip)
                        continue
                    print_debug("Send magic packet to mac=%s" %mac)
                    if not WakeOnLan("%s"%mac):
                        self.main.write_into_statusbar(_("Not valid MAC address: \"%s\"")%mac)

    def wol_all(self, *args):
        if self.main.config.GetVar("scan_network_method") != "static":
            msg=(_("Wake On Lan only works with static list.\n\nEnable scan method \"static\" in Preferences\nand (wake on lan) support in bios of clients." ))
            shared.info_msg ( msg )
            return
                    
        msg=_( _("Do you want boot all clients?" ))
        if shared.ask_msg ( msg ):
            data=[]
            hostslist=self.main.config.GetVar("statichosts")
            #eth=self.main.config.GetVar("network_interface")
            if hostslist == "":
                return
            data=hostslist.split("#")
            data=data[:-1]
            errors=[]
            for host in data:
                mac=host.split("|")[1]
                if mac == "":
                    self.main.write_into_statusbar(_("No register MAC address for ip: \"%s\"")%host)
                    continue
                print_debug("Send magic packet to mac=%s" %mac)
                if not WakeOnLan("%s"%mac):
                    errors.append(mac)
            if len(errors) >1:
                print_debug("menu_event_all() errors=%s"%errors)
                self.main.write_into_statusbar(_("Not valid MAC address: \"%s\"")%" ".join(errors))


__extclass__=WOL