Created
February 22, 2019 04:01
-
-
Save samatjain/07e6325cb03abc929f1a568ed9a98a60 to your computer and use it in GitHub Desktop.
wakeonlan wrapper script w/ pre-programmed hostnames and MAC addresses
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
# -*- coding: utf-8 -*- | |
""" | |
Send a magic packet to a list pre-programmed hostnames. | |
wakeonlan.sh is the old shell-based version; please use this program instead. | |
Requires the "wakeonlan" program is installed, i.e. | |
sudo apt-get install -y wakeonlan | |
requirements.txt: | |
sh | |
termcolor | |
""" | |
import ipaddress | |
import sys | |
import typing | |
from pprint import pprint # NOQA | |
from termcolor import colored | |
from sh import wakeonlan | |
class Host(typing.NamedTuple): | |
""" | |
Groups information about hosts. | |
Attributes: | |
hostname: Non-FQDN hostname | |
mac: MAC adddress, formatted 00:00:00:00:00:00 | |
ipv4: IPv4 address | |
ipv6: IPv6 address | |
""" | |
hostname: str | |
mac: str = "00:00:00:00:00:00" | |
ipv4: ipaddress.IPv4Address = ipaddress.IPv4Address('127.0.0.1') | |
ipv6: ipaddress.IPv6Address = ipaddress.IPv6Address('::1') | |
HOSTS_LIST = [ | |
Host(hostname="apis", | |
mac="ac:22:0b:cd:e1:69", | |
ipv4=ipaddress.IPv4Address('192.168.1.156'), | |
), | |
Host(hostname="tiny", | |
mac="6c:3b:e5:0d:8d:81", | |
ipv4=ipaddress.IPv4Address('192.168.1.187'), | |
), | |
Host(hostname="nu", | |
mac="70:85:c2:5a:14:19", | |
ipv4=ipaddress.IPv4Address('192.168.1.240'), | |
), | |
] | |
HOSTS_DICT = {h.hostname: h for h in HOSTS_LIST} | |
def main(hostname: str) -> None: | |
"""Wrapper around wakeonlan.""" | |
if hostname not in HOSTS_DICT: | |
print(colored("error", "red") + ": ", end='', flush=True, file=sys.stderr) | |
print("Unrecognized hostname.", file=sys.stderr) | |
sys.exit(1) | |
mac = HOSTS_DICT[hostname] | |
wakeonlan(mac) | |
print("Magic packet sent to", colored(hostname, 'yellow') + ".") | |
if __name__ == '__main__': | |
if len(sys.argv) < 2: | |
print(colored("usage", "yellow") + ": ", end='', flush=True, file=sys.stderr) | |
print("{program_name} [HOSTNAME_TO_WAKE]".format(program_name=sys.argv[0]), file=sys.stderr) | |
sys.exit(1) | |
main(sys.argv[1]) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment