#!/usr/bin/env python ###################################################################################### # check_wol - check to see if your target is alive, by sending a continuous ping. # if the target is down the pluging send the magic packet # to turn on the machine. # # Example: check_wol -t 192.168.16.203 -b 192.168.31.255 -a ff:ff:ff:ff:ff:ff -m LAN # # # License Information: # This program 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 program 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., 675 Mass Ave, Cambridge, MA 02139, USA. # # $Id: check_wol 2009-07-01 14:43:00 MEZGANI Ali $ # ###################################################################################### import struct, socket, getopt import os, sys, time, re from threading import Thread class WakeOn: def __init__(self, ip, mac, broadcast, mode, version): self.ip=ip self.port=9 self.mac=mac self.broadcast=broadcast if not mode: mode="LAN" self.mode=mode if not version: version=4 self.version=version def WakeOnLan(self): # Construct a six-byte hardware address addr_byte = self.mac.split(':') hw_addr = struct.pack('BBBBBB', int(addr_byte[0], 16), int(addr_byte[1], 16), int(addr_byte[2], 16), int(addr_byte[3], 16), int(addr_byte[4], 16), int(addr_byte[5], 16)) # Build the Wake-On-LAN "Magic Packet"... msg = '\xff' * 6 + hw_addr * 16 # ...and send it to the broadcast address using UDP if self.version==4: s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) else: s = socket.socket(socket.AF_INET6, socket.SOCK_DGRAM) s.setsockopt(socket.SOL_SOCKET, socket.SO_BROADCAST, 1) if self.mode=="LAN": self.broadcast='' s.sendto(msg, (self.broadcast, self.port)) s.close() def ping(self): try: if self.version==4: req=os.popen("ping -c2 -q "+self.ip, "r") elif self.version==6: req=os.popen("ping6 -c2 -q "+self.ip, "r") while 1: reponse=req.readline() if not reponse: break stat = re.findall(re.compile("(\d) received"), reponse) if stat: break return stat except Exception, e: print ("Check WOL critical: %s" % e) sys.exit(2) def usage(): print """Usage: check_wol [-h|--help] [-t|--target] [-b|--broadcast] \ [-a|--mac] [-m|--mode]""" sys.exit(3) if __name__=="__main__": if len(sys.argv) != 9: usage() try: options, args = getopt.getopt(sys.argv[1:], "h:t:b:a:m:", "--help --target= --broadcast= --mac= --mode=", ) except getopt.GetoptError: usage() sys.exit(3) for name, value in options: if name in ("-h", "--help"): usage() if name in ("-t", "--target"): target=value if name in ("-b", "--broadcast"): broadcast=value if name in ("-a", "--mac"): mac=value if name in ("-m", "--mode"): mode=value s=WakeOn(target, mac, broadcast, mode, 4) tab=("No response", "Partial Response", "Alive") stat=s.ping() i=int(stat[0]) if i==2 or i==1: print ("Check WOL ok: Status %s" % tab[i]) sys.exit(0) else: s.WakeOnLan()