Search code examples
linuxocelot

compare the expiration date in /etc/shadow to today's date and figure out which accounts have expired


root@node033:~# vi exppass
root@node033:~# bash exppass
exppass: line 7: syntax error: unexpected end of file
root@node033:~# cat exppass
cat /etc/shadow |
while IFS=":" read col1 col2 col3 col4 col5 col6 col8;
do
echo $col3 $col8 
if [ $expire -lt $today ];
then

I am trying to remove the expiration passwd using to compare the date and time. adjust your script to do something along the lines:

if [ $expire -lt $today ]; then        
#delete the password

I think $expire above is equivalent to one of the columns you're reading. and you can get today's date by doing something like today=$(date +%s)


Solution

  • If you're happy w/ an awk & bash solution rather than doing the hard lifting in a loop:

    awk -F: -v today=$(( $( date "+%s" ) / 86400 ))  '$8!=""{print $1, today-$8}' /etc/shadow
    

    Explanation: -F: defines the input field separator to be a colon.

    -v today=$(( $( date "+%s" ) / 86400 )) expresses today's date in days since epoch (which is the format used in /etc/shadow) and assigns it to an awk variable called today.

    Now for the awk logic:

    $8!="" if the 8th field (Account Expiry) isn't unset, {print $1, today-$8} print the username and the difference between today and the expiry date in days. If the date lies in the past, you get a positive value, if it's in the future, a negative one.

    Edit:

    Looking at the jumble above it appears that you're trying to check for both password and account expiry:

    This should do:

    awk -F: -v today=$(( $( date "+%s" ) / 86400 ))  '$3!="" || $8!=""{printf "%s\tpw: %s\tacc: %s\n", $1,today - $3, today-$8}' /etc/shadow
    

    The 2nd field now shows the age of the password, the 3rd the account expiration. If this still isn't what you're after you'll need to sit down and rephrase your question so it reflects what you're actually after.