Script to change password in UNIX

Yes below mentioned is working

 printf "%s\n" "oldpass" "newpass" "newpass" | (passwd rsaini) 

Enter existing login password:

i am not able to fetch 17-June password from below command

grep -e"``date +"%d`-1-date +%b"`" 

Yes, you will need to use perl for that:

YESTERDAY=`perl -e "use POSIX; print strftime('%d-%b',localtime(time()-86400))"`
OLDPASS=`grep "$YESTERDAY" file.txt | cut -c8-`

i think can we get yesterday password from file without using perl...i dont know bout perl

can u please advise below is one of the example

| grep -e "\($(($(date +%d)-0))\|$(($(date +%d)-1))\|$(($(date +%d)-2))\) $(date +%b)"

Perl should be installed by default, so I don't think you will need to setup anything.

If you want to process yesterday manually you also need something for 1st/2nd/3rd of month and 1st/2nd/3rd Jan - Dont forget about leap years as well.

I have seen some solutions that try and change the timezone and trick date in displaying yesterdays date but they are a little prone to issues around different timezones and daylight savings and I wouldn't suggest them, below you would adjust the +20 for your actual timezone, again watch out for daylight savings changes and running this early in the morning or late at night:

TZ=GMT+20 date +%d-%b

Hey Chubler,

i have compiled the program but it is manually asking for the input of passwords it should automatically take from file.txt
output

sh test.sh
Todays password is: OLiver@123#
Enter existing login password:

#!/bin/sh
CWD=/home/raj/TEST
cd $CWD
rm outpp.log
DT=`date +"%d-%b"`
PASS=`grep "$DT" file.txt | cut -c8-`
YESTERDAY=`perl -e "use POSIX; print strftime('%d-%b',localtime(time()-86400))"`
OLDPASS=`grep "$YESTERDAY" file.txt | cut -c8-`
if [ -n "$PASS" ]
then
  echo "Todays password is: $PASS"
  printf "%s\n" "$OLDPASS" "$PASS" "$PASS" | (passwd raj) > /home/raj/TEST/outpp.log
else
  echo "No password for $DT"
fi
if egrep -iq '(Permission)|(successfully)' /home/raj/TEST/outpp.log
then
   MAILTO="raj@abc.com"
   CONTENT="/home/raj/TEST/outpp.log"
   (
        echo "Subject: Password Change Status "
        echo "MIME-Version: 1.0"
        echo "Content-Type: text/html"
        echo "Content-Disposition: inline"
        cat $CONTENT
   ) | /usr/sbin/sendmail -t $MAILTO
else
  echo "bbb"
fi

Back in post #21 you said that this:

printf "%s\n" "oldpass" "newpass" "newpass" | (passwd rsaini)

worked from the command line so we know sending an oldpassword into passwd works OK.

I suspect the $YESTERDAY value is blank

put this debug in: echo "Yesterdays ($YESTERDAY) password is: $OLDPASS" just to confirm the perl+grep is getting the correct date and password.

i have given only below line in command line and it was working

printf "%s\n" "oldpass" "newpass" "newpass" | (passwd rsaini)

here is the output

sh test.sh
Yesterdays (17-Jun) password is: oss123!
Todays password is: OLiver@123#
Enter existing login password:

added the line in the code

---------------
---
DT=`date +"%d-%b"`
PASS=`grep "$DT" file.txt | cut -c8-`
YESTERDAY=`perl -e "use POSIX; print strftime('%d-%b',localtime(time()-86400))"`
OLDPASS=`grep "$YESTERDAY" rnc.txt | cut -c8-`
echo "Yesterdays ($YESTERDAY) password is: $OLDPASS"
if [ -n "$PASS" ]
then
--
---

I think you may be stuck without root access, as mentioned before expect can do this sort of thing but it's unlikely to be installed on your system.

It could be time to investigate alternate methods to deal with this. Perhaps a prompt for the daily password from a script run by the users profile? Do they just login via telnet/ssh or are they using ftp/web/other interfaces?

login is done via ssh, i am getting below output from the code is there any way to input the fields automatically?

sh test.sh
Yesterdays (17-Jun) password is: oss123!
Todays password is: OLiver@123#
Enter existing login password: