Posts Tagged: ‘bash’

Changing password on multiple in an automated way using a bash script

August 31, 2012 Posted by admin

I was recently tasked with changing the root password on a hundred servers.

This action need to be done quickly and in an automated fashion.

As I was trying to figure out how to do this I remembered we had an ssh script user

that had sudo access. So I put together a text file consisting of all the ip’s

root@triplehelix ~$ for i in {1..100}; do echo "10.38.251.$i" >>servers.txt; done
root@triplehelix ~$ cat servers.txt
10.38.251.1
10.38.251.2
10.38.251.3
10.38.251.4
10.38.251.5
10.38.251.6
10.38.251.7
10.38.251.8

 

Next I created a script that I could run from the command line using my severs.txt file as input.

 

root@triplehelix ~$ cat changepw
#!/bin/bash

for server in `cat servers.txt`; do
ssh -i ~/.ssh/id_rsa $server -l scriptuser ‘sudo passwd root< <EOF
newpassword
newpassword
EOF’;
done