Skip to content

Instantly share code, notes, and snippets.

@castironclay
Last active April 14, 2020 23:54
Show Gist options
  • Save castironclay/c25329f11868eb780fede4653e6b140d to your computer and use it in GitHub Desktop.
Save castironclay/c25329f11868eb780fede4653e6b140d to your computer and use it in GitHub Desktop.
Connect to remote wireguard server and create tunnel
#!/bin/bash
echo Peer IP Address?
read PEERIP
echo Peer Username?
read PEERUSER
echo Peer Password?
read -s PEERPASS
echo Connecting to $PEERIP...
sleep 1
wg genkey | tee privatekey | wg pubkey > publickey
CLIENTPUB="$(cat publickey)"
sudo ip link del dev wg3
sudo ip link add dev wg3 type wireguard
sudo ip address add dev wg3 10.0.1.2/24
echo $PEERPASS | ssh -t $PEERUSER@$PEERIP 'mkdir wg3; cd wg3; wg genkey | tee privatekey | wg pubkey > publickey; sudo ip link del dev wg3; sudo ip link add dev wg3 type wireguard;'
scp publickey $PEERUSER@$PEERIP:~/wg3/clientpublickey
echo $PEERPASS | ssh -t $PEERUSER@$PEERIP 'cd wg3; sudo ip address add dev wg3 10.0.1.1/24; CLIENTPUB="$(cat clientpublickey)"; sudo wg set wg3 listen-port 51820 private-key privatekey peer $CLIENTPUB allowed-ips 10.0.1.2/32 persistent-keepalive 10; sudo ip link set wg3 up'
scp $PEERUSER@$PEERIP:~/wg3/publickey ./serverpublickey
SERVERPUB="$(cat serverpublickey)"
sudo wg set wg3 private-key privatekey peer $SERVERPUB allowed-ips 0.0.0.0/0 endpoint $PEERIP:51820 persistent-keepalive 10
sudo ip link set wg3 up
sudo route add default dev wg3
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment