Skip to content

Instantly share code, notes, and snippets.

@milesrichardson
Created August 2, 2022 21:37
Show Gist options
  • Save milesrichardson/0c5ae30bbf03596922488863da98d168 to your computer and use it in GitHub Desktop.
Save milesrichardson/0c5ae30bbf03596922488863da98d168 to your computer and use it in GitHub Desktop.
Python script to create random wireguard peer by calling`wg genkey`, `wg pubkey` and `wg genpsk` with `subprocess` module
# Hopefully it saves you a few minutes looking at the subprocess docs
import os
import subprocess
env = os.environ.copy()
private_key = (
subprocess.run(
["wg", "genkey"],
env=env,
capture_output=True,
)
.stdout.decode()
.rstrip("\n")
)
public_key = (
subprocess.Popen(
["wg", "pubkey"], stdin=subprocess.PIPE, env=env, stdout=subprocess.PIPE
)
.communicate(input=private_key.encode())[0]
.decode()
.rstrip("\n")
)
preshared_key = (
subprocess.run(["wg", "genpsk"], env=env, capture_output=True)
.stdout.decode()
.rstrip("\n")
)
print("PRIVATE: {}".format(private_key))
print("PUBLIC : {}".format(public_key))
print("PSK : {}".format(preshared_key))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment