Skip to content

Instantly share code, notes, and snippets.

@starkers
Last active May 17, 2021 17:41
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save starkers/234909caf153904e5428 to your computer and use it in GitHub Desktop.
Save starkers/234909caf153904e5428 to your computer and use it in GitHub Desktop.
generate password and copy it into clipboard
#!/usr/bin/env bash
# See: https://gist.github.com/starkers/234909caf153904e5428
#Check xpass exists
if [ ! -x "$(which xclip)" ]; then
echo "Please install xclip" ; exit 1
fi
#Length of string:
LEN=32
#Generate it
PASS="$(head -1000 /dev/urandom | tr -dc A-Za-z0-9 | tail -1 | cut -c 1-$LEN)"
#Import it into the clipboard
printf "$PASS" | xclip -i -selection clipboard
@starkers
Copy link
Author

generate a random password and have it ready for pasting

TL;DR = apt-get install xclip -y

The Objective is the quickly and easily generate a new password.
I bind this to a shortcut on my desktop environment to make life easier
Firstly lets consider how we're generating the password.

Good little read linux password generation

I'm using the kernels own generator.. good 'ol: /dev/urandom
It works fairly well on all the machines I've used it on:

  • solaris{9.10}
  • ubuntu/centos
  • aloha loadbalancers
  • no doubt any other posix compliant *nix

Example urandom usage:

PASS="$(< /dev/urandom tr -dc A-Za-z0-9 | head -c${1:-32};echo;)" ; echo "#$PASS#"

For some reason this just doesn't work reliably.. perhaps bash versions or something related to the initial STDIN "<". I don't care.. I just adapted it to this and so far so good:

PASS="$(head -1000 /dev/urandom | tr -dc A-Za-z0-9 | tail -1 | cut -c 1-32)" ; echo "#$PASS#"

alternative to /dev/urandom

if you're sticking to linux: apt-get install pwgen -y

Important Note!!

its essential that you remove trailing white-space and newlines (aka: \n or CR) before piping into xclip!

Bad pwgen example:

LEN=32 ; pwgen $LEN 1 | xclip -i -selection clipboard

Good pwgen example:

LEN=32 ; pwgen $LEN 1 | tr '\n' ' ' | sed 's+ ++g' | xclip -i -selection clipboard

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment