Skip to content

Instantly share code, notes, and snippets.

@ksaitor
Created April 19, 2014 17:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ksaitor/11091519 to your computer and use it in GitHub Desktop.
Save ksaitor/11091519 to your computer and use it in GitHub Desktop.
during my unix class i developed this script. now i thought it might be useful to someone, and decided to post it here. it might need some additional editing to match ur system. enjoy
#!/bin/sh
# DESCRIPTION
#User Information gathering script developed by Raman Ksaitor ( raman.ksaitor at gmail.com)
#The script is designed primaraly for system administrators (and other root privelaged). The purpose is to gather most of system user information and save it in privatized files. Emphases is also put on easy and quick access to scripts parameters (by elemenating "choose forms", useless decorations, and by allowing pass parameters with a command line all-at-once).
#
# FUNCTIONALITY
#Script allows to deal with one single user (example with user "root": $ ./ui.sh -u root -niShfbsoH) or with a list of users (example with list of users "userlist": $ ./ui.sh -l "userlist -niShfbsoH")
#informational options:
# -n displays network and general information about user(s). Includes full login name, short name, home directory, shell, wether user is currently online or not, online idle time, current ip adress, first and last known login date, total online time.
# -i displays used ip addresses, dates and time
# -S security logs
# -h occupied hard drive space by user home directory
# -f total nmber of files+folders in user home directory
# -b displays path and size of the biggersr file in user home directory
# -s displays path and size of the smallest file in user home directory
# -o displays path and size of the oldest file in user home directory
# -H copies user bash history to ur execution folder. Will be named as <USERNAME>.bash_history
#run options:
# -a allows to select all information at once, while in single user mode (e.g. for user "root": $./ui.sh -a root)
# -u runs script for one username and should be followed a set of informational options -niShfbsoH.
# -l runs script with a list of users
#all selected information is outputed to screen as well as to file named <USERNAME>.info
#after execution all created files with user information are privatized with read and write permissions for owner only
#
# MULTIPLE USER MODE
#Multiple user mode is emplimented by assigning usernames in a given file to field of an array, then script sequantialy runs itself in a single user mode with passed username (from array)
#
# NOTES:
#If script is run without root privileges it for each informational option it will display a note about absence of root rights and will not execute option. Exept network statistics (-n).
#Due to the fact that getopts does not allow multiple arguments to be passed to one option at once, in case of -l option the script should be run with name of the file and options in double quates ( $ ./ui.sh -l "userlist -niShfbsoH") . This way two space-separated arguments would be passed to the option where they are devided into two separate arguments. Use of -a option(for quick selecting of all informational options) can not be used while usernames are passed through a file (-l option).
#If script is executed with no or incorrect options message will be shown with instructions of a proper script usage.
#If script is executed with nonexistent username notification will be displayed and the script or a subprocess(while in multiple user mode) will be terminated.
#Additional functionality might be added in future
# FIRST LOGIN + LAST LOGIN + TOTAL ONLINE TIME
function netlog {
finger $uid
echo
echo "First login: `last | grep "$uid" | tail -1 | awk '{print $4,$5,$6,"\t",$7}'` "
echo "Last login: `last | grep "$uid" | head -1 | awk '{print $4,$5,$6,"\t",$7}'` "
last | grep -i "$uid" | awk '{print $10}' | tr -d "(-)" | awk '
BEGIN{FS=":"}
{min+=$1
sec+=$2}
END{
min+=(sec-(sec%60))/60
sec%=60
print "Total online time: "min":"sec}'
echo
}
# USED IP ADDRESSES
function ipaddresses {
echo "Used IP addresses:"
last | grep -i "$uid" | awk '{print $3,"\t",$4,$5,$6,"\t",$7,$8,$9,$10}'
echo
}
# SECURITY LOG
function secure {
if [ "$(id -u)" != "0" ];
then echo "-$opt No root rights for this action"
else echo "Security logs:";
uid=`echo $uid | tr "[:upper:]" "[:lower:]"`;
cat /var/log/secure | grep "$uid";
uid=`echo $uid | tr "[:lower:]" "[:upper:]"`;
echo;
exit 1
fi
}
# USED HD SPACE
function usedHD {
if [ "$(id -u)" != "0" ];
then echo "-$opt No root rights for this action"
else ls -AlR | egrep "[-]......... " | awk '{size+=$5}END{print "Home directory size: "size" bytes"}';
echo;
exit 1
fi
}
# TOTAL FILES IN DIRECTORY
function nFiles {
if [ "$(id -u)" != "0" ];
then echo "-$opt No root rights for this action"
else echo "Total folders and files: `find | wc -l | awk '{print $1-1}'`";
echo;
exit 1
fi
}
# THE BIGGEST FILE
function biggestFile() {
if [ "$(id -u)" != "0" ];
then echo "-$opt No root rights for this action"
else echo "The biggest file:";
find . -follow -mount -type f -print | xargs ls -l | sort -r -n -k 5,5 | head -1 | tr -s " " | cut -d" " -f5,9 | awk '{print " ",$1,"Bytes\n", $2}';
echo;
exit 1
fi
}
# THE SMALLEST FILE
function smallestFile() {
if [ "$(id -u)" != "0" ];
then echo "-$opt No root rights for this action"
else echo "The smallest file:";
find . -follow -mount -type f -print | xargs ls -l | sort -r -n -k 5,5 | tail -1 | tr -s " " | cut -d" " -f5,9 | awk '{print " ",$1,"Bytes\n", $2}';
echo;
exit 1
fi
}
# HAVE NOT BEEN ACCESSED THIS MONTH
function oldFiles() {
if [ "$(id -u)" != "0" ];
then echo "-$opt No root rights for this action"
else echo "Files that have not beed access within 30 days:";
find . -follow -mount -type f -mtime +30 | xargs ls -l | sort -m | tr -s " " | awk '{print $6,$7,"\t",$8,$9}';
echo;
exit 1
fi
}
# SHOW USER NAME
function username {
echo "${bold}INFORMATION OF:" $uid ${offbold}
true=`finger $uid | wc -l`
if [ $true = 0 ]
then kill $$
else udir=`finger $uid | grep "Directory"`; udir=`echo $udir | cut -d" " -f2`; cd $udir
fi
}
bold=`tput bold`
offbold=`tput sgr0`
#clear
#CHEKING IF THERE IS ANY OPTIONS PASSED TO THE SCRIPT
if [ $# = 0 ]
then
echo "${bold}Usage:${offbold} -u <USER> -niShfbsoH ${bold}OR${offbold} -l <"FILE_WITH_USERS -nihfbsoH"> ${bold}OR${offbold} -a <USER>
-u information about a single user | single user mode
-l information about a list of users
-a all information in single user mode
-n netlog
-i used ip addresses
-S security logs
-h used hard drive space
-f number of files and folders in a home directory
-b biggest file
-s smallest file
-o oldest file
-H coping .bash_history"
exit $E_OPTERROR
fi
while getopts "l:a:u:nSihfbsoH" opt; do
case $opt in
l) userlist=`echo $OPTARG | cut -d" " -f1`; # l) WORKING WITH LISTS
echo $userlist;
users=(`cat "$userlist"`);
i=0;
echo $OPTARG | cut -d" " -f2
while [ $i -lt ${#users[@]} ]; do
sh ui.sh -u ${users[i]} "`echo $OPTARG | cut -d" " -f2`"
let i+=1
done;
exit;;
u) uid=$OPTARG; # u) PREPARING AND SHOWING USERNAME
uid=`echo $uid | tr "[:lower:]" "[:upper:]"`;
out=`pwd`/"$uid".info;
rundir=`pwd`; #MEMORIZING RUNDIR OF UI.SH
echo "${bold}INFORMATION OF:" $uid ${offbold}
true=`finger $uid | wc -l`
if [ $true = 0 ]
then kill $$
else udir=`finger $uid | grep "Directory"`; udir=`echo $udir | cut -d" " -f2`; cd $udir
fi
username > $out;;
a) uid=$OPTARG; # a) TO USE ALL OPTIONS AT ONCE
uid=`echo $uid | tr "[:lower:]" "[:upper:]"`;
out=`pwd`/"$uid".info;
rundir=`pwd`;
echo "${bold}INFORMATION OF:" $uid ${offbold}
true=`finger $uid | wc -l`
if [ $true = 0 ]
then kill $$
else udir=`finger $uid | grep "Directory"`; udir=`echo $udir | cut -d" " -f2`; cd $udir
fi
username > $out;
netlog | tee -a $out;
ipaddresses | tee -a $out;
secure | tee -a $out;
usedHD | tee -a $out;
nFiles | tee -a $out;
biggestFile | tee -a $out;
smallestFile | tee -a $out;
oldFiles | tee -a $out;
cp $udir/.bash_history $rundir/$uid.bash_history;;
n) netlog | tee -a $out;;
i) ipaddresses | tee -a $out;;
S) secure | tee -a $out;;
h) usedHD | tee -a $out;;
f) nFiles | tee -a $out;;
b) biggestFile | tee -a $out;;
s) smallestFile | tee -a $out;;
o) oldFiles | tee -a $out;;
H) if [ "$(id -u)" != "0" ];
then echo "-$opt No root rights for this action"
else cp $udir/.bash_history $rundir/$uid.bash_history
exit 1;
fi;;
?) echo "${bold}Usage:${offbold} -u <USER> -niShfbsoH ${bold}OR${offbold} -l <"FILE_WITH_USERS -niShfbsoH"> ${bold}OR${offbold} -a <USER>
-u information about a single user | single user mode
-l information about a list of users
-a all information in single user mode
-n netlog
-i used ip addresses
-S security logs
-h used hard drive space
-f number of files and folders in a home directory
-b biggest file
-s smallest file
-o oldest file
-H coping .bash_history"
exit -1;;
esac
done
# MAKING REPORTS PRIVATE
if [ -a $rundir/$uid.info ]
then
chmod 600 $rundir/$uid*
fi
# TO DO
# MAIL server is probably off... can't send work any mail
# maybe SHOW USER LIST, allow to choose...
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment