Encyrpt does not work in bash script

Hi All,

I am looking here from any one.

I am preparing script for create user and set password to remote servers.
I have made password less. and able to add user and also able to set entry in /etc/shadow. But it does not make complete entry in shadow file. it lefts initial character which are after $.

cat /etc/shadow file - output
  
 user1:jpKuSI11sl4wz2zJ6C3w6Qph2evoCqrO8SJjaz1Ux32kbPkMUxz8ppw7DcnyIeEqxhbb0GxsyCq18amHxL.zaxD1IZ1.:17408:0:99999:7:::

******************************
script......

#!/bin/bash
for i in `cat servername1`;
do
ssh -q -t root@$i sudo /usr/sbin/useradd -u 1001  -s /bin/bash -m -d /home/user1 -c 'for user1' -p $6$9jpKuSI1$91sl4wz2zJ6C3w6Qph2evoCqrO8SJjaz1Ux32kbPkMUxz8ppw7DcnyIeEqxhbb0GxsyCq18amHxL.zaxD1IZ1. user1
echo user1 created on server $i
 done

You need to escape the leading $ character, or put the entire password in single quotes.
bash is eating the leading $. There used to be limits to the size of passwords, since you did not post the OS, I would guess you need to be careful of the number of characters.

FWIW: huge passwords invite users to write them down, which creates a security concern. Why? nobody can remember 40 random characters reliably. I do not know what you are trying to do, but this seems like a bad approach to me.

I am using rhel 6.5.

And I used the encrypt password in the script. (usermod -p, useradd -p)

and I tried single and double quote also . but it is not working.

can you please help.

Show what you tried.

I tried to create user on remote and set password also.

Below is my script. it creates user but does not set password correctly in /etc/shadow.

 #!/bin/bash
for i in `cat servername1`;
do
ssh -q -t root@$i sudo /usr/sbin/useradd -u 1001  -s /bin/bash -m -d /home/user1 -c 'for user1' -p $6$9jpKuSI1$91sl4wz2zJ6C3w6Qph2evoCqrO8SJjaz1Ux32kbPkMUxz8ppw7DcnyIeEqxhbb0GxsyCq18amHxL.zaxD1IZ1. user1
echo user1 created on server $i
 done

Did you actually try (showing only the -p argument ) :

-p '$6$9jpKuSI1$91sl4wz2zJ6C3w6Qph2evoCqrO8SJjaz1Ux32kbPkMUxz8ppw7DcnyIeEqxhbb0GxsyCq18amHxL.zaxD1IZ1.'  

# or 

-p "\$6\$9jpKuSI1$91sl4wz2zJ6C3w6Qph2evoCqrO8SJjaz1Ux32kbPkMUxz8ppw7DcnyIeEqxhbb0GxsyCq18amHxL.zaxD1IZ1."

I am assuming the period characters are part of the value you want.

Also consider actually logging in to the remote. (I think you may have done this, but use the above syntax) Then try your command, do not issue a command from ssh. Why? The command on the remote will have had the ' or " characters stripped before processing. Whenever you have issues with a complex ssh command, make the actual command work locally first - or put it in a shell script, scp the script over to the remote, then activate the script with an ssh command. This has eliminated some issues for me in the past.

Jim, Thanks for your reply.

I have changed in the script, what you suggested. but in shadow file does not put password correctly. it left after dollar($) sign.
It works fine in command prompt.

Please find my script and running output.

[root@server-rhel6 ~]# cat adduser.sh
#!/bin/bash
for i in `cat servername1`;
do
ssh -q -t root@$i sudo /usr/sbin/useradd -u 1001 -s /bin/bash -m -d /home/testuser -c 'URid_testuser-p "\$6\$h8GQyPz4\$I1C.whHxlC65VYVfnEfSunDf8W.zTl3OtU2p6fULLG0S7JGMnfrvCytj3VGBsZ3gK./fUDTL.Vxxxd22u48St." testuser
echo testuser created on server $i
done

Running script with -x option-----

[root@server-rhel6 ~]# sh -x adduser.sh
++ cat servername1
+ for i in '`cat servername1`'
+ ssh -q -t root@node1-rhel6 sudo /usr/sbin/useradd -u 1001-p '$6$h8GQyPz4$I1C.whHxlC65VYVfnEfSunDf8W.zTl3OtU2p6fULLG0S7JGMnfrvCytj3VGBsZ3gK./fUDTL.Vxxxd22u48St.' testuser
+ echo testuser created on server node1-rhel6
testuser created on server node1-rhel6
+ for i in '`cat servername1`'
+ ssh -q -t root@node2-rhel6 sudo /usr/sbin/useradd -u 1001 -s /bin/bash -m -d /home/adubey01 -c URid_testuser -p '$6$h8GQyPz4$I1C.whHxlC65VYVfnEfSunDf8W.zTl3OtU2p6fULLG0S7JGMnfrvCytj3VGBsZ3gK./fUDTL.Vxxxd22u48St.' testuser
+ echo testuser created on server node2-rhel6
testuser created on server node2-rhel6
[root@server-rhel6 ~]#

Shadow file output-----

testuser:.whHxlC65VYVfnEfSunDf8W.zTl3OtU2p6fULLG0S7JGMnfrvCytj3VGBsZ3gK./fUDTL.Vxxxd22u48St.:17410:0:99999:7:::

Please suggest.