I am trying to write a small batch script that will run on Windows systems and generate new WireGuard config files that are ready for deployment in a road warrior setup.
To do this I am saving the output of wg genkey
, wg pubkey
and wg genpsk
to variables in the script.
Based on these variables it will then generate a new client.conf
.
In bash
this wouldn't be a problem.
wg genkey | tee privatekey | wg pubkey > publickey
In batch
however I can't figure out a nice and clean way of piping the private key to the wg pubkey
command without loosing the private key of the wg genkey
command.
Preferred Solution
Public key generation not working.
REM generate private key --> WORKING
for /f "tokens=* usebackq" %%f in (`wg genkey`) do (set var_privatekey=%%f)
REM generate public key --> NOT WORKING
for /f "tokens=* usebackq" %%f in (`echo %var_privatekey% ^| wg pubkey`) do (set var_publickey=%%f)
REM generate preshared key --> WORKING
for /f "tokens=* usebackq" %%f in (`wg genpsk`) do (set var_presharedkey=%%f)
Woraround
Use tmp files. Though this is working I would prefer if the keys are not written to any files.
REM generate public key from var_privatekey
REM
echo %var_privatekey% | (wg pubkey) >publickey.tmp & <publickey.tmp (set /p var_publickey=)
del publickey.tmp