Had to do a quick crontab job at work to grab user details out of /etc/passwd and email the details in a report, here is the resulting script

> account-report.txt
date > account-report.txt
echo -e "=== Account Report ===n" >> account-report.txt
hName=( $(awk '{print $1}' $Nlist) );
for name in "${hName[@]}"; do

        users=$(cat <<EOL
                cat /etc/passwd | grep "/bin/bash" | grep "[1-9][0-9][0-9]" | cut -d: --fields=1,5

        echo "Host: $name" >> account-report.txt
        ssh -o StrictHostKeyChecking=no ${name} "$users" >> account-report.txt
        echo -e "End of $name accountsnn" >> account-report.txt

mailx -s "Monthly Account Report" user@host.tld < account-report.txt

It grabs a list of hostnames from nodelist.txt and utilizes a ssh key-trust to connect to the hosts without supplying a password.  We lucked out bigtime that there was a trust already setup otherwise the whole process would have been overly complicated requesting permission to setup a trust in the first place.

Leave a Reply

Your email address will not be published.

Close Bitnami banner