add script to configure ip address
This commit is contained in:
parent
1f61be45d3
commit
b870478b72
|
@ -2,3 +2,4 @@ rsa_key.priv
|
|||
ecdsa_key.priv
|
||||
tinc.conf
|
||||
tinc-macaddr
|
||||
tinc-subnet
|
||||
|
|
46
tinc-up
46
tinc-up
|
@ -3,6 +3,7 @@ set -eu
|
|||
|
||||
DIR=$(cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd)
|
||||
MAC_ADDRESS_FILE=${DIR}/tinc-macaddr
|
||||
TINC_SUBNET_FILE=${DIR}/tinc-subnet
|
||||
|
||||
has() {
|
||||
command -v "$1" >/dev/null 2>&1
|
||||
|
@ -31,6 +32,40 @@ set_mac() {
|
|||
fi
|
||||
}
|
||||
|
||||
mask2cidr() {
|
||||
local netmask=$1
|
||||
local nbits=0
|
||||
IFS=.
|
||||
for dec in "$netmask"; do
|
||||
case $dec in
|
||||
255) let nbits+=8;;
|
||||
254) let nbits+=7;;
|
||||
252) let nbits+=6;;
|
||||
248) let nbits+=5;;
|
||||
240) let nbits+=4;;
|
||||
224) let nbits+=3;;
|
||||
192) let nbits+=2;;
|
||||
128) let nbits+=1;;
|
||||
0);;
|
||||
*) die "Error: $dec is not recognised"
|
||||
esac
|
||||
done
|
||||
echo "$nbits"
|
||||
}
|
||||
|
||||
set_ip() {
|
||||
local interface=$1
|
||||
local ip=$2
|
||||
local netmask=$3
|
||||
if has ifconfig; then
|
||||
ifconfig "$interface" "$ip" netmask "$netmask"
|
||||
elif has ip; then
|
||||
ip addr add "$ip" dev "$interface"
|
||||
else
|
||||
die "no suitable program found to set ip address, need iproute2 or ifconfig"
|
||||
fi
|
||||
}
|
||||
|
||||
start_dhcp() {
|
||||
local interface=$1
|
||||
if has dhcpcd; then
|
||||
|
@ -54,4 +89,13 @@ fi
|
|||
read -r MAC_ADDRESS <"$MAC_ADDRESS_FILE"
|
||||
set_mac "$INTERFACE" "$MAC_ADDRESS"
|
||||
|
||||
start_dhcp "$INTERFACE"
|
||||
if [ -e $TINC_SUBNET_FILE ]; then
|
||||
read -a SUBNET <"$TINC_SUBNET_FILE"
|
||||
IP=${SUBNET[0]}
|
||||
NETMASK=${SUBNET[1]}
|
||||
[ -z "$IP" ] && die "no ip set in '$TINC_SUBNET_FILE'"
|
||||
[ -z "$NETMASK" ] && die "no netmask set in '$TINC_SUBNET_FILE'"
|
||||
set_ip "$INTERFACE" "$IP" "$NETMASK"
|
||||
else
|
||||
start_dhcp "$INTERFACE"
|
||||
fi
|
||||
|
|
Loading…
Reference in New Issue