ciphermethod.com

changelin.sh

by on Jul.12, 2011, under Scripts

#!/bin/sh
# Change user password across a list of servers.
# 20100421 – Created by Jamey Hopkins
# 20110712 – Check if login exists before changing password
#            Exit if login or password is blank
#
# note:  be sure account.info gets removed from remote server if script is aborted
#

echo
echo “Change Users Linux/AIX Password”
echo

if [ “$1” = “” ]
then
  echo “Need server list.”
  echo “example: $0 servers.txt”
  echo
  exit
fi

if [ ! -f $1 ]
then
  echo “Server list $1 not found.”
  echo
  exit
fi

# list seems good, accept it
LIST=$1

echo -n “Enter users login: ”
read LOGIN

if [ “$LOGIN” = “” ]
then
  echo “user account can not be blank”
  echo
  exit
fi

echo “Enter new password”
echo -n “Password: ”
stty -echo
read PASS1
stty echo
echo
echo “Enter password again”
echo -n “Password: ”
stty -echo
read PASS2
stty echo

echo

if [ “$PASS1” != “$PASS2” ]
then
  echo “password mismatch”
  echo
  exit
fi

if [ “$PASS1” = “” ]
then
  echo “passwords can not be blank”
  echo
  exit
fi
echo “$LOGIN:$PASS1” >account.info

echo
# let’s roll
for X in `cat $LIST`
do
  echo “$X: setting new password for $LOGIN”
  VALID=`ssh $X “grep ^$LOGIN: /etc/passwd | cut -f1 -d:”`
  if [ $VALID ]
  then
     scp ./account.info $X: >/dev/null
     ssh $X “cat account.info | sudo /usr/sbin/chpasswd” >/dev/null
     # clean up password file (be sure to manually remove if script gets aborted)
     ssh $X rm account.info
  else
     echo “Account doesn’t exist.  Skipping….”
  fi
done

# clean up password file
rm account.info

echo

No comments for this entry yet...

Leave a Reply

You must be logged in to post a comment.

Looking for something?

Use the form below to search the site:

Still not finding what you're looking for? Drop a comment on a post or contact us so we can take care of it!

But Wait, There's More!

A few highly recommended friends...