Skip to content

Instantly share code, notes, and snippets.

@samdoran
Created April 4, 2013 15:34
Show Gist options
  • Save samdoran/5311444 to your computer and use it in GitHub Desktop.
Save samdoran/5311444 to your computer and use it in GitHub Desktop.
Change root password on many machines via SSH
#!/bin/bash
#
# SSH into hosts listed in /root/Documents/machines and change root password
# Create list of hosts not online in /tmp/offline-hosts
# Assumes direct root login is available on remote machines
# Works bes if SSH keys are setup to allow passwordless login of root
# --- Variables --- #
HOST_LIST=$(cat /root/Documents/machines)
# Make sure to clear this out after running. DO NOT store the root password in clear text!
# I have had problems wih certain symbols in the password. You may need to avoid certain characters like {, }, $, ", and '
ROOT_PASSWORD=
# --- Main Program --- #
# Make sure the root password is set before running
if [ "${ROOT_PASSWORD}" == "" ] ; then
echo -e "\nYou forgot to enter a value for ROOT_PASSWORD\n"
exit 1
fi
# Cleanup temp files if they exist
if [ -f /tmp/offline-hosts ] ; then rm -f /tmp/offline-hosts ; fi
if [ -f /tmp/root-password-changed] ; then rm -f /tmp/root-password-changed ; fi
for HOST in ${HOST_LIST} ; do
clear
echo "Checking to see if ${HOST} is online..."
ping -q -c 3 ${HOST} 2>&1 > /dev/null
if [ ${?} = 0 ] ; then
/usr/bin/ssh $HOST \
"echo "${ROOT_PASSWORD}" | \
/usr/bin/passwd --stdin root"
echo ${HOST} >> /tmp/root-password-changed
else
echo "${HOST} is offline. Continuing on."
echo ${HOST} >> /tmp/offline-hosts
sleep 1
fi
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment