Last active
February 29, 2024 23:43
-
-
Save dasimmet/e6037242602ae1b83722fe9e0b2ee5ea to your computer and use it in GitHub Desktop.
an openvpn wrapper script to start dhclient for layer 2 bridges in linux
This file contains hidden or 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
| #!/usr/bin/env bash | |
| # | |
| # | |
| set -e | |
| SELF="$0" | |
| OPENVPN="${OPENVPN:-openvpn}" | |
| main(){ | |
| if [ "$1" = "OPENVPN_BRIDGE_WRAPPER" ];then | |
| shift | |
| connect_hook "$@" | |
| else | |
| if [ -z "$*" ];then | |
| cat <<EOF | |
| $SELF | |
| by Tobias Simetsreiter <dasimmet@gmail.com> 2024 | |
| https://gist.github.com/dasimmet/e6037242602ae1b83722fe9e0b2ee5ea | |
| usage: | |
| $SELF --config <ovpn config file> | |
| this script run openvpn and starts dhclient on bridged dhcp connections. | |
| NetworkManager is supposed to do this but since 10 years noone implemented it. | |
| it works by running itself as an up-hook for the openvpn cli with a magic subcommand: | |
| openvpn --script-security 2 --up "$SELF OPENVPN_BRIDGE_WRAPPER" <ovpn args> | |
| EOF | |
| else | |
| "$OPENVPN" --script-security 2 --up "$SELF OPENVPN_BRIDGE_WRAPPER" "$@" | |
| fi | |
| fi | |
| } | |
| connect_hook(){ | |
| echo "$*" | |
| set -x | |
| if [ -z "$(which ip)" ];then | |
| if [ -z "$(which ifconfig)" ];then | |
| echo "Neither ip nor ifconfig found... Exiting with error." | |
| exit 1 | |
| else | |
| ifconfig "$1" up | |
| fi | |
| else | |
| ip link set "$1" up | |
| fi | |
| DHCLIENT="$(which dhclient)" | |
| if [ -z "$DHCLIENT" ]; | |
| then | |
| echo "dhclient not found, exiting with error...." | |
| exit 1 | |
| fi | |
| $DHCLIENT "$1" & | |
| } | |
| main "$@" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment