Computing.Net > Forums > Linux > script problem

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

script problem

Reply to Message Icon

Name: suse
Date: April 21, 2003 at 18:01:33 Pacific
OS: slackware8.0
CPU/Ram: PIV
Comment:

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/sh

if [ $# -ne 1 ]; then

cat /etc/passwd | while read line
do
if [ echo `awk -F: '{print $3}' $line` > 500 ]; then

username = 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
fi

if [ echo `awk -F: '{print $5}' temp` ne 5 ]; then
passwd -w5 $username
fi

if [ grep -v '\/[0-9][0-9]\/[0-9][0-9]\/2003' `echo `awk $2 temp`` ]; then
echo $line
fi
fi
done

else

cat /etc/passwd | while read line
do
if [ echo `awk -F: '{print $3}' $line` >= 500 ]; then

username = 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
fi

if [ echo `awk -F: '{print $5}' temp` ne 5 ]; then
passwd -w5 $username
fi

if [ 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
fi

if [ $1 = p ]; then
passwd -x1 -n1 $username
fi

if [ $1 = f ]
passwd -f $username
fi

fi
fi
done

fi

exit 0



Sponsored Link
Ads by Google

Response Number 1
Name: David Perry
Date: April 22, 2003 at 05:59:32 Pacific
Reply:

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 ] ; then

Your 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
fi

ID=`id | cut -f 2 -d '=' | sed -e 's/(.*//'`
if [ $ID -ne 0 ] ; then
echo "This script needs to be run as root"
exit 4
fi

usercheck () {
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


0

Response Number 2
Name: David Perry
Date: April 22, 2003 at 07:08:49 Pacific
Reply:

I made an error by ommitting the IFS declaration. Please include the line

IFS=":" ; export IFS

right after the shadow_line=`... line


0

Sponsored Link
Ads by Google
Reply to Message Icon

Related Posts

See More


Printing trouble Writing to syslog



Post Locked

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


Go to Linux Forum Home


Sponsored links

Ads by Google


Results for: script problem

Linux scripting problem www.computing.net/answers/linux/linux-scripting-problem/23493.html

WinLinux 2003 script problems www.computing.net/answers/linux/winlinux-2003-script-problems/23994.html

ftp script problems www.computing.net/answers/linux/ftp-script-problems/24683.html