From 1f61be45d32b2632cd838c630599c08112b701a1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=B6rg=20Thalheim?= Date: Thu, 25 Dec 2014 21:47:26 +0100 Subject: [PATCH] add tinc-up --- tinc-up | 57 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 57 insertions(+) create mode 100755 tinc-up diff --git a/tinc-up b/tinc-up new file mode 100755 index 0000000..2b22bf4 --- /dev/null +++ b/tinc-up @@ -0,0 +1,57 @@ +#!/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"