<commit_before>#Version 1.10, Updated Nov 22, 2015

#Made by <NAME> and <NAME>
#CS 336, Networks, Term Project, Southern Oregon University
#Pinger - A UNIX based script that expands on the built-in ping program. 
#         Provides a user friendly layout and useful statistical information about pings.

#!/bin/bash

clear
#this is to make it more user friendly, this is skipped if user is already authenticated as sudoer
echo "Pinger requires root/sudo access to work properly."
echo "Please Enter your Administrative (sudo) password: "
sudo echo "Success!" 


#creating folders for logs/files, -p flag ignores errors
mkdir -p ./whoisLogs
mkdir -p ./stats


#goAgain(), prompts user asking if they want to use the program again, calls mainMenu if they do
goAgain()
{
	while true; do
		echo -e "\n"
		read -p "Go again? (y/n)" yn
		case $yn in
			y)
				mainMenu
				break;;
			n)
				endProgram
				break;;
			*)
				echo "Invalid selection, try again! ";;
		esac
	done
}

#Stops the script's execution, start the script using "bash pinger.sh" to avoid your entire terminal being exited
endProgram()
{
	echo "Goodbye! Thanks for using Pinger."
	exit 1
}

#displays information about the program, gets input from user for the ip/domain of what they want to use
mainMenu()
{
	clear
	echo "========================================"
	echo "|--------------------------------------|"
	echo "|-----------------Pinger---------------|"
	echo "|---------By Chandler and Abdul--------|"
	echo "|-------Southern Oregon University-----|"
	echo "|-----------Networks, CS 336-----------|"
	echo "|------------Fall Term 2015------------|"
	echo "|--------------------------------------|"
	echo -e "========================================\n\n"
	
	read -p "Please Enter an IP Address or Domain Name: " name
	selMenu
}

#Prints out the menu for selections and calls the selector method
selMenu()
{
	clear
	echo "========== $name =========="
	echo "1: Ping $name"
	echo "2: Ping and make statistic file for $name"
	echo "3: Get WHOIS info about $name"
	echo "4: Go Back "
	echo "5: Quit Program"
	echo "========================================"
	selector
}

#Gets the user's selection, protecting against invalid input
selector() 
{
while true; do
	read -p "Make your Selection: " sel
	
	case $sel in 
	
	1)
		clear
		normalPing
		break;;
	2)
		clear
		statsPing
		break
		;;
	3)
		clear
		whoisFunc
		break
		;;
	4)
		mainMenu
		break
		;;
	5)
		endProgram
		break;;
	*)
		echo Invalid selection, Try Again! ;;
	esac
done
}


normalPing()
{
	sudo ping -ac 5 $name
	goAgain
}
statsPing()
{	
	echo "Pinging $name ..."
	sudo ping -fc 30 $name > ./stats/$name.out
	
	#offline=`grep -c '0 packets received' ./stats/$name.out`
	
	if [ `grep -c '0 packets received' ./stats/$name.out` -eq 1 ]
	then
		echo -e "\n\n$name is offline or has blocked pings."
		sudo rm ./stats/$name.out
	else
		lineA=`grep 'transmitted' ./stats/$name.out`
		lineB=`grep 'round-trip' ./stats/$name.out`
		
		echo -e "==========\nReport for $name:\n" > ./stats/$name.txt
		
		#########ADD IN packets info (transmitted/received/loss)
		
		grep 'round-trip' ./stats/$name.out| awk '{print $4}' | awk -F '/' '{print "Round-Trip-Time (RTT) info:\n\tMinimum: "$1 " miliseconds\n\tMaximum: " $3 " miliseconds\n\tAverage: " $2 " miliseconds\n\tStandard Deviation: " $4 " miliseconds"}' >> ./stats/$name.txt
	fi
	
	goAgain
}

whoisFunc()
{
	sudo whois $name > ./whoisLogs/$name.out
	
	#Seeing if the whois returns nothing (e.g. google.com doesn't return anything when we do this)
	lineCount=`grep -c -E -A 8 'Registrant|Administrative' ./whoisLogs/$name.out`
	if [ $lineCount -eq 0 ] 
	then
		echo -e "\n\nNo WHOIS information found for $name"
		sudo rm ./whoisLogs/$name.out
	
	else
		grep -E -A 8 'Registrant|Administrative' ./whoisLogs/$name.out > ./whoisLogs/$name.txt
		sudo rm ./whoisLogs/$name.out
		more ./whoisLogs/$name.txt
		echo -e "\n\nWHOIS information for $name saved in ./whoisLogs/$name.txt"
		

	fi
	
	goAgain
}


#This is where the first mainMenu call is.
mainMenu 








#This is the format of the statistic report file.

#===============
#   Report:
#Packets Transmitted: X
#Packets Received: X
#Packet Loss: X%
#
#Round-Trip-Time (RTT) info:
#	Minimum: X.xxx miliseconds
#	Maximum: XX.xxx miliseconds
#	
#	Average: XX.xxx miliseconds
#
#	Standard Deviation: XX.xxx miliseconds
#	95% of Ping RTTs to [site] will be between: XX.xxx and XX.xxx miliseconds
#================
<commit_msg>Pinger v2.00<commit_after>#Version 2.00, Updated Nov 23, 2015

#Made by <NAME> and <NAME>
#CS 336, Networks, Term Project, Southern Oregon University
#Pinger - A UNIX based script that expands on the built-in ping program. 
#         Provides a user friendly layout and useful statistical information about pings.

#!/bin/bash

########################################################
#VARIABLES
version=2.00
header="**********Pinger v$version**********\n"
name="127.0.0.1"
minPing=1
maxPing=100

#creating folders for logs/files, -p flag ignores errors
mkdir -p ./whoisLogs
mkdir -p ./stats
########################################################


#------------------------------------------------------#
#this is to make it more user friendly, this is skipped if user is already authenticated as sudoer
clear
echo "Pinger requires root/sudo access to work properly."
echo "Please Enter your Administrative (sudo) password: "
sudo echo "Success!" #sudo here makes user type password before starting
#------------------------------------------------------#


##################menu functions########################
########################################################
#displays information about the program, gets input from user for the ip/domain of what they want to use
mainMenu()
{
	clear
	creatorInfo
	read -p "Please Enter an IP Address or Domain Name: " name
	selMenu
}

#Prints out the menu for selections and calls the selector method
selMenu()
{
	clear
	echo -e $header
	echo "========== $name =========="
	echo "1: Ping $name"
	echo "2: Ping and make statistic file for $name"
	echo "3: Get WHOIS info about $name"
	echo "4: Go Back "
	echo "5: Quit Program"
	echo "========================================"
	selector
}

#Gets the user's selection, protecting against invalid input
selector() 
{
while true; do
	read -p "Make your Selection: " sel
	
	case $sel in 
	
	1)
		clear
		normalPing
		break;;
	2)
		clear
		statsPing
		break
		;;
	3)
		clear
		whoisFunc
		break
		;;
	4)
		mainMenu
		break
		;;
	5)
		endProgram
		break;;
	*)
		echo "Invalid selection, Try Again!" ;;
	esac
done
}
########################################################
########################################################


################ping/whois functions####################
########################################################
#normally pings a location, asks for number of pings from the user
normalPing()
{
	#Getting User input for number of pings
	clear
	echo -e $header
	while true; do
		read -p "Enter the desired amount of pings ($minPing - $maxPing): " pingCount
		re='^[0-9]+$'
		if ! [[ $pingCount =~ $re ]] ; then
			echo "Input must be a number!" 
		elif [ $pingCount -gt $maxPing ] ; then
			echo "Input must be between $minPing and $maxPing"
		elif [ $pingCount -lt $minPing ] ; then
			echo "Input must be between $minPing and $maxPing"
		else break;
		fi
	done
	
	#Starting the Ping Process
	clear
	echo -e $header
	echo -e "Pinging $name $pingCount times.\n"
	sudo ping -ac $pingCount $name
	goAgain
}

#pings location and creates statistic file with information
statsPing()
{	
	echo -e $header
	echo "Pinging $name ..."
	sudo ping -fc 60 $name > ./stats/$name.out
	
	if [ `grep -c '100.0% packet loss' ./stats/$name.out` -eq 1 ] ; then
		echo -e "\n\n$name is offline or has blocked pings."
		sudo rm ./stats/$name.out
	else
		#if the file exists already, we don't want to add more header info. Just keep appending reports to the bottom
		if ! [ -a ./stats/$name.txt ] ; then
			creatorInfo > ./stats/$name.txt
		fi
		echo -e "\n==============================\nPing statistics report for $name\nReport generated at: `date`\n" >> ./stats/$name.txt
		#------------------------------------------------------# 
		#Packet Transmission section of report
		echo -e "\nPacket Transmission info: " >> ./stats/$name.txt
		grep 'transmitted' ./stats/$name.out | awk -F ", " '{print "\t"$1"\n\t"$2"\n\t"$3"\n"}' >> ./stats/$name.txt
		#------------------------------------------------------#
		#RTT Info section of report
		stddev=`grep 'round-trip' ./stats/$name.out| awk '{print $4}' | awk -F '/' '{print $4}'`
		avgping=`grep 'round-trip' ./stats/$name.out| awk '{print $4}' | awk -F '/' '{print $2}'`
		minTime=`echo $avgping - 2 \* $stddev | bc -l`
		maxTime=`echo $avgping + 2 \* $stddev | bc -l`
		grep 'round-trip' ./stats/$name.out| awk '{print $4}' | awk -F '/' '{print "Round-Trip-Time (RTT) info:\n\tMinimum: "$1 " milliseconds\n\tMaximum: " $3 " milliseconds\n\tAverage: " $2 " milliseconds\n\tStandard Deviation: " $4 " milliseconds"}' >> ./stats/$name.txt
		echo -e "\t95% of ping RTTs to $name will be between $minTime ms and $maxTime ms" >> ./stats/$name.txt
		#------------------------------------------------------#
		echo -e "==============================" >> ./stats/$name.txt
		sudo rm -r ./stats/$name.out
		more ./stats/$name.txt
		echo -e "\n\nPing statistics file for $name saved in ./stats/$name.txt"
	fi
	goAgain
}

#gets whois information for the entered domain, will not work for IPs entered
whoisFunc()
{
	echo -e $header
	sudo whois $name > ./whoisLogs/$name.out
	
	#Seeing if the whois returns nothing (e.g. google.com doesn't return anything when we do this)
	lineCount=`grep -c -E -A 8 'Registrant|Administrative' ./whoisLogs/$name.out`
	if [ $lineCount -eq 0 ] ; then
		echo -e "\n\nNo WHOIS information found for $name"
		sudo rm ./whoisLogs/$name.out
	else
		creatorInfo > ./whoisLogs/$name.txt
		echo -e "WHOIS information for $name\nReport generated at: `date`\n" >> ./whoisLogs/$name.txt
		grep -E -A 8 'Registrant|Administrative' ./whoisLogs/$name.out >> ./whoisLogs/$name.txt
		sudo rm ./whoisLogs/$name.out
		more ./whoisLogs/$name.txt
		echo -e "\n\nWHOIS information for $name saved in ./whoisLogs/$name.txt"
	fi
	
	goAgain
}
########################################################
########################################################


################Extra Functions#########################
########################################################
#goAgain(), prompts user asking if they want to use the program again, calls mainMenu if they do
goAgain()
{
	while true; do
		echo -e "\n"
		read -p "Would you like to go again? (y/n)" yn
		case $yn in
			y)
				mainMenu
				break;;
			n)
				endProgram
				break;;
			*)
				echo "Invalid selection, try again! ";;
		esac
	done
}

#echos information of creators
creatorInfo()
{
	echo "========================================"
	echo "|--------------------------------------|"
	echo "|--------------Pinger v$version------------|"
	echo "|---------By <NAME> Abdul--------|"
	echo "|-------Southern Oregon University-----|"
	echo "|-----------Networks, CS 336-----------|"
	echo "|------------Fall Term 2015------------|"
	echo "|--------------------------------------|"
	echo -e "========================================\n"
}

#Stops the script's execution, start the script using "bash pinger.sh" to avoid your entire terminal being exited
endProgram()
{
	clear
	creatorInfo
	echo -e "Goodbye! Thanks for using Pinger.\n\n"
	exit 1
}
########################################################
########################################################

#Begin Execution Here: 
mainMenu 
{'fraction_non_alphanumeric': 0.260425604942509, 'fraction_numerical': 0.010296893770379269, 'mean_word_length': 2.928210313447927, 'pattern_counts': {'":': 0, '<': 8, '<?xml version=': 0, '>': 34, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 1, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2512995', 'n_tokens_mistral': 4033, 'n_tokens_neox': 3582, 'n_words': 1273}