#!/bin/bash

bash /home/rnp/2/101.sh

echo "Now running 103.sh"

assign_vlan(){
	local dev=$1
	local eth=$2
	local id=$3
	local ip=$4
	ssh $dev "ip link add link $eth name $eth.$id type vlan id $id"
	ssh $dev "ip link set dev $eth.$id up"
	ssh $dev "ip addr flush dev $eth"
	ssh $dev "ip addr add $ip dev $eth.$id"
}

assign_vlan "router4" "eth1" "100" "10.5.1.4/24"
assign_vlan "pc3" "eth1" "100" "10.5.1.3/24"
assign_vlan "pc2" "eth1" "200" "10.5.1.2/24"
assign_vlan "pc1" "eth1" "200" "10.5.1.1/24"

ping_dev(){
	local dev=$1
	local ip=$2
	local eth_n=$3
	# ssh "$dev" $cmd 
	loss=$(ssh $dev "ping -c 5 -W 2 -I eth$eth_n $ip | awk -F', ' '/packet loss/ {print \$3}' | awk '{print int(\$1)}'")
	output=$(ssh $dev "ping -c 5 -W 2 -I eth$eth_n $ip")
	echo $output > 2/output/"${dev}_${ip}_${eth_n}"
	echo $loss
}

check(){
	local dev1=$1
	local dev2=$2
	local ping_loss=$3
	local num=$4
	echo $ping_loss
	if [ $ping_loss -eq $num ]; then
		echo -e "from $dev1 to $dev2: \t yes"
	else
		echo -e "from $dev1 to $dev2: \t no"
	fi
}

loss=$(ping_dev "router4" "10.5.1.1" "1.100")
check "router4" "pc1" "$loss" 100
loss=$(ping_dev "router4" "10.5.1.2" "1.100")
check "router4" "pc2" "$loss" 100
loss=$(ping_dev "router4" "10.5.1.3" "1.100")
check "router4" "pc3" "$loss" 0

loss=$(ping_dev "pc1" "10.5.1.2" "1.200")
check "pc1" "pc2" "$loss" 0
loss=$(ping_dev "pc1" "10.5.1.3" "1.200")
check "pc1" "pc3" "$loss" 100
loss=$(ping_dev "pc1" "10.5.1.4" "1.200")
check "pc1" "router4" "$loss" 100

loss=$(ping_dev "pc2" "10.5.1.1" "1.200")
check "pc2" "pc1" "$loss" 0
loss=$(ping_dev "pc2" "10.5.1.3" "1.200")
check "pc2" "pc3" "$loss" 100
loss=$(ping_dev "pc2" "10.5.1.4" "1.200")
check "pc2" "router4" "$loss" 100

loss=$(ping_dev "pc3" "10.5.1.1" "1.100")
check "pc3" "pc1" "$loss" 100
loss=$(ping_dev "pc3" "10.5.1.2" "1.100")
check "pc3" "pc2" "$loss" 100
loss=$(ping_dev "pc3" "10.5.1.4" "1.100")
check "pc3" "router4" "$loss" 0
