Passwortgenerator: Unterschied zwischen den Versionen

Aus wiki.archlinux.de
(Die Seite wurde neu angelegt: „Ein einfacher Passwortgenerator #!/bin/bash # Generate a random password # # $1 number of characters; defaults to 20 # # $2 charset # 0 = special ch…“)
 
(Skript um eine Hilfefunktion erweitert und eine Absicherung gegen Fehleingaben eingebaut.)
Zeile 1: Zeile 1:
Ein einfacher Passwortgenerator
Ein "einfacher" Passwortgenerator


  #!/bin/bash
  #!/bin/bash
Zeile 7: Zeile 7:
  # $1 number of characters; defaults to 20
  # $1 number of characters; defaults to 20
  #
  #
  # $2 charset  
  # $2 charset
  # 0 = special characters, numbers and letters (default)
  # 0 = special characters, numbers and letters (default)
  # 1 = numbers and letters
  # 1 = numbers and letters
  # 2 = letters only
  # 2 = letters only
# 3 = numbers only
  #
  #
  #
  #
  # usage: pwgen.sh <LENGTH> <CHARS>
  # usage: pwgen.sh <LENGTH> <CHARS>
  #
  #
  # examples:  
  # examples:
  #
  #  
  # length = 20, special characters, numbers and letters
  # length = 20, special characters, numbers and letters
  # pwgen.sh  
  # pwgen.sh
  #
  #
  # length = 10, special characters, numbers and letters
  # length = 10, special characters, numbers and letters
Zeile 30: Zeile 31:
   
   
   
   
_helptext_()
  if [ "$1" = "" ]; then
  {
LENGTH=20;
    echo
else
    echo "\"pwgen.sh\" generate a random password."
LENGTH=$1
    echo "usage: pwgen.sh [LENGTH CHARS] [-h]"
  fi
    echo
    echo "LENGTH number of characters; default is 20"
    echo "CHARS 0 = special characters, numbers and letters (default)"
    echo " 1 = numbers and letters"
    echo " 2 = letters only"
    echo " 3 = numbers only"
    echo "-h Show this text"
    echo
    echo "If one introduces \"pwgen.sh\" from without \"[LENGTH CHARS]\", then the default values are used."
    echo
    echo "Examples:"
    echo
    echo "\"pwgen.sh\" OR \"pwgen.sh 20 0\""
    echo "  length: 20 characters"
    echo "  charset: special characters, numbers and letters"
    echo "  example: "`cat /dev/urandom | tr -dc [:graph:] | head -c 20`
    echo
    echo "pwgen.sh 10"
    echo "  length: 10 characters"
    echo "  charset: special characters, numbers and letters"
    echo "  example: "`cat /dev/urandom | tr -dc [:graph:] | head -c 10`
    echo
    echo "pwgen.sh 8 1"
    echo "  length: 8 characters"
    echo "  charset: numbers and letters"
    echo "  example: "`cat /dev/urandom | tr -dc [:alnum:] | head -c 8`
    echo
    echo "pwgen.sh 25 2"
    echo "  length: 25 characters"
    echo "  charset: letters only"
    echo "  example: "`cat /dev/urandom | tr -dc [:alpha:] | head -c 25`
    echo
    echo "pwgen.sh 4 3"
    echo "  length: 4 characters"
    echo "  charset: numbers only"
    echo "  example: "`cat /dev/urandom | tr -dc [:digit:] | head -c 4`
    echo
    exit 0
}
   
   
   
_invalid_()
{
    echo
    echo "Invalid input. For more information use"
    echo "  pwgen.sh -h"
    echo
    exit 1
}
   
   
if [ "$2" = "" ]; then
CHARS="[:graph:]"
fi
   
   
  if [ "$2" = "1" ]; then
  if [ "$1" = "" ]; then
CHARS="[:alnum:]"
    LENGTH=20
elif [ "$1" = "-h" ]; then
    _helptext_
<Nowiki>elif [[ `echo "$1" | grep -E ^[[:digit:]]+$` ]]; then</Nowiki>
    LENGTH=$1
else
    _invalid_
  fi
  fi
   
   
if [ "$2" = "2" ]; then
CHARS="[:alpha:]"
fi
   
   
case $2 in
    "") CHARS="[:graph:]" ;;
    0) CHARS="[:graph:]" ;;
    1) CHARS="[:alnum:]" ;;
    2) CHARS="[:alpha:]" ;;
    3) CHARS="[:digit:]" ;;
    *) _invalid_ ;;
esac
   
   
echo
  cat /dev/urandom | tr -dc $CHARS | head -c $LENGTH
  cat /dev/urandom | tr -dc $CHARS | head -c $LENGTH
echo
  echo
  echo


[[Kategorie:Scripte]]
[[Kategorie:Scripte]]

Version vom 4. März 2014, 01:04 Uhr

Ein "einfacher" Passwortgenerator

#!/bin/bash

# Generate a random password
#
# $1 number of characters; defaults to 20
#
# $2 charset
# 0 = special characters, numbers and letters (default)
# 1 = numbers and letters
# 2 = letters only
# 3 = numbers only
#
#
# usage: pwgen.sh <LENGTH> <CHARS>
#
# examples:
# 
# length = 20, special characters, numbers and letters
# pwgen.sh
#
# length = 10, special characters, numbers and letters
# pwgen.sh 10
#
# length = 8, numbers and letters
# pwgen.sh 8 1
#
# length = 25, letters only
# pwgen.sh 25 2


_helptext_()
{
    echo
    echo "\"pwgen.sh\" generate a random password."
    echo "usage: pwgen.sh [LENGTH CHARS] [-h]"
    echo
    echo "LENGTH		number of characters; default is 20"
    echo "CHARS		0 = special characters, numbers and letters (default)"
    echo "		1 = numbers and letters"
    echo "		2 = letters only"
    echo "		3 = numbers only"
    echo "-h		Show this text"
    echo
    echo "If one introduces \"pwgen.sh\" from without \"[LENGTH CHARS]\", then the default values are used."
    echo
    echo "Examples:"
    echo
    echo "\"pwgen.sh\" OR \"pwgen.sh 20 0\""
    echo "   length:	20 characters"
    echo "   charset:	special characters, numbers and letters"
    echo "   example:	"`cat /dev/urandom | tr -dc [:graph:] | head -c 20`
    echo
    echo "pwgen.sh 10"
    echo "   length:	10 characters"
    echo "   charset:	special characters, numbers and letters"
    echo "   example:	"`cat /dev/urandom | tr -dc [:graph:] | head -c 10`
    echo
    echo "pwgen.sh 8 1"
    echo "   length:	8 characters"
    echo "   charset:	numbers and letters"
    echo "   example:	"`cat /dev/urandom | tr -dc [:alnum:] | head -c 8`
    echo
    echo "pwgen.sh 25 2"
    echo "   length:	25 characters"
    echo "   charset:	letters only"
    echo "   example:	"`cat /dev/urandom | tr -dc [:alpha:] | head -c 25`
    echo
    echo "pwgen.sh 4 3"
    echo "   length:	4 characters"
    echo "   charset:	numbers only"
    echo "   example:	"`cat /dev/urandom | tr -dc [:digit:] | head -c 4`
    echo
    exit 0
}


_invalid_()
{
    echo
    echo "Invalid input. For more information use"
    echo "   pwgen.sh -h"
    echo
    exit 1
}


if [ "$1" = "" ]; then
    LENGTH=20
elif [ "$1" = "-h" ]; then
    _helptext_
elif [[ `echo "$1" | grep -E ^[[:digit:]]+$` ]]; then
    LENGTH=$1
else
    _invalid_
fi


case $2 in
    "") CHARS="[:graph:]" ;;
    0) CHARS="[:graph:]" ;;
    1) CHARS="[:alnum:]" ;;
    2) CHARS="[:alpha:]" ;;
    3) CHARS="[:digit:]" ;;
    *) _invalid_ ;;
esac

echo
cat /dev/urandom | tr -dc $CHARS | head -c $LENGTH
echo
echo