Switch or power-cycle smart plugs from scripts, CI jobs and Rust: one binary, no Python or .NET at run time. Built for power-cycling hardware under test, where switching the wrong plug, or switching it back on too soon, is the thing to avoid.
TP-Link Kasa plugs come first, including firmware that only accepts KLAP (the encrypted HTTP protocol that needs the TP-Link account login). Kasa bulbs work too, for on, off, state and cycle only: no brightness or colour. Other brands can be added behind the same Brand and Plug traits.
- Plugs are identified by MAC, so a DHCP move cannot make it switch the wrong device: the MAC the plug reports is checked before anything is sent.
- Protected plugs refuse
offandcyclewithout--allow-protected, however they are addressed. - Each plug has a minimum off time:
cycleuses it by default and refuses anything shorter, and anonsoon after plugctl's ownoffwaits out the rest. - Each plug has a lock, so two jobs on one machine never switch it at once, and a test run can hold it throughout.
- Plugs on an isolated network are reached through an SSH tunnel on a host that can see them.
Download the archive for your platform from Releases and put plugctl (or plugctl.exe) on your PATH. There are static Linux builds for x86_64 and aarch64 (which also run on aarch64 OpenWrt routers), Windows x86_64, and macOS on Apple silicon and Intel. SHA256SUMS lists each archive's checksum.
On Windows, Chocolatey installs it and puts it on PATH (--pre while releases are pre-releases; a new version appears once Chocolatey's moderators approve it):
choco install plugctl --preEach archive and image carries a build provenance attestation, which shows that this repository's release workflow built it and from which commit:
gh attestation verify plugctl-<version>-<target>.tar.gz -R zompinc/plugctl
gh attestation verify oci://ghcr.io/zompinc/plugctl:<version> -R zompinc/plugctlThe Windows binary is Authenticode-signed by Zomp Inc.; the macOS binaries are not signed yet. On macOS, a binary downloaded with a browser needs xattr -d com.apple.quarantine plugctl before it runs.
Each release is also an image for linux/amd64 and linux/arm64, ghcr.io/zompinc/plugctl:<version> (latest follows stable releases only). It keeps the config and the off-time log in /config, and takes the login from the environment, since there is no keyring in a container:
docker run --rm --network host -v ~/.config/plugctl:/config \
-e KASA_USERNAME -e KASA_PASSWORD \
ghcr.io/zompinc/plugctl:<version> state benchPlug locks are kept in /config/locks, so containers sharing the /config volume also share the locks.
--network hostis what lets discovery broadcasts and direct connections reach the plugs. It works on a Linux host; Docker Desktop on Windows and macOS runs containers in a VM that may not see the plugs' network, so use an SSH route or the native binary there.- For Docker secrets or other mounted files, set
PLUGCTL_KASA_USERNAME_FILEandPLUGCTL_KASA_PASSWORD_FILEto their paths instead. - For an SSH route, mount the keys and
known_hostswith-v ~/.ssh:/home/plugctl/.ssh:ro. The container runs as uid 1000, which must own them, as it usually does for the first user on a Linux host.
Needs Rust; the exact toolchain is pinned in rust-toolchain.toml, and rustup installs it on first build.
cargo build --release
# the binary: target/release/plugctl (plugctl.exe on Windows)For a container or a host without an OS keyring, build without it and pass the login through the environment:
cargo build --release --no-default-featuresplugctl config-path # where the config is read from
plugctl login # save the TP-Link account login in the OS keyring
plugctl discover # find Kasa devices on this network: MAC, IP, model, protocol
plugctl add bench 192.0.2.40 --min-off 8s # connect, read its MAC, add it to the config
plugctl list # every configured plug: on/off, model, MAC, address
plugctl state bench # one plug; --json for scripts
plugctl on bench
plugctl off bench
plugctl cycle bench # off for the plug's min_off (or 5 s), then on
plugctl cycle bench --off 20s
plugctl lock bench -- ./run-tests.sh # no other job switches bench until this ends
plugctl wait bench --above 2W --for 5s # on plugs with a power meter: wait for the load to drawA plug can be named by its config name, its MAC, or an IP address. --via <route> sends a request through a configured route (or a raw SSH destination). A plug given by IP still gets the rules of whichever configured plug has its MAC.
Exit status is 0 on success and 1 on any failure, with the reason on stderr. cycle retries switching back on with a fresh connection, and says plainly if the plug was left off.
min_off holds for a separate off and on too: plugctl records when it switches a plug off (plugctl/off-times.json in the user's local data directory, or wherever PLUGCTL_OFF_LOG points), and an on within min_off of that waits out the rest first. A plug switched off by the app or its button has no record, and is not waited on.
On devices with a power meter (Kasa KP115, KP125 and HS110 plugs, and Kasa bulbs such as the KL125), state shows the draw in watts (power_w in --json). plugctl wait <plug> --above 2W waits until it is over that, which after a cycle tells a load that came back up from one that did not; --below waits for it to go idle. --for 5s makes the level hold that long without a break, so a spike at power-on does not count, and --timeout (60 s by default) bounds the wait, after which it exits 1 with the last reading. A plug without a meter fails at once.
on, off and cycle take the plug's lock first. When another job holds it, they fail at once with who holds it and since when, or wait for it with --wait-lock 5m. plugctl lock <plug> -- <command> holds it while the command runs, for a test run that cycles the plug and then needs it left alone; plugctl calls inside the command switch the plug as usual, and lock exits with the command's status. Name the holder with --holder or PLUGCTL_HOLDER; it defaults to the process ID and host.
The lock is the operating system's file lock on a file in plugctl/locks in the user's local data directory (or PLUGCTL_LOCK_DIR), so it is released when the process ends, even if it crashes. It covers jobs on one machine, run as users who share that directory: two machines switching the same plug do not see each other's locks.
A TOML file in the user's config directory (%APPDATA%\plugctl\config.toml on Windows, ~/.config/plugctl/config.toml on Linux), or wherever --config or PLUGCTL_CONFIG points. See docs/config.example.toml:
[routes.iot]
ssh = "user@router.example"
[[plugs]]
name = "bench"
mac = "00:00:5E:00:53:01"
route = "iot"
min_off = "8s"
[[plugs]]
name = "printer"
mac = "00:00:5E:00:53:02"
route = "iot"
min_off = "30s"
protected = trueplugctl add <name> <ip> writes an entry for you (add --via <route>, --min-off and --protected as needed), appending to the file so its comments stay.
host is an optional address to try first. Without it, or when it answers with another MAC, plugctl looks the MAC up: through an SSH route it reads the DHCP leases and neighbour table on that host (override with lookup); directly, it sends a Kasa discovery broadcast and then checks this host's ARP cache.
plugctl discover sends both Kasa probes: the legacy one on UDP 9999 and the current one on 20002, which KLAP devices answer. Two things can hide devices from it:
- Firmware that does not answer. Kasa HS103 on firmware 1.1.x answers neither probe. Find its IP in your router or the Kasa app and use
plugctl add. A configured plug like this is still found by MAC through the ARP cache or the router's leases. - A firewall on the host. Replies to a broadcast come from each device's own address, so connection tracking does not match them and a host firewall that drops unsolicited input drops them too. On a host with several networks (a router), also pass that network's broadcast address, such as
--broadcast 192.0.2.255, or setbroadcast = ["192.0.2.255"]at the top of the config. On OpenWrt, a rule accepting UDP from source ports 9999 and 20002 on that zone lets the replies in.
Tried in this order:
PLUGCTL_KASA_USERNAMEandPLUGCTL_KASA_PASSWORD, orPLUGCTL_KASA_USERNAME_FILEandPLUGCTL_KASA_PASSWORD_FILEnaming files that hold them (Docker and Kubernetes secrets).- python-kasa's
KASA_USERNAMEandKASA_PASSWORD. - The OS keyring entry
plugctl/kasathatplugctl loginwrites: Windows Credential Manager, macOS Keychain, or the Secret Service on Linux desktops.plugctl logoutremoves it.
When the plugs sit on a network this machine cannot route to (an IoT VLAN, say), give them a route with an SSH destination that can. Each command opens ssh -N -L to the plug's port for as long as it runs, so the SSH host needs nothing installed, but it must accept key authentication without a prompt (BatchMode=yes). The system ssh client is used, so ~/.ssh/config aliases and keys work as usual.
The other option is to run plugctl on a host that is on that network, such as the router itself, and call it there over SSH. The static aarch64 Linux build runs on aarch64 OpenWrt routers (tested on one). Pass the login through the environment or the _FILE variables, since a router has no keyring.
The CLI is a thin layer over the plugctl crate in crates/plugctl:
use plugctl::{Config, Controller, CycleOptions};
let config = Config::load(&Config::default_path().unwrap())?;
let mut plug = Controller::new(config).connect("bench", None).await?;
println!("{} is {}", plug.name, if plug.info.on { "on" } else { "off" });
plug.cycle(CycleOptions::default()).await?;A new brand implements Brand (connect, discover, port) and Plug (info, set on/off), and is added to brand_by_name, or passed in through Controller::with_brands.
pnpm install # the pre-commit hook: Prettier, final newlines, cargo fmt
cargo clippy --all-targets -- -D warnings
cargo testTests that need a real plug are opt-in; see docs/manual-tests.md.
MIT, copyright Zomp Inc.