Issue
I'm new to Linux (and to Stack overflow) so I may be making a dumb mistake, but I made a script to add one same user with one same password to many servers :
#!/bin/bash
password=`cat /root/scripts/password`
for i in `cat /root/scripts/LIST_TEST.txt`
do
printf "Serveur : $i \n"
ssh -tt -o PasswordAuthentication=no $i
adduser newuser
yes `echo $password` | passwd newuser
exit 0
done
Also I'm in root when using this script, it seems that the user is created but the password doesn't get changed, as I cannot login when I try ssh newuser@server
.
What is bothering me is that when I manually log into the server as root, and do the command yes `echo $password` | passwd newuser
and then logout and try again newuser@server
, it works...
Sorry if it isn't very clear, and thanks for anyone who will try to help !
EDIT :
The script looks like this now it is a bit clearer but it still doesn't add the right password, I don't know what it gives as a new password...
#!/bin/bash
password=`cat /root/scripts/password`
for i in `cat /root/scripts/LIST_TEST.txt`
do
printf "Serveur : $i \n"
ssh $i 'adduser newuser; yes $password | passwd newuser'
echo $password
done
Solution
Try to create a script which will be changing the passwords and execute that in the remote machine like this:
Change your fist script:
#!/bin/bash
password=`cat /root/scripts/password`
for i in `cat /root/scripts/LIST_TEST.txt`
do
printf "Serveur : $i \n"
#Password is passed as $1 to the next script
cat ./paschange.sh $passwd | ssh $i #Command to excecute the script
echo $password
done
And paschange.sh
will be:
#!/bin/bash
adduser newuser
yes $1 | passwd newuser #Password is passed as $1
Note that this assumes:
- You will create a user named:
'newuser
' with the same name in ALL servers - He will have the same password for all servers
- You are using the same name to login yourself to all those servers to run the script, which the username you have on the computer you are currently executing this script.
Answered By - YourHelper Answer Checked By - David Goodson (WPSolving Volunteer)