Tom's Guide | Tom's Hardware | Tom's Games
![]() |
![]() |
![]() |
Hi;
im a newbie for linux. i have a home work which i wrote as bellow. anybody can help me to correct what is wrong. im appreciated.the question is:
write a script to check users' id and ensure they have:
1. specific min days to change password (7)
2. specific max days to change password (30)
3. specific warning days (5)
4. ensure the "last changed" setting for each user occured in the past and report any that do not match this (include the date set in the shadow file in the report)
if -o option is passed as a command line, interactively prompt the user do:
1. disabling accounts
2. force change a users password
3. force a user to change password at next login.
#!/bin/shif [ $# -ne 1 ]; then
cat /etc/passwd | while read line
do
if [ echo `awk -F: '{print $3}' $line` > 500 ]; thenusername = echo `awk -F: '{print $1}' $line`
temp = echo `passwd -S $username`if [ echo `awk -F: '{print $3}' temp` ne 7 ]; then
passwd -n7 $username
fi
if [ echo `awk -F: '{print $4}' temp` ne 30 ]; then
passwd -x30 $username
fiif [ echo `awk -F: '{print $5}' temp` ne 5 ]; then
passwd -w5 $username
fiif [ grep -v '\/[0-9][0-9]\/[0-9][0-9]\/2003' `echo `awk $2 temp`` ]; then
echo $line
fi
fi
doneelse
cat /etc/passwd | while read line
do
if [ echo `awk -F: '{print $3}' $line` >= 500 ]; thenusername = echo `awk -F: '{print $1}' $line`
temp = `passwd -S $username`if [ echo `awk -F: '{print $3}' temp` ne 7 ]; then
passwd -n7 $username
fi
if [ echo `awk -F: '{print $4}' temp` ne 30 ]; then
passwd -x30 $username
fiif [ echo `awk -F: '{print $5}' temp` ne 5 ]; then
passwd -w5 $username
fiif [ grep -v '^\/[0-9][0-9]\/[0-9][0-9]\/2003' `echo `awk $2 temp`` ]; then
echo $line
fi
if [ $1 = -o ]; then
echo 'Enter the command as bellow:'
echo 'l -- for disable the password'
echo 'p -- force change a users password'
echo 'f -- force change his/her passwd at next login'
if [ $1 = l ]; then
passwd -l $username
fiif [ $1 = p ]; then
passwd -x1 -n1 $username
fiif [ $1 = f ]
passwd -f $username
fifi
fi
donefi
exit 0

First some notes.
You cannot have spaces around the '=' in a variable assignment.
For numerical comparisons, you should use the -gt syntax as in
if [ $var -gt 500 ] ; thenYour interactive section needed a 'read' and a way to process the user input.
I hope the following is a start. You probably want to modify it to actually do something when the values from /etc/shadow are outside of specs.
#!/bin/sh
if [ $# -eq 2 ]; then
username=$2
elif [ $# -eq 1 ] ; then
username=$1
else
echo "Usage $0 [-o] username"
exit 4
fiID=`id | cut -f 2 -d '=' | sed -e 's/(.*//'`
if [ $ID -ne 0 ] ; then
echo "This script needs to be run as root"
exit 4
fiusercheck () {
if [ `grep -c "^$username:" /etc/passwd` -eq 0 ] ; then
echo "Invalid username $username"
exit 4
fi
}if [ "$1" = "-o" ] ; then
username=$2
usercheck
echo 'Enter the command as bellow:'
echo 'l -- for disable the password'
echo 'p -- force change a users password'
echo 'f -- force change his/her passwd at next login'
echo 'q -- exit interactive password management'
while [ "$ans" != "q" ] ; do
read ans
ans=`echo $ans | cut -c 1 | tr '[A-Z]' '[a-z]'`
if [ "$ans" = "l" ] ; then
passwd -l $username
elif [ "$ans" = "p" ] ; then
passwd -x1 -n1 $username
elif [ "$ans" = "f" ] ; then
passwd -f $username
elif [ "$ans" = "q" ] ; then
echo "Exiting now"
else
echo "Invalid input. Please try again"
fi
done
else
username=$1
usercheck
shadow_line=`grep "^$username:" /etc/shadow`
echo "###$shadow_line###"
passwd=`echo $shadow_line | awk '{ print $2 }'`
lastchg=`echo $shadow_line | awk '{ print $3 }'`
min=`echo $shadow_line | awk '{ print $4 }'`
max=`echo $shadow_line | awk '{ print $5 }'`
warn=`echo $shadow_line | awk '{ print $6 }'`
inactive=`echo $shadow_line | awk '{ print $7 }'`
expire=`echo $shadow_line | awk '{ print $8 }'`
flag=`echo $shadow_line | awk '{ print $9 }'`
echo "Userid $passwd $lastchg $min $max $warn $inactive $expire $flag"
if [ -z "$min" ] ; then
echo "WARNING 'min' unset for $username"
elif [ $min -ne 7 ] ; then
echo "WARNING minimum days between password changes is not equal to 7"
fi
if [ -z "$max" ] ; then
echo "WARNING 'max' unset for $username"
elif [ $max -ne 30 ] ; then
echo "WARNING valid password duration is not equal to 30"
fi
if [ -z "$warn" ] ; then
echo "WARNING 'warn' unset for $username"
elif [ $warn -ne 5 ] ; then
echo "WARNING warning days is not equal to 5"
fi
echo "Last changed date '$lastchg'"
fi

I made an error by ommitting the IFS declaration. Please include the line
IFS=":" ; export IFS
right after the shadow_line=`... line

![]() |
Printing trouble
|
Writing to syslog
|

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.
| Ads by Google |