salt-states/_runners/cms_net.py
2019-11-11 14:22:13 -05:00

43 lines
1.4 KiB
Python

# -*- coding: utf-8 -*-
'''
Network tools to run from the Master, specific to the Claremont MakerSpace
'''
import logging
import socket
log = logging.getLogger(__name__)
def wolmatch(tgt, destport=9):
'''
Send a "Magic Packet" to wake up Minions that are matched in the
grains cache, automatically determining the destination broadcast address
CLI Example:
.. code-block:: bash
salt-run network.wolmatch minion_id
'''
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():
if iface == 'lo':
continue
mac = mac.strip()
#print(name, iface, mac, minion['ip_interfaces'])
if 'ip4_interfaces' in minion and iface in minion['ip_interfaces']:
ip_addrs = minion['ip4_interfaces'][iface]
for ip_addr in ip_addrs:
[subnet, _, _] = ip_addr.rpartition('.')
bcast = subnet + '.255'
__salt__['network.wol'](mac, bcast, destport)
log.info('Waking up %s', mac)
ret.append(f"{name}: {ip_addrs} {mac}")
return ret