Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Prevent pwgen -y generating backticks or quotes? (or otherwise sanitize output)

Tags:

bash

passwords

I'm using pwgen in a bash script. For security, we have to use the -y flag to include at least one special character. However, this frequently returns passwords with one or more of ` or " which break the surrounding script.

Can I prevent these characters being generated? If not, what's the cleanest way to remove, replace or otherwise sanitize pwgen's output to exclude these characters?

My current pwgen is;

intPW=$(pwgen -c -n -y -B -1 15)

which means intPW's value could be something like;

iqui`c:ee4so4Ch
hph7eew"ohr9Ee

I need to guarantee that every password has a symbol in it, just not one of the problematic symbols.

Anything I can think of in terms of replacement, from an if loop to a sed, breaks because I'm trying to target a backtick.

like image 271
Alex Avatar asked Oct 26 '25 05:10

Alex


2 Answers

If you have pwgen version 2.08, you can use the new option -r to exclude unwanted characters

pwgen -cnyB1 -r \`\" 15
like image 113
etopylight Avatar answered Oct 28 '25 17:10

etopylight


Assuming you don't also need to protect single quote characters, the following should be OK:

intPW=$(pwgen -c -n -y -B -1 15 | tr '`"' '$@')

That will translate ` to $ and " to @. Feel free to pick any other characters you fancy!

like image 38
Neil Winton Avatar answered Oct 28 '25 17:10

Neil Winton