I’m trying to generate a random string using this command:
strings /dev/urandom | grep -o '[[:alnum:]]' | head -n 30 | tr -d '\n';
Works fine, but when I try to do subprocess.call(cmd,shell=True) it just gets stuck on the strings /dev/urandom command and spams my screen with grep: writing output: Broken pipe
What’s causing this and how do I fix it?
No need for subprocess, observe:
Also,
stringsing and thengreping alphanumeric characters from/dev/urandomis hugely inefficient and wastes a whole lot of randomness. On my desktop PC, the above python takes less than 10 ms to executed from bash, yourstrings ...oneliner takes 300-400…For a pure python solution that works also on systems without
/dev/urandom– and gives only alphanumeric characters (if you really don’t want + or /):