Debian Codegraph @

ansible_collections/community/general/plugins/modules/wakeonlan.py

raw
1 #!/usr/bin/python
2
3 # Copyright (c) 2016, Dag Wieers <[email protected]>
4 # GNU General Public License v3.0+ (see LICENSES/GPL-3.0-or-later.txt or https://www.gnu.org/licenses/gpl-3.0.txt)
5 # SPDX-License-Identifier: GPL-3.0-or-later
6
7 from __future__ import annotations
8
9 DOCUMENTATION = r"""
10 module: wakeonlan
11 short_description: Send a magic Wake-on-LAN (WoL) broadcast packet
12 description:
13 - The C(wakeonlan) module sends magic Wake-on-LAN (WoL) broadcast packets.
14 extends_documentation_fragment:
15 - community.general._attributes
16 attributes:
17 check_mode:
18 support: full
19 diff_mode:
20 support: none
21 options:
22 mac:
23 description:
24 - MAC address to send Wake-on-LAN broadcast packet for.
25 required: true
26 type: str
27 broadcast:
28 description:
29 - Network broadcast address to use for broadcasting magic Wake-on-LAN packet.
30 default: 255.255.255.255
31 type: str
32 port:
33 description:
34 - UDP port to use for magic Wake-on-LAN packet.
35 default: 7
36 type: int
37 todo:
38 - Add arping support to check whether the system is up (before and after)
39 - Enable check-mode support (when we have arping support)
40 - Does not have SecureOn password support
41 notes:
42 - This module sends a magic packet, without knowing whether it worked.
43 - Only works if the target system was properly configured for Wake-on-LAN (in the BIOS and/or the OS).
44 - Some BIOSes have a different (configurable) Wake-on-LAN boot order (in other words, PXE first).
45 seealso:
46 - module: community.windows.win_wakeonlan
47 author:
48 - Dag Wieers (@dagwieers)
49 """
50
51 EXAMPLES = r"""
52 - name: Send a magic Wake-on-LAN packet to 00:00:5E:00:53:66
53 community.general.wakeonlan:
54 mac: '00:00:5E:00:53:66'
55 broadcast: 192.0.2.23
56 delegate_to: localhost
57
58 - community.general.wakeonlan:
59 mac: 00:00:5E:00:53:66
60 port: 9
61 delegate_to: localhost
62 """
63
64 RETURN = r"""
65 # Default return values
66 """
67 import socket
68 import struct
69 import traceback
70
71 from ansible.module_utils.basic import AnsibleModule
72
73
74 def wakeonlan(module, mac, broadcast, port):
75 """Send a magic Wake-on-LAN packet."""
76
77 mac_orig = mac
78
79 # Remove possible separator from MAC address
80 if len(mac) == 12 + 5:
81 mac = mac.replace(mac[2], "")
82
83 # If we don't end up with 12 hexadecimal characters, fail
84 if len(mac) != 12:
85 module.fail_json(msg=f"Incorrect MAC address length: {mac_orig}")
86
87 # Test if it converts to an integer, otherwise fail
88 try:
89 int(mac, 16)
90 except ValueError:
91 module.fail_json(msg=f"Incorrect MAC address format: {mac_orig}")
92
93 # Create payload for magic packet
94 data = b""
95 padding = f"FFFFFFFFFFFF{mac * 20}"
96 for i in range(0, len(padding), 2):
97 data = b"".join([data, struct.pack("B", int(padding[i : i + 2], 16))])
98
99 # Broadcast payload to network
100 sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
101 sock.setsockopt(socket.SOL_SOCKET, socket.SO_BROADCAST, 1)
102
103 if not module.check_mode:
104 try:
105 sock.sendto(data, (broadcast, port))
106 except OSError as e:
107 sock.close()
108 module.fail_json(msg=f"{e}", exception=traceback.format_exc())
109
110 sock.close()
111
112
113 def main():
114 module = AnsibleModule(
115 argument_spec=dict(
116 mac=dict(type="str", required=True),
117 broadcast=dict(type="str", default="255.255.255.255"),
118 port=dict(type="int", default=7),
119 ),
120 supports_check_mode=True,
121 )
122
123 mac = module.params["mac"]
124 broadcast = module.params["broadcast"]
125 port = module.params["port"]
126
127 wakeonlan(module, mac, broadcast, port)
128
129 module.exit_json(changed=True)
130
131
132 if __name__ == "__main__":
133 main()