#!/usr/bin/env bash set -eu DIR=$(cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd) MAC_ADDRESS_FILE=${DIR}/tinc-macaddr has() { command -v "$1" >/dev/null 2>&1 } die() { echo $1 &>2 exit 1 } set_mac() { local interface=$1 local mac_address=$2 if has ifconfig; then ifconfig "$interface" down if [[ "$OSTYPE" == "linux-gnu" ]]; then ifconfig "$interface" hw ether "$mac_address" else ifconfig "$interface" ether "$mac_address" fi elif has ip; then ip link set dev "$interface" down ip link set dev "$interface" address "$mac_address" else die "no suitable program found to configure the network interface, need iproute2 or ifconfig" fi } start_dhcp() { local interface=$1 if has dhcpcd; then dhcpcd "$interface" elif has dhclient; then dhclient "$interface" elif [[ "$OSTYPE" == "darwin"* ]]; then # TODO untested ipconfig set "$interface" DHCP else die "no suitable dhcp program found, need dhcpcd or dhclient" fi } [ -z "${INTERFACE:-}" ] && die "no INTERFACE environment variable set" if [ ! -f "$MAC_ADDRESS_FILE" ]; then die "${MAC_ADDRESS_FILE} does not exists. Generate it with ./tinc-generate-mac" fi read -r MAC_ADDRESS <"$MAC_ADDRESS_FILE" set_mac "$INTERFACE" "$MAC_ADDRESS" start_dhcp "$INTERFACE"