0

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
  • Why not pipe the result of the genkey directly to the pubkey? – Gerhard Feb 16 '22 at 11:58
  • @Gerhard - Based on his bash example, he wants to save the result of the genkey to a file, as well as sending it on to gen the pubkey. – Jeff Zeitlin Feb 16 '22 at 12:05
  • 2
    PowerShell has a `Tee-Object` cmdlet; perhaps you should look at doing this in PowerShell instead of winbatch? – Jeff Zeitlin Feb 16 '22 at 12:06
  • @JeffZeitlin, OP states _" I would prefer if the keys are not written to any files."_ – Gerhard Feb 16 '22 at 12:10
  • @Gerhard - I somehow missed that. In that case, `Tee-Object` is inappropriate, but it can still be done in PowerShell if the querent considers that an acceptable route. One can assign the output of a command to a variable via `$foo = (desiredcommand)`, and then use `$foo` as input/parameters to other commands or the pipeline. – Jeff Zeitlin Feb 16 '22 at 12:15
  • Sadly I am not familiar at all with powershell. Is there no way of doing this in batch? – TheHellSite Feb 16 '22 at 12:35
  • Have you tried piping directly from one to the next? I do now have `wg` to test this for you. – Gerhard Feb 16 '22 at 12:39
  • Could you please give an example for this? I don't quite get what you mean by that. – TheHellSite Feb 16 '22 at 13:12
  • for instance `wg genkey| wg pubkey` – Gerhard Feb 16 '22 at 13:48
  • ````for /f "tokens=* usebackq" %%f in (wg genkey | wg pubkey) do (set var_publickey=%%f)```` This will lose the private key and only save the public key. – TheHellSite Feb 16 '22 at 15:00
  • Does this help you: [Using a custom Tee command for .bat file](https://stackoverflow.com/a/10719322)? – aschipfl Feb 16 '22 at 18:17
  • This might even work. But is pretty overkill for just one command I think. Thanks anyway! – TheHellSite Feb 17 '22 at 12:09

0 Answers0