#!/bin/bash

echo "reboot to erase the old configurations;"
bash ~/reboot.sh
echo "rebooting"
countdown=40
while [ $countdown -gt 0 ]; do
	echo -ne "counting down: $countdown s \033[0K\r"
	sleep 1
	countdown=$((countdown - 1))
done

assign_ip(){
	local dev=$1
	local eth_num=$2
	local ip=$3
	ssh $dev "ip link set dev eth$eth_num up"
	ssh $dev "ip addr add $ip dev eth$eth_num"
	echo "dev $dev eth$eth_num assign $ip"
}

assign_br(){
				local dev=$1
				local ip=$2
				local ip_nocode=$3
				ssh $dev "ip link add name br0 type bridge"
				ssh $dev "ip link set dev br0 up"
				ssh $dev "ip address add $ip dev br0"
				ssh $dev "ip route append default via $ip_nocode dev br0"
}

turn_up(){
				local dev=$1
				local eth_num1=$2
				local eth_num2=$3
				local eth_num3=$4
				ssh $dev "ip link set eth$eth_num1 up"
				ssh $dev "ip link set eth$eth_num1 master br0"
				ssh $dev "ip link set eth$eth_num2 up"
				ssh $dev "ip link set eth$eth_num2 master br0"
				ssh $dev "ip link set eth$eth_num3 up"
				ssh $dev "ip link set eth$eth_num3 master br0"
}

ping_dev(){
	local dev=$1
	local ip=$2
	local eth_n=$3
	ssh $dev "which ping"
	local cmd="ping -c 5 -W 2 -I eth$eth_n $ip"
	echo $cmd
	ssh "$dev" $cmd
}

assign_ip "pc1" 1 "10.5.1.1/24"

assign_ip "pc2" 1 "10.5.1.2/24"

assign_ip "pc3" 1 "10.5.1.3/24"


assign_ip "router4" 1 "10.5.1.4/24"


assign_br "router1" "10.5.1.5/24" "10.5.1.5"
turn_up "router1" 1 3 4

assign_br "router2" "10.5.1.5/24" "10.5.1.5"
turn_up "router2" 1 3 4

assign_br "router3" "10.5.1.6/24" "10.5.1.6"
turn_up "router3" 1 2 3

ping_dev "pc1" "10.5.1.3" "1"
ping_dev "router4" "10.5.1.3" "1"
ping_dev "router4" "10.5.1.1" "1"
ping_dev "router4" "10.5.1.2" "1"
ping_dev "pc1" "10.5.1.4" "1"
ping_dev "pc2" "10.5.1.4" "1"
ping_dev "pc3" "10.5.1.4" "1"



