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

Linux Sign into your lab account Write a bash shell script that does the following: Print...
Linux Sign into your lab account Write a bash shell script that does the following: Print out a friendly welcome message Ask the user for the name of their favorite animal Grep that animal from a text file noises.txt Tell the user what that animal says (i.e. what noise does it make) If the animal does not exist ask the user what noise the animal makes Store that new information in noises.txt
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.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT