2019-11-11 14:22:13 -05:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
'''
|
|
|
|
Network tools to run from the Master, specific to the Claremont MakerSpace
|
|
|
|
'''
|
|
|
|
|
|
|
|
import logging
|
|
|
|
import socket
|
2022-05-09 16:37:00 -04:00
|
|
|
import netifaces
|
2019-11-11 14:22:13 -05:00
|
|
|
|
2022-05-09 16:37:00 -04:00
|
|
|
import salt.utils.network
|
2019-11-11 14:22:13 -05:00
|
|
|
|
2022-05-09 16:37:00 -04:00
|
|
|
log = logging.getLogger(__name__)
|
2019-11-11 14:22:13 -05:00
|
|
|
|
2022-05-09 16:37:00 -04:00
|
|
|
def wol(mac, bcast="255.255.255.255", destport=9):
|
|
|
|
"Like network.wol, but sends on each interface"
|
|
|
|
dest = salt.utils.network.mac_str_to_bytes(mac)
|
|
|
|
for iface in netifaces.interfaces():
|
|
|
|
if iface == 'lo':
|
|
|
|
continue
|
|
|
|
with socket.socket(socket.AF_INET, socket.SOCK_DGRAM) as sock:
|
|
|
|
sock.setsockopt(socket.SOL_SOCKET, socket.SO_BROADCAST, 1)
|
|
|
|
sock.setsockopt(socket.SOL_SOCKET, socket.SO_BINDTODEVICE, iface.encode('ascii'))
|
|
|
|
sock.sendto(b"\xff" * 6 + dest * 16, (bcast, int(destport)))
|
|
|
|
return True
|
2019-11-11 14:22:13 -05:00
|
|
|
|
2022-05-09 16:37:00 -04:00
|
|
|
def wolmatch(tgt, destport=9):
|
|
|
|
'Like network.wolmatch'
|
2019-11-11 14:22:13 -05:00
|
|
|
ret = []
|
|
|
|
minions = __salt__['cache.grains'](tgt, 'compound')
|
|
|
|
for name, minion in minions.items():
|
|
|
|
if 'hwaddr_interfaces' not in minion:
|
|
|
|
continue
|
|
|
|
# NOTE: FIVE, NINE, and TEN all should have eth addr as well, why is there only one printed?
|
|
|
|
for iface, mac in minion['hwaddr_interfaces'].items():
|
|
|
|
mac = mac.strip()
|
2022-10-05 14:22:37 -04:00
|
|
|
if iface == 'lo' or len(mac) != len("00:00:00:00:00:00"):
|
|
|
|
continue
|
2019-11-11 14:22:13 -05:00
|
|
|
#print(name, iface, mac, minion['ip_interfaces'])
|
2022-05-09 16:37:00 -04:00
|
|
|
wol(mac, destport=destport)
|
|
|
|
log.info('Waking up %s', mac)
|
|
|
|
ret.append(f"{name}: {mac}")
|
2019-11-11 14:22:13 -05:00
|
|
|
return ret
|