Question

In: Computer Science

Assignment Write a network diagnostics bash script that does the following: Finds the IPv4 address of...

Assignment

Write a network diagnostics bash script that does the following:

  • Finds the IPv4 address of the default gateway on your machine (one way to get this is the 'default' entry, also shown as 0.0.0.0, in the output of 'ip route') and runs a ping (with a count of 5 pings) to it.
  • Runs another count of 5 pings to the site example.com.
  • Outputs a 1-line summary of interfaces on your machine, not including the loopback address (lo).
  • Outputs a 1-line summary of how many TCP ports are open (in state LISTEN)

The output of your script should look exactly like this (different numbers are OK). For the ping times output, use the 'avg' number from the ping command. This is the average that is calculated for you -- no need to calculate it in your script.

Ping time to default gateway:  0.348 ms
Ping time to example.com: 24.807 ms
Interface count:  2, and 2/2 are up
There are 7 TCP ports in the LISTEN state

Solutions

Expert Solution


I have tried to be as descriptive as possible.

Here is the script :

#!/bin/sh

#here we get the ip of the default gateway.
default_ip=`ip route | grep default | awk '{print $3}'`

#now we ping to the default_ip address 5 times and store the avg score
avg_default=`ping -c 5 $default_ip | grep rtt | awk '{print $4}' | awk -F\/ '{print $2}'`

#We do the same for the example.com
avg_example=`ping -c 5 example.com | grep rtt | awk '{print $4}' | awk -F\/ '{print $2}'`


#Here we get the interfaces available on the machine and store the names in the variable 'interfaces'
interfaces=`ls -A /sys/class/net | grep -v lo`

#now we count the number of interfaces using word count(wc).
num_interfaces=`ls -A /sys/class/net | grep -v lo | wc -l`

#we declare a variable for the interfaces that are up initialized to 0.
up_interfaces=0

#this iterates over each interface
for i in $(seq 1 $num_interfaces)
do
   #here we retrieve the i-th interface in the variable 'interfaces'
   eth=`echo $interfaces | cut -d' ' -f $i`
   #now we get the status of that interface
   status=`cat /sys/class/net/$eth/operstate`
  
   # if the interface is up then var 'up_interfaces' is incremented
   if [ "$status" = "up" ]
   then
       up_interfaces=$((up_interfaces+1))
   fi
done  

#Here we get the number of TCP ports that are open and are in LISTEN state.
n=`netstat -anp | grep -w -c LISTEN`

echo "OUTPUT:"
echo "Ping time to default gateway: $avg_default ms."
echo "Ping time to example.com: $avg_example ms"
echo "Interface count: $num_interfaces, and $up_interfaces / $num_interfaces are up."
echo "There are $n TCP ports in the LISTEN state."


Related Solutions

Program in Bash: Write a program using bash script that can read a file from the...
Program in Bash: Write a program using bash script that can read a file from the same directory, sort the nonrepeating integers from 0-9 from smallest to largest, and output the results on the same line. Do not use the sort function.
Program in Bash: Write a program using bash script that can read a file from the...
Program in Bash: Write a program using bash script that can read a file from the same directory, sort the nonrepeating integers from 0-9 from smallest to largest, and output the results on the same line. Do not use the sort function.
Using a 23 bit network prefix, what network would the IPv4 address 123.154.89.114 be forwarded to?  Use...
Using a 23 bit network prefix, what network would the IPv4 address 123.154.89.114 be forwarded to?  Use dotted decimal format and do not include the mask in your answer.
Fully Functional Script written in BASH In this section, you will write a fully functional script...
Fully Functional Script written in BASH In this section, you will write a fully functional script to help your manager with an important project that will build upon what you learned from your script work in Milestone Four. After you have written the script, you will execute it to generate a report for all three users (once for Bob, once for Henry, and once for Frank). You should have three unique generated reports in your ~/scripts directory (or more if...
⦁   Write a Bash script that prompts for user input and reads a string of text...
⦁   Write a Bash script that prompts for user input and reads a string of text from the user. If the user enters a no null (no empty) string, the script should prompt the user to re-enter again the string; otherwise should display the string entered “. ⦁   Given an array of the following four integers (3, 5, 13, 14); write a Bash script to display the second and all elements in the array.    ⦁   Write a short Bas...
this is bash scripting. a. Write a shell script that adds an extension “.deb” to all...
this is bash scripting. a. Write a shell script that adds an extension “.deb” to all the files in a directory. b. Write a command sequence or a script to insert a line “GHIJKLM” at every 10th line of a file? c. Write a bash command or script to find all the files modified in less than 5 days and print the record count of each.
Bash Script Write a script using while-do-done loop to convert the kilometers to miles. - Ask...
Bash Script Write a script using while-do-done loop to convert the kilometers to miles. - Ask the user to input the number of kilometers they need to travel. - Display the number of equivalent miles for the number. Use formula, Kilometers = miles/0.62137 - Every time the loop runs, it should ask the user if they want to continue, if user enters “Y” or “y”, then the loop runs again, if user enters “N” or “n”, then stop the loop...
Write a bash script that... create new user ./finalProject user if a new user is to...
Write a bash script that... create new user ./finalProject user if a new user is to be created ask for the new users information and use it when creating the new user add a new printer ./finalProject printer ask anything you need in order to create the new printer (I.e. name) permissions ./finalProject permissions ask what document and permissions the user wants restarting the computer ./finalProject restart
Write a bash script named q1f.sh that will edit the PATH environment variable to include the...
Write a bash script named q1f.sh that will edit the PATH environment variable to include the sourcefiles directory in your home directory and make the new variable global. Please show picture of output.
Write a bash script that will allow you to: Read in your Your name Course name...
Write a bash script that will allow you to: Read in your Your name Course name and Instructor’s name Then prompt the user to enter two numbers and determine which number is greater. Ex: If 10 is entered for the first number and 3 for the second, you should output Your name Course name Instructor’s name 10 is greater than 3. If 33 is entered for the first number and 100 for the second, you shou output Your name Course...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT