0

This question is an exact duplicate of:

I have below script which creates user list and also filters ignore.txt file to ignore matching usernames. I would like to run this script on multiple hosts like hostA, hostB, hostC. Like if I run on one server it should run on other 4 servers and get the output and mail the output.

#!/bin/sh
(
echo List of users in PRD 
echo
date
echo
grep -wvf ignore.txt /etc/passwd | awk -F: -v OFS=: '{print $1,$3,$5}' | sort 
) | tee "$(hostname)"_userlogins.txt |
mailx -s file xxxxxx@unix.com

marked as duplicate by roaima, Anthon, jasonwryan, slm Apr 24 '15 at 23:28

This question was marked as an exact duplicate of an existing question.

  • 2
    This isn't a very good way to manage users on your servers. – jordanm Apr 24 '15 at 4:46
  • @jordanm your suggestion is to run this individually on each server like setting cron job to run each month.It will be like 4 mails sent to recipient.This is for reports for auditing purpose to make sure we donot have unwanted users in the list – Zaira Zareena Apr 24 '15 at 4:50
  • 2
    No, my suggestion would be to use some kind of centralized authentication, such as kerberos (freeIPA, active directory, etc). That way there is only a single host to audit. – jordanm Apr 24 '15 at 4:52
2

Try to use pdsh. A lot of examples available on Project Page

If you like to use simple bash script:

#!/bin/bash
HOSTS="host1 host2 host3"
USER=root
CMD="ls"
for host in $HOST; do
  ssh ${USER}:{$host} "$CMD"
done

In all cases you would need to tune no-password auth using keys and append to ssh command: -i /path/to/key

Example to use:

#!/bin/bash
HOSTS="host1 host2"
N=1
FILE=report.txt
echo "List of users in PRD" > $FILE
date >> $FILE

for host in $HOSTS; do
  ssh $host -l root cat /etc/passwd|cut -d ':' -f 1 >/tmp/temp 2>/dev/null
  echo "====host $host====" >> $FILE
  cat /tmp/temp | tail -n +$N |grep -wvf ignore.txt|sort >> $FILE
done

rm -f /tmp/temp

#mailx -s $FILE xxx@unix.com
  • @resishin will this be secure no-password auth using keys? – Zaira Zareena Apr 24 '15 at 13:17
  • you could look here for the answer: here – Reishin Apr 24 '15 at 14:00
  • Additionally here. More topics about ssh and passing password as argument, and why this is bad think: here – Reishin Apr 24 '15 at 14:09
  • #!/bin/bash HOSTS=" 10.xx.xx.xxx" FILE=report.txt echo "List of users in PRD" > $FILE date >> $FILE for host in $HOSTS; do ssh $host -l zaira cat /etc/passwd|cut -d ':' -f 1 >/tmp/temp echo "====host $host====" >> $FILE cat /tmp/temp |grep -wvf ignore.txt|sort >> $FILE done rm -f /tmp/temp #mailx -s $FILE xxxxx@unix.com – Zaira Zareena Apr 24 '15 at 16:38
  • 1
    i think we should move to chat, since you have less than 20 point i googled random chat room us21.chatzy.com/69026314701767 – Reishin Apr 24 '15 at 20:47

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