Fast and easy password generation

Posted by Dan Sosedoff on January 22, 2011

I used to generate a lot of passwords and usually it would be an online service found by googling “generate password online”. It worked fine until i got tired of it and decided to find something else, much easier and faster. Something that will give me the results right away from terminal while doing server setup.

Here we go, bash script (found online and modified by my needs):

#!/bin/bash
 
charspool=('a' 'b' 'c' 'd' 'e' 'f' 'g' 'h' 'i' 'j' 'k' 'l' 'm' 'n' 'o' 'p'
'q' 'r' 's' 't' 'u' 'v' 'w' 'x' 'y' 'z' '0' '1' '2' '3' '4' '5' '6' '7'
'8' '9' '0' 'A' 'B' 'C' 'D' 'E' 'F' 'G' 'H' 'I' 'J' 'K' 'L' 'M' 'N' 'O'
'P' 'Q' 'R' 'S' 'T' 'U' 'V' 'W' 'X' 'Y' 'Z' '-' '_');
 
len=${#charspool[*]}
 
if [ $# -lt 1 ]; then
  num=20;
else
  num=$1;
fi
 
randomnumbers=$(head -c $num /dev/urandom | od -t u1 | awk '{for (i = 2; i <= NF; i++) print $i}')
echo -n "password: "
 
for c in $randomnumbers; do
  echo -n ${charspool[$((c % len))]}
done
echo

Installation

Just create a script called something like “genpassword” in your bin dir and make it executable.

sudo nano /usr/local/bin/genpassword
sudo chmod +x /usr/local/bin/genpassword

Works on any unix-like machine with bash installed.

Usage

Usage is pretty straight-forward. Just type “genpassword” in terminal and you’ll get the password.

genpassword # This will give a default length (20 chars) password
genpassword 64 # This will give you a 64 chars long password

Snippet: Cleanup your Git repository

Posted by Dan Sosedoff on June 15, 2010

Snippet (found on net) for removing files from repository that are no longer present under your project.

$ git rm $(git ls-files -d)

For best use add it to bash alias file: ~/.bashrc or ~/.bash-aliases (under ubuntu):

alias gitclean='git rm $(git ls-files -d)'

Date separated MySQL backups

Posted by Dan Sosedoff on September 18, 2009

Here is the bash shell script that makes archived dumps of your database server. All databases are separated from each other and stored into date based folders.

#!/bin/bash

MyUSER="root"
MyPASS=""
MyHOST="localhost"
NOW="$(date +"%d-%m-%Y")"
STOREDIR="/home/storage/backup/database/by_dates/$NOW"
DBLIST="$(mysql -u $MyUSER -h $MyHOST -Bse 'show databases')"

[ ! -d $STOREDIR ] && mkdir -p $STOREDIR || :

for db in $DBLIST
do
	FILE="$STOREDIR/$db.gz"
	mysqldump -u $MyUSER -h $MyHOST $db | gzip -9 > $FILE
done

Install Git on CentOS 5.2

Posted by Dan Sosedoff on June 28, 2009

First, we need to install all dependencies:

# yum install gettext-devel expat-devel curl-devel zlib-devel openssl-devel

Next, get the git 1.6.x sources:

# wget http://kernel.org/pub/software/scm/git/git-1.6.3.3.tar.gz

Then, unpack and cd into git sources folder and install it:

# make && make install & make clean

That`s it, now you`ll have git system ready to go.

Check root user permissions in bash scripts

Posted by Dan Sosedoff on March 09, 2009

For example, in your bash shell script you`re going to use some root-specific commands like network operations, mounting devices and so on. There are couple easy ways to check if your script is executing under root privileges.

#!/bin/bash
# ...
if [ "$(id -u)" != "0" ]; then
   echo "This script must be run as root" 1>&2
   exit 1
fi
# ...

Another way: use EUID. When user account created a user ID is assigned to each user. Bash shell stores the user ID in $UID variable. Your effective user ID is stored in $EUID variable.

#!/bin/bash
# ...
if [[ $EUID -ne 0 ]]; then
   echo "This script must be run as root" 1>&2
   exit 1
fi
# ...