#!/usr/bin/env python3
import argparse
import subprocess
import sys
import datetime

def ts_print(msg):
    """ts_print message prefixed with a timestamp (YYYY-MM-DD HH:MM:SS)."""
    now = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")
    print(f"{now} {msg}")
    
IPMIUSER = "<username>"
IPMIPW = "<password>"
IPMIEK = "0000000000000000000000000000000000000000"

def main():
    p = argparse.ArgumentParser(description="Send IPMI power off")
    p.add_argument("-H", "--host", required=True, help="IPMI host address")
    args = p.parse_args()

    cmd = [
        "ipmitool",
        "-I", "lanplus",
        "-H", args.host,
        "-U", IPMIUSER,
        "-P", IPMIPW,
        "-y", IPMIEK,
        "power", "on",
    ]
    print(f"Sending power on to {args.host}")
    proc = subprocess.run(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True)
    if proc.returncode != 0:
        print("ipmitool failed:", proc.stderr.strip(), file=sys.stderr)
        sys.exit(proc.returncode)
    print(proc.stdout.strip())

if __name__ == "__main__":
    main()
