| 1 |
#!/usr/bin/python |
| 2 |
# |
| 3 |
# Copyright (c) 2025, (@miyuk) <[email protected]> |
| 4 |
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt) |
| 5 |
# SPDX-License-Identifier: GPL-3.0-or-later |
| 6 |
|
| 7 |
|
| 8 |
DOCUMENTATION = r""" |
| 9 |
--- |
| 10 |
module: proxmox_sendkey |
| 11 |
short_description: Send key presses to a Proxmox VM console |
| 12 |
version_added: 1.5.0 |
| 13 |
description: |
| 14 |
- Uses the Proxmox API to send a sequence of key presses to the console of a VM. |
| 15 |
- Keys can be specified explicitly or derived from a plain text string. |
| 16 |
author: |
| 17 |
- "miyuk (@miyuk172) <[email protected]>" |
| 18 |
attributes: |
| 19 |
check_mode: |
| 20 |
support: none |
| 21 |
diff_mode: |
| 22 |
support: none |
| 23 |
notes: |
| 24 |
- Keys must match the qemu key names listed in |
| 25 |
L(QEMU ui.QKeyCode,https://www.qemu.org/docs/master/interop/qemu-qmp-ref.html#enum-QMP-ui.QKeyCode). |
| 26 |
options: |
| 27 |
name: |
| 28 |
description: |
| 29 |
- The unique name of the VM. |
| 30 |
type: str |
| 31 |
vmid: |
| 32 |
description: |
| 33 |
- The unique ID of the VM. |
| 34 |
- Takes precedence over O(name). |
| 35 |
type: int |
| 36 |
keys_send: |
| 37 |
description: |
| 38 |
- List of keys or key sequence to send in order. |
| 39 |
- Each item must follow the qemu key naming format such as |
| 40 |
C(ctrl-alt-delete) or C(ret). |
| 41 |
- You can specify either O(keys_send) or O(string_send). |
| 42 |
type: list |
| 43 |
elements: str |
| 44 |
string_send: |
| 45 |
description: |
| 46 |
- Raw string that will be transformed to the corresponding key presses |
| 47 |
- Only ASCII-characters are supported. |
| 48 |
before sending. |
| 49 |
- You can specify either O(keys_send) or O(string_send). |
| 50 |
type: str |
| 51 |
delay: |
| 52 |
description: |
| 53 |
- Delay in seconds between each key press. |
| 54 |
type: float |
| 55 |
default: 0.0 |
| 56 |
extends_documentation_fragment: |
| 57 |
- community.proxmox.proxmox.actiongroup_proxmox |
| 58 |
- community.proxmox.proxmox.documentation |
| 59 |
- community.proxmox.attributes |
| 60 |
""" |
| 61 |
|
| 62 |
EXAMPLES = r""" |
| 63 |
- name: Send Ctrl+Alt+Delete to a Windows VM |
| 64 |
proxmox_sendkey: |
| 65 |
name: win-test |
| 66 |
keys_send: |
| 67 |
- ctrl-alt-delete |
| 68 |
|
| 69 |
- name: Type a login string into a Linux VM console |
| 70 |
proxmox_sendkey: |
| 71 |
vmid: 101 |
| 72 |
string_send: | |
| 73 |
root |
| 74 |
P@ssw0rd |
| 75 |
delay: 1.0 |
| 76 |
""" |
| 77 |
|
| 78 |
RETURN = r""" |
| 79 |
vmid: |
| 80 |
description: The VM vmid. |
| 81 |
returned: success |
| 82 |
type: int |
| 83 |
sample: 101 |
| 84 |
total_keys: |
| 85 |
description: List of sent keys that were sent to the VM console. |
| 86 |
returned: success |
| 87 |
type: list |
| 88 |
elements: str |
| 89 |
sample: ["H", "e", "l", "l", "o"] |
| 90 |
keys_num: |
| 91 |
description: Number of sent keys that were sent to the VM console. |
| 92 |
returned: success |
| 93 |
type: int |
| 94 |
sample: 5 |
| 95 |
completed_keys_num: |
| 96 |
returned: success |
| 97 |
description: Number of keys that were sent to the VM console. |
| 98 |
type: int |
| 99 |
sample: 5 |
| 100 |
""" |
| 101 |
|
| 102 |
import time |
| 103 |
|
| 104 |
from ansible_collections.community.proxmox.plugins.module_utils.proxmox import ( |
| 105 |
ProxmoxAnsible, |
| 106 |
create_proxmox_module, |
| 107 |
) |
| 108 |
|
| 109 |
|
| 110 |
def module_args(): |
| 111 |
return dict( |
| 112 |
vmid=dict(type="int"), |
| 113 |
name=dict(type="str"), |
| 114 |
keys_send=dict(type="list", elements="str", no_log=False), |
| 115 |
string_send=dict(type="str"), |
| 116 |
delay=dict(type="float", default=0.0), |
| 117 |
) |
| 118 |
|
| 119 |
|
| 120 |
def module_options(): |
| 121 |
return dict( |
| 122 |
required_one_of=[ |
| 123 |
("keys_send", "string_send"), |
| 124 |
("vmid", "name"), |
| 125 |
], |
| 126 |
mutually_exclusive=[("keys_send", "string_send")], |
| 127 |
supports_check_mode=False, |
| 128 |
) |
| 129 |
|
| 130 |
|
| 131 |
class ProxmoxSendkeyAnsible(ProxmoxAnsible): |
| 132 |
"""Proxmox sendkey module implementation.""" |
| 133 |
|
| 134 |
ALL_KEYS = [ |
| 135 |
"unmapped", |
| 136 |
"pause", |
| 137 |
"ro", |
| 138 |
"kp_comma", |
| 139 |
"kp_equals", |
| 140 |
"power", |
| 141 |
"hiragana", |
| 142 |
"henkan", |
| 143 |
"yen", |
| 144 |
"sleep", |
| 145 |
"wake", |
| 146 |
"audionext", |
| 147 |
"audioprev", |
| 148 |
"audiostop", |
| 149 |
"audioplay", |
| 150 |
"audiomute", |
| 151 |
"volumeup", |
| 152 |
"volumedown", |
| 153 |
"mediaselect", |
| 154 |
"mail", |
| 155 |
"calculator", |
| 156 |
"computer", |
| 157 |
"ac_home", |
| 158 |
"ac_back", |
| 159 |
"ac_forward", |
| 160 |
"ac_refresh", |
| 161 |
"ac_bookmarks", |
| 162 |
"muhenkan", |
| 163 |
"katakanahiragana", |
| 164 |
"lang1", |
| 165 |
"lang2", |
| 166 |
"f13", |
| 167 |
"f14", |
| 168 |
"f15", |
| 169 |
"f16", |
| 170 |
"f17", |
| 171 |
"f18", |
| 172 |
"f19", |
| 173 |
"f20", |
| 174 |
"f21", |
| 175 |
"f22", |
| 176 |
"f23", |
| 177 |
"f24", |
| 178 |
"shift", |
| 179 |
"shift_r", |
| 180 |
"alt", |
| 181 |
"alt_r", |
| 182 |
"ctrl", |
| 183 |
"ctrl_r", |
| 184 |
"menu", |
| 185 |
"esc", |
| 186 |
"1", |
| 187 |
"2", |
| 188 |
"3", |
| 189 |
"4", |
| 190 |
"5", |
| 191 |
"6", |
| 192 |
"7", |
| 193 |
"8", |
| 194 |
"9", |
| 195 |
"0", |
| 196 |
"minus", |
| 197 |
"equal", |
| 198 |
"backspace", |
| 199 |
"tab", |
| 200 |
"q", |
| 201 |
"w", |
| 202 |
"e", |
| 203 |
"r", |
| 204 |
"t", |
| 205 |
"y", |
| 206 |
"u", |
| 207 |
"i", |
| 208 |
"o", |
| 209 |
"p", |
| 210 |
"bracket_left", |
| 211 |
"bracket_right", |
| 212 |
"ret", |
| 213 |
"a", |
| 214 |
"s", |
| 215 |
"d", |
| 216 |
"f", |
| 217 |
"g", |
| 218 |
"h", |
| 219 |
"j", |
| 220 |
"k", |
| 221 |
"l", |
| 222 |
"semicolon", |
| 223 |
"apostrophe", |
| 224 |
"grave_accent", |
| 225 |
"backslash", |
| 226 |
"z", |
| 227 |
"x", |
| 228 |
"c", |
| 229 |
"v", |
| 230 |
"b", |
| 231 |
"n", |
| 232 |
"m", |
| 233 |
"comma", |
| 234 |
"dot", |
| 235 |
"slash", |
| 236 |
"asterisk", |
| 237 |
"spc", |
| 238 |
"caps_lock", |
| 239 |
"f1", |
| 240 |
"f2", |
| 241 |
"f3", |
| 242 |
"f4", |
| 243 |
"f5", |
| 244 |
"f6", |
| 245 |
"f7", |
| 246 |
"f8", |
| 247 |
"f9", |
| 248 |
"f10", |
| 249 |
"num_lock", |
| 250 |
"scroll_lock", |
| 251 |
"kp_divide", |
| 252 |
"kp_multiply", |
| 253 |
"kp_subtract", |
| 254 |
"kp_add", |
| 255 |
"kp_enter", |
| 256 |
"kp_decimal", |
| 257 |
"sysrq", |
| 258 |
"kp_0", |
| 259 |
"kp_1", |
| 260 |
"kp_2", |
| 261 |
"kp_3", |
| 262 |
"kp_4", |
| 263 |
"kp_5", |
| 264 |
"kp_6", |
| 265 |
"kp_7", |
| 266 |
"kp_8", |
| 267 |
"kp_9", |
| 268 |
"less", |
| 269 |
"f11", |
| 270 |
"f12", |
| 271 |
"print", |
| 272 |
"home", |
| 273 |
"pgup", |
| 274 |
"pgdn", |
| 275 |
"end", |
| 276 |
"left", |
| 277 |
"up", |
| 278 |
"down", |
| 279 |
"right", |
| 280 |
"insert", |
| 281 |
"delete", |
| 282 |
"stop", |
| 283 |
"again", |
| 284 |
"props", |
| 285 |
"undo", |
| 286 |
"front", |
| 287 |
"copy", |
| 288 |
"open", |
| 289 |
"paste", |
| 290 |
"find", |
| 291 |
"cut", |
| 292 |
"lf", |
| 293 |
"help", |
| 294 |
"meta_l", |
| 295 |
"meta_r", |
| 296 |
"compose", |
| 297 |
] |
| 298 |
|
| 299 |
CHAR_MAP = { |
| 300 |
"a": ["a"], |
| 301 |
"b": ["b"], |
| 302 |
"c": ["c"], |
| 303 |
"d": ["d"], |
| 304 |
"e": ["e"], |
| 305 |
"f": ["f"], |
| 306 |
"g": ["g"], |
| 307 |
"h": ["h"], |
| 308 |
"i": ["i"], |
| 309 |
"j": ["j"], |
| 310 |
"k": ["k"], |
| 311 |
"l": ["l"], |
| 312 |
"m": ["m"], |
| 313 |
"n": ["n"], |
| 314 |
"o": ["o"], |
| 315 |
"p": ["p"], |
| 316 |
"q": ["q"], |
| 317 |
"r": ["r"], |
| 318 |
"s": ["s"], |
| 319 |
"t": ["t"], |
| 320 |
"u": ["u"], |
| 321 |
"v": ["v"], |
| 322 |
"w": ["w"], |
| 323 |
"x": ["x"], |
| 324 |
"y": ["y"], |
| 325 |
"z": ["z"], |
| 326 |
"1": ["1"], |
| 327 |
"2": ["2"], |
| 328 |
"3": ["3"], |
| 329 |
"4": ["4"], |
| 330 |
"5": ["5"], |
| 331 |
"6": ["6"], |
| 332 |
"7": ["7"], |
| 333 |
"8": ["8"], |
| 334 |
"9": ["9"], |
| 335 |
"0": ["0"], |
| 336 |
"-": ["minus"], |
| 337 |
"=": ["equal"], |
| 338 |
"[": ["bracket_left"], |
| 339 |
"]": ["bracket_right"], |
| 340 |
"\\": ["backslash"], |
| 341 |
";": ["semicolon"], |
| 342 |
"'": ["apostrophe"], |
| 343 |
",": ["comma"], |
| 344 |
".": ["dot"], |
| 345 |
"/": ["slash"], |
| 346 |
"A": ["shift", "a"], |
| 347 |
"B": ["shift", "b"], |
| 348 |
"C": ["shift", "c"], |
| 349 |
"D": ["shift", "d"], |
| 350 |
"E": ["shift", "e"], |
| 351 |
"F": ["shift", "f"], |
| 352 |
"G": ["shift", "g"], |
| 353 |
"H": ["shift", "h"], |
| 354 |
"I": ["shift", "i"], |
| 355 |
"J": ["shift", "j"], |
| 356 |
"K": ["shift", "k"], |
| 357 |
"L": ["shift", "l"], |
| 358 |
"M": ["shift", "m"], |
| 359 |
"N": ["shift", "n"], |
| 360 |
"O": ["shift", "o"], |
| 361 |
"P": ["shift", "p"], |
| 362 |
"Q": ["shift", "q"], |
| 363 |
"R": ["shift", "r"], |
| 364 |
"S": ["shift", "s"], |
| 365 |
"T": ["shift", "t"], |
| 366 |
"U": ["shift", "u"], |
| 367 |
"V": ["shift", "v"], |
| 368 |
"W": ["shift", "w"], |
| 369 |
"X": ["shift", "x"], |
| 370 |
"Y": ["shift", "y"], |
| 371 |
"Z": ["shift", "z"], |
| 372 |
"!": ["shift", "1"], |
| 373 |
"@": ["shift", "2"], |
| 374 |
"#": ["shift", "3"], |
| 375 |
"$": ["shift", "4"], |
| 376 |
"%": ["shift", "5"], |
| 377 |
"^": ["shift", "6"], |
| 378 |
"&": ["shift", "7"], |
| 379 |
"*": ["shift", "8"], |
| 380 |
"(": ["shift", "9"], |
| 381 |
")": ["shift", "0"], |
| 382 |
"_": ["shift", "minus"], |
| 383 |
"+": ["shift", "equal"], |
| 384 |
"{": ["shift", "bracket_left"], |
| 385 |
"}": ["shift", "bracket_right"], |
| 386 |
"|": ["shift", "backslash"], |
| 387 |
":": ["shift", "semicolon"], |
| 388 |
'"': ["shift", "apostrophe"], |
| 389 |
"<": ["shift", "comma"], |
| 390 |
">": ["shift", "dot"], |
| 391 |
"?": ["shift", "slash"], |
| 392 |
" ": ["spc"], |
| 393 |
"\n": ["ret"], |
| 394 |
} |
| 395 |
|
| 396 |
def __init__(self, module): |
| 397 |
super().__init__(module) |
| 398 |
self.params = module.params |
| 399 |
self.total_keys = [] |
| 400 |
self.completed_keys = [] |
| 401 |
|
| 402 |
def string_to_keys(self, text): |
| 403 |
"""Convert text to key list.""" |
| 404 |
keys = [] |
| 405 |
for ch in str(text): |
| 406 |
ch_keys = self.CHAR_MAP.get(ch, None) |
| 407 |
if ch_keys is None: |
| 408 |
self.module.fail_json(f"The character {ch} passed to string_send is not allowed") |
| 409 |
key = "-".join(ch_keys) |
| 410 |
keys.append(key) |
| 411 |
return keys |
| 412 |
|
| 413 |
def validate_keys(self, keys): |
| 414 |
"""Validate keys.""" |
| 415 |
for key_combo in keys: |
| 416 |
for key in key_combo.split("-"): |
| 417 |
if key not in self.ALL_KEYS: |
| 418 |
self.module.fail_json(f"Key is invalid: {key}") |
| 419 |
|
| 420 |
def send_keys(self, vmid, keys_send, delay): |
| 421 |
"""Send keys to VM console.""" |
| 422 |
vm = self.get_vm(vmid) |
| 423 |
for key in keys_send: |
| 424 |
self.proxmox_api.nodes(vm["node"]).qemu(vmid).sendkey.put(key=key) |
| 425 |
self.completed_keys.append(key) |
| 426 |
if delay > 0.0: |
| 427 |
time.sleep(delay) |
| 428 |
|
| 429 |
def run(self): |
| 430 |
"""Main execution method.""" |
| 431 |
vmid = self.params.get("vmid") |
| 432 |
name = self.params.get("name") |
| 433 |
keys_send = self.params.get("keys_send") |
| 434 |
string_send = self.params.get("string_send") |
| 435 |
delay = self.params.get("delay") |
| 436 |
|
| 437 |
# Get vmid from name |
| 438 |
if not vmid: |
| 439 |
vmid = self.get_vmid(name) |
| 440 |
|
| 441 |
# Convert text to key list |
| 442 |
if string_send: |
| 443 |
keys_send = self.string_to_keys(string_send) |
| 444 |
self.total_keys = keys_send |
| 445 |
self.validate_keys(self.total_keys) |
| 446 |
|
| 447 |
self.send_keys(vmid, self.total_keys, delay) |
| 448 |
|
| 449 |
self.module.exit_json( |
| 450 |
changed=True, |
| 451 |
vmid=vmid, |
| 452 |
total_keys=self.total_keys, |
| 453 |
keys_num=len(self.total_keys), |
| 454 |
completed_keys_num=len(self.completed_keys), |
| 455 |
) |
| 456 |
|
| 457 |
|
| 458 |
def main(): |
| 459 |
"""Main entry point.""" |
| 460 |
module = create_proxmox_module(module_args(), **module_options()) |
| 461 |
proxmox = ProxmoxSendkeyAnsible(module) |
| 462 |
|
| 463 |
proxmox.run() |
| 464 |
|
| 465 |
|
| 466 |
if __name__ == "__main__": |
| 467 |
main() |