I'm trying to set a users password from a shell script by passing an argument to the shell script so that the password itself is not embedded within the file, and can be updated and interchanged between systems.
Here is an example password that has all sorts of problems, and is just one of many examples of something Bash pukes on: 'my@secret\npasword123!
The way that this is being run and scripted I am going to call "semi-automation" in that there will be a user behind a keyboard having to place the argument which contains the password.
Here's an example of what that looks like from the command line to visualize
./password.sh administrator 'my@secret\npasword123!
Here's what I've currently looked up for advice but have come up short, I feel like putting them here for reference may be helpful as it's possible I overlooked something but also to get a better idea as to other users with similar problems.
- Changing an AIX password via script?
- bash: how to pass password containing special characters
- Script to change password on linux servers over ssh
- Passing Variable with Special characters as password breaks bash script. How do I Sanitize special characters in bash?
Here are some various methods I've currently tried for my password.sh
file but have come up short with each of them
echo -e '$2\n$2' | (passwd --stdin $1)
echo $1:$2 | sudo chpasswd
yes $2 | passwd $1
sudo passwd $1 <<EOF
$2
$2
EOF
What I'm hoping to find here, is a simple "one size fits all" catch all solution that will let me encapsulate any sort of randomly generated password that can't be escaped by any means and all edge cases are covered, backslashes, dollar signs, single and double quotes, whatever Bash pukes on. Having to manually escape every instance of a special character as mentioned is out of the question.
I would also a way to quickly programmatically turn what would be a password containing special characters into a modified escaped sequence just for Bash. I looked up things such as "Bash Escape Sequence Converter" but I couldn't find any sort of tool that I could give input and would spit out a sequence that would do the needful in making sure Bash behaved as expected once I fed it that. This would add an additional step to the process, but would be acceptable.
Lastly, if there are any other alternative methods to doing and running this I would also be interested in hearing them, as long as they're able to properly escape / ignore interpretation of special characters which are going to be expected for the password.
Since this is what I call "semi-automation", I would say a desired answer is something that I could run from a Bash shell as a one-liner like I described above ( ./password.sh administrator 'my@secret\npasword123!
) but I understand this may not be possible due to limitations with Bash. I'd also as a last ditch consider a way to prompt the user from the Shell for input after the script is run as valid, but I'm not entirely sure how some automation tools would take to that in the future so it may not be entirely future proof solution.
Thanks!