#!/bin/bash

run204=$1
ping_type=$2
if [ "$run204" = "y" ]; then
	bash /home/rnp/2/204.sh
fi
echo "205" > /home/rnp/2/205.txt

get_v6(){
	local output=$1
	local eth_n=$2
	echo "$output" | grep -A 1 "^[0-9]: eth$eth_n" | awk '/inet6/ {split($2, parts, "/"); print parts[1]}'
}

pc1_ip6=$(ssh "pc1" "ip -6 addr show")
pc1_6=$(get_v6 "$pc1_ip6" 1)
echo $pc1_6
echo -e $pc1_ip6 >> /home/rnp/2/205.txt

pc2_ip6=$(ssh "pc2" "ip -6 addr show")
pc2_6=$(get_v6 "$pc2_ip6" 1)
echo -e $pc2_6 >> /home/rnp/2/205.txt

ping6_dev(){
	local dev=$1
	local ip=$2
	local eth_n=$3
	local output=$(ssh $dev "ping6 -c 5 -W 2 -I eth$eth_n $ip")
	echo -e $output
}

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 -e $output > 2/output/"${dev}_${ip}_${eth_n}"
	echo $loss
}

echo "using ping"
echo "using ping" >> /home/rnp/2/205.txt


if [ $ping_type -eq 4 ]; then
	output=$(ping_dev "pc1" "10.5.1.2" 1)
	echo $output
	echo $output >> /home/rnp/2/205.txt

	output=$(ping_dev "pc2" "10.5.1.1" 1)
	echo $output
	echo $output >> /home/rnp/2/205.txt
elif [ $ping_type -eq 6 ]; then
	echo "$pc2_6"
	output=$(ping6_dev "pc1" "$pc2_6" 1)
	echo -e $output
	echo -e $output >> /home/rnp/2/205.txt

	output=$(ping6_dev "pc2" "$pc1_6" 1)
	echo "$pc1_6"
	echo -e $output
	echo -e $output >> /home/rnp/2/205.txt
fi


