Changeset 5089


Ignore:
Timestamp:
Jun 6, 2017, 12:23:41 PM (22 months ago)
Author:
hectorgh
Message:

updating wol executin

File:
1 edited

Legend:

Unmodified
Added
Removed
  • epoptes/trunk/fuentes/epoptes/core/wol.py

    r295 r5089  
    2424###########################################################################
    2525
    26 import array
    27 import fcntl
     26import sys
    2827import socket
    2928import struct
    30 import sys
    31 
    32 
    33 IFNAMSIZ = 16               # interface name size
    34 # From <bits/ioctls.h>
    35 SIOCGIFADDR = 0x8915        # get PA address
    36 SIOCGIFBRDADDR  = 0x8919    # get broadcast PA address
    37 SIOCGIFCONF = 0x8912        # get iface list
    38 
    39 
    40 # create a socket to communicate with system
    41 sockfd = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
    42 
    43 
    44 def _call(ifname, func, ip = None):
    45 
    46     if ip is None:
    47         data = (ifname + '\0'*32)[:32]
    48     else:
    49         ifreq = (ifname + '\0' * IFNAMSIZ)[:IFNAMSIZ]
    50         data = struct.pack("16si4s10x", ifreq, socket.AF_INET, socket.inet_aton(ip))
    51 
    52     try:
    53         result = fcntl.ioctl(sockfd.fileno(), func, data)
    54     except IOError:
    55         return None
    56 
    57     return result
    58 
    59 
    60 def getInterfaceList():
    61     """ Get all interface names in a list """
    62     # get interface list
    63     buffer = array.array('c', '\0' * 1024)
    64     ifconf = struct.pack("iP", buffer.buffer_info()[1], buffer.buffer_info()[0])
    65     result = fcntl.ioctl(sockfd.fileno(), SIOCGIFCONF, ifconf)
    66 
    67     # loop over interface names
    68     iflist = []
    69     size, ptr = struct.unpack("iP", result)
    70     for idx in range(0, size, 32):
    71         ifconf = buffer.tostring()[idx:idx+32]
    72         name, dummy = struct.unpack("16s16s", ifconf)
    73         name, dummy = name.split('\0', 1)
    74         iflist.append(name)
    75 
    76     return iflist
    77 
    78 
    79 def getBroadcast(ifname):
    80     """ Get the broadcast addr for an interface """
    81     result = _call(ifname, SIOCGIFBRDADDR)
    82     return socket.inet_ntoa(result[20:24])
    83 
     29import netifaces
    8430
    8531def getBroadcastList():
    8632    brlist = ['<broadcast>']
    87     for ifname in getInterfaceList():
    88         if ifname != 'lo':
    89             brlist.append(getBroadcast(ifname))
     33    ifaces = [iface for iface in netifaces.interfaces() if iface != 'lo']
     34    for ifname in ifaces:
     35        # An interface can have more than one address, even within the
     36        # same family (AF_INET), or none, so check this case too.
     37        addresses = netifaces.ifaddresses(ifname)
     38        if netifaces.AF_INET not in addresses:
     39            continue
     40        for addr in addresses[netifaces.AF_INET]:
     41            if 'broadcast' in addr:
     42                brlist.append(addr['broadcast'])
    9043    return brlist
    9144
     
    11467
    11568    # Broadcast it to the LAN.
    116     sockfd.setsockopt(socket.SOL_SOCKET, socket.SO_BROADCAST, 1)
     69    sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
     70    sock.setsockopt(socket.SOL_SOCKET, socket.SO_BROADCAST, 1)
    11771    for br in getBroadcastList():
    118         sockfd.sendto(send_data, (br, 9))
     72        sock.sendto(send_data, (br, 9))
    11973
    12074if __name__ == '__main__':
Note: See TracChangeset for help on using the changeset viewer.