0
echo here is a list of the users who have not logged in for a month
olduser="who | grep | -u, -q +30"
if $olduser
then
 echo these users havent been logged in for thirty 30 days
else
 echo user is active
;;
  • 2
    Firstly, you are using command which is designed to list logged users. I find it not useful in this scenario. More, is your command line lost during writing a question? It is not a valid not mentioning of providing some useful data. Please, edit your question, use code formatting and try to be more specific about what you tried already. – Kalavan Nov 30 '16 at 15:22
  • Use accounting, not who. – Satō Katsura Nov 30 '16 at 15:29
1

One recommendation... edit the question with the following, to allow a more thorough/relevant answer:

  • Your OS (i.e. Solaris is very different than RHEL in acct mgmt)
  • Type of script/code (Bash, csh, ksh, etc) as it looks like this is just part of a script and not the whole thing, since we can't see the shebang (#!/bin/bash, etc..).

First thing I do notice is that your variable $olduser is not set to command substitution but to the literal string "who | grep | -u, -q +30"

Command substitution is generally done with $(command_to_be_run) or with backticks like `command_to_be_run`. And I don't think you are looking for who but rather should be looking, as mentioned above, at accounting commands like logins or last or even log files.

Something like a for-loop to gen a list of users, and from there using that last and your variable substitution to generate a last logon date for each invidivual so you can perform an evaluation statement like a if-then-fi if they are older than 35 days.

e.g. On Solaris 10, with Bash

LAST_LOGON_DATE=$(last $user | awk 'NR==1{print $5,$6}' | custom_date_convert_cmd )

if [[ ${LAST_LOGON_DATE} -ge 35 ]]; then
    echo "${user} is expired. Last logon: ${LAST_LOGON_DATE} days ago"
    important_commands_here
fi

Obviously sed/perl would probably be a better solution as far as speed of processing over AWK, but if you are in a fix and just need something working, that's probably a good place to start.

Look up your system STIG from DISA, as usually they include commands to do exactly this... check users last logins, users w/o passwds, etc...
https://www.stigviewer.com/stigs

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.