Sometimes you need to pass a password or even just a string on the command line which you would rather obscure. For example:
serverControl.sh -u admin -p $MYPASS -c shutdown
Note anything below is not the ideal way of dealing with passwords you should probably use SSH keys if possible instead.
Sometimes you really do not have a better option and this might be your only option. Still it is a weak solution though to store passwords. I simplified but you probably don”t want to use obvious variable names or files either.
Very simple base64 encoding:
cGFzc3dkCg==$ echo "cGFzc3dkCg==" | base64 --decodepasswd
# Use in script as follow or better use a file to store the string:MYENCPASS="cGFzc3dkCg=="MYPASS=`echo "$MYENCPASS" | base64 --decode`Using aesutil:
I saw someone mention aesutil on the Internet but it appears like few modern Linux distros comes with aesutil tools though.
$ SALT=`mkrand 15` passwd
$ `echo "passwd" | aes -e -b -B -p $SALT`i/b9pkcpQAPy7BzH2JlqHVoJc2mNTBM=
# Use in script as follow or use a file to store the string:MYENCPASS="i/b9pkcpQAPy7BzH2JlqHVoJc2mNTBM="MYPASS=`echo "$MYENCPASS" | aes -d -b -p $SALT`Or maybe openssl is an option:
This is still very lame as you still have to use a password for the opensssl command. I just named it garbageKey but you are probably better off making it more obscure.
$ echo 'mySecretPassword' | openssl enc -base64 -e -aes-256-cbc -nosalt -pass pass:garbageKeyyQA4stTBI8njgNgdmttwjlcFrywQD4XEIgK8HzqEOxI=$ echo 'yQA4stTBI8njgNgdmttwjlcFrywQD4XEIgK8HzqEOxI=' | openssl enc -base64 -d -aes-256-cbc -nosalt -pass pass:garbageKeymySecretPassword
# Use a hidden file$ echo 'mySecretPassword' | openssl enc -base64 -e -aes-256-cbc -nosalt -pass pass:garbageKey > .hidden.lck$ cat .hidden.lckyQA4stTBI8njgNgdmttwjlcFrywQD4XEIgK8HzqEOxI=
# In a script$ MYENCPASS=`cat .hidden.lck | openssl enc -base64 -d -aes-256-cbc -nosalt -pass pass:garbageKey`$ echo $MYENCPASSmySecretPasswordAs you can see in the last example I used a hidden file also instead of keeping the encryption string in the file.