Created
August 2, 2022 21:37
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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