0

I have around 100 servers on which I want to check if a user exists. Instead of going manually on each and every server, I am trying to write a script to do that. So, far I have written the following script to verify it.

#!/bin/bash
if id user1 >/dev/null 2>&1; then
        echo "user1 user exists"
else
        echo "user1 user does not exist"
fi

I believe I need to do ssh also into the servers using the script but please suggest if there is a better way to perform it.

meallhour
  • 13,921
  • 21
  • 60
  • 117
  • Are those 100 servers Unix? or mixed? (Windows, Unix.. etc) – Fadi Oct 07 '16 at 16:18
  • Also, not sure if there's a secure way to this without SSH.. I guess you could expose an API on those servers then have a script that can check if a user is there or not. If you want to do it over SSH, then if there's no password you could loop through all the servers that you have and do something like this: http://stackoverflow.com/a/14811956/4557537 – Fadi Oct 07 '16 at 16:19
  • 1
    Can you ssh to each host without entering a password every time? If so, just wrap this in a for loop and use `if ssh "$host" id user1 ..` – that other guy Oct 07 '16 at 16:22
  • all the servers are RHEL7 servers. and all the ids have passwords for them. I cannot ssh without a password – meallhour Oct 07 '16 at 16:41

1 Answers1

0

"you have" implies you're an administrator, so can use the root account with a public key... just ensure you keep the private key secure and enable ssh access without a password.

Otherwise, safer to have your own account on all machines and ssh into each one and run that id command @that-other-guy mentioned.

It doesn't matter that a password is required, if you have your ssh public key in your authorized_keys file on every host. Then on your machine, your ssh-agent maintains the private key, so that you only have to enter your passphrase once, and off you go.

strobelight
  • 267
  • 2
  • 7