Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Procedure: These steps assume you are using the Ubuntu Linux. Shell Scripts as \

ID: 3801883 • Letter: P

Question

Procedure: These steps assume you are using the Ubuntu Linux. Shell Scripts as "wrappers" for Linux Commands A "wrapper" is typically a function that is used to provide a simpler interface to another function. In this problem you will develop bash shell scripts that use the standard Linux commands to provide the user with a simple means to access the underlying functionality available in Linux. For directories with a large number of files, it is sometimes helpful to be able to list the contents based on some category of the files. Three obvious categories are: 1) directories 2) ordinary, executable files (these would be scripts or binary code) 3) ordinary, non-executable files (these would typically be text files, either data or source code) We assume that none of the hidden files will be listed when this script is run. Write a bash script named dlist that takes one argument, either a d, or an x, or an nx. If no argument or more than one argument is given, then print an error message, such as "incorrect number of arguments If the argument is "d', then dlist lists all of the directories within the current directory. If the argument is 'x' then dlist lists all of the ordinary, executable files (not directory). If the argument is nx' then dlist lists all the ordinary, non-executable files in the current directory a) Write the dlist script, test it, and then save it in your ee462 directory in AFS as dlist.

Explanation / Answer

#!/bin/bash
# $Header: $
# First attempt at a consolidated auth log collection from kaserver
# Timestamps in the raw files are NOT designed for easy sorting.
  
# Options:
# -i -- translate hex IP addresses to dotted-decimal (relatively quick)
# -h -- translate hex IP addresses to DNS names (somewhat slower - DNS lookups)
# -u user -- filter for the named user before translating addresses
  
hextodec()
{
# convert the IP address in reverse-hex to dotted-decimal
echo $((0x${1:6:2})).$((0x${1:4:2})).$((0x${1:2:2})).$((0x${1:0:2}))
}
  
hostlookup()
{
# Convert a decimal IP to hostname - calls 'host' each time
hostname=$(host $1)
case $hostname in
* not found*)
# Just echo the address we tried to look up
echo "$1"
;;
*)
# The result is word 5. Lower-case it for consistency
set $hostname
echo "$5" | tr 'A-Z' 'a-z'
;;
esac
}

# Options
iptranslate=0
gethostnames=0
filter=cat
while getopts ihu: o ; do
case $o in
i) iptranslate=1 ;;
h) gethostnames=1; iptranslate=1 ;;
u) filter="grep $OPTARG" ;;
esac
done
shift $(($OPTIND-1))

# We could get the DB server names from 'fs checkservers', but it isn't obvious what is from our cell. We
# could also grep CellServDB. I cop out and hard code one known DB server and get the others from it.
masterserver=halley.dartmouth.edu
serverlist=$(bos listhosts -server $masterserver| grep 'Host .* is ' | awk '{print $4}')

# If we want to filter usernames, it is more efficient to do it inline, before sorting, translation and hostname lookups

# Array to hold IP address/name conversions (associative array, ksh only)
# ksh - use -A for associative array. bash - use -a and numeric array
typeset -a hostnames

(
for dbserver in $serverlist; do
bos getlog -server $dbserver -file /usr/afs/logs/AuthLog
done
) | grep -v 'Fetching log file' | $filter | sed -e 's/^... //' -e 's/ ([1-9]) / 0 /' | sort --month-sort |
sed '-e s/ ([0-9a-f][0-9a-f][0-9a-f][0-9a-f][0-9a-f][0-9a-f][0-9a-f])$/ 0/' |
while read line; do
if [[ $iptranslate == 1 ]] ; then
# Ugly!
# Sometimes we get a 7-digit hex code in the log - the kaserver apparently drops leading zeros.
# The second 'sed' in the pipe catches these are fixes them.
case $line in
* from [0-9a-f][0-9a-f][0-9a-f][0-9a-f][0-9a-f][0-9a-f][0-9a-f][0-9a-f])
# translate the reverse-hex address
iphex=${line##* from }
# bash version - index by numeric value only, but can be sparse array -- use the raw IP
ipdec=$((0x$iphex))
frontpart=${line% from *}
if [[ $gethostnames == 1 ]]; then
# ksh - index on hex value as a string (iphex)
# bash - index on numeric value (ipdec)
index=$ipdec
if [[ -z "${hostnames[$index]}" ]]; then
hostnames[$index]="$(hostlookup $(hextodec $iphex))"
fi
echo "$frontpart from ${hostnames[$index]}"
else
echo "$frontpart from $(hextodec $iphex)"
fi
;;
*)
echo "$line"
;;
esac
else
# No ip translation, just echo the whole line
echo "$line"
fi   
done

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote