Otro de mis pequeños proyectos

Generar passwords aleatorios desde la consola

Seguro que alguna vez has necesitado una contraseña de estas complicadas y no sabí­as que poner. Pues bien, de las millones de posibilidades que tienes para generar una, yo tengo esta que la verdad, me parece sencilla y rápida:

cat /dev/urandom | tr -dc "a-zA-Z0-9-_\.\!\$\?" | fold -w 10 | head -n 1

Ok, explicación:

  • /dev/urandom: fuente aleatorio de información
  • tr -dc «a-zA-Z0-9-_\.\!\$\?»: elimina cualquier caracter que no sea una letra, un dí­gito o alguno de los sí­mbolos – _ . ! $ ?
  • fold -w 10: obliga a que el tamaño sea de 10 caracteres
  • head -n 1: mostrar una contraseña

Así­ pues, si quieres que tu contraseña tenga x caracteres, cambia el 10 del comando fold por ese valor y si quieres obtener más de una contraseña, indicalo cambiando el 1 de head por tantas como quieras. Por supuesto, si quieres cambiar los caracteres válidos, modifica la expresión de tr.

2 comentarios

  1. epplestun

    Muy bueno y muy completo el truquillo 🙂

  2. Nastia

    I would do this instead (off the top of my head, not evlaeatud thoroughly):- Write a C program to generate random numbers in the same way that $RANDOM does. Then call it in place of $RANDOM, like so (assume it’s called rnd).In code using $RANDOM => Use `rnd` or $(rnd)(Arrange for rnd.c to send its random number output to standard output.)Pros:- shorter than your solution using dd and /dev/urandom (Not much of a pro, except if using it a lot, and also, you could make your own solution shorter to type by packaging part of it in a shell script with a short name and then call that script with the rest of the original command line or you might even be able to use a shell alias in cases where the dd / urandom code comes at the start of the command line).Cons:- Since it is a C program, not a bash built-in like $RANDOM, it would load slower, which could be an issue in loops that run many times, depending on whether the Linux version keeps the executable in memory or not between invocations.- C compiler may not be available on all Linux machines where you want to use it, whereas bash should be. Could carry the executable on a USB stick and run it from the stick, or copy it to hard disk, but would need to do it on each new machine you use (if you use a lot of machines).There can be other pros and cons, not evlaeatud in detail – Vasudev

Deja una respuesta

Tu dirección de correo electrónico no será publicada. Los campos obligatorios están marcados con *