Showing posts with label bash. Show all posts
Showing posts with label bash. Show all posts

Wednesday, March 2, 2011

How to generate consistent "random" numbers

Here is an article that explain a way to get consistent "random" number. The problem appears in virtual environments, like lxc, where hostid is the same. The sample below solves this problem by generating "random" number from a network adapter (eth0) MAC address. So here it is (file func/random_sleep)
#!/bin/bash

# 
# random_sleep shift_max rnd_max
#
# shift_max - a number to generate consistent "random"
# number.
# rnd_max - a random number to add
random_sleep()
{
    shift_max=600
    if [ ! -z $1 ]; then shift_max=$1; fi

    rnd_max=60
    if [ ! -z $2 ]; then rnd_max=$2; fi

    # shift is a number betweem 0 and 600
    shift=`/sbin/ifconfig eth0 | /bin/grep HWaddr | \
        /bin/sed -e 's/.*HWaddr //' | /usr/bin/tr -d ':'`
    shift=$((0x$shift%$shift_max))

    # Sleep a random amount of time + shift
    sleep_time=$(($RANDOM % $rnd_max + $shift))

    echo "Sleeping for $sleep_time seconds..."
    /bin/sleep $sleep_time
}
Here is how you can use it:
#!/bin/bash
. /usr/local/sbin/func/random_sleep
# or
# . `dirname $_`/func/random_sleep

random_sleep 300 60

Wednesday, October 27, 2010

Recovering from Ctrl+S in Putty

The problem is related to XON/XOFF command that is mapped to Ctrl+S sequence. The terminal doesn't echo the commands you issue, so you need to remember press Ctrl+Q in order to turn flow control ON. There is a way to ignore such behaviour. What you need to do is to change your terminal characteristics.
stty -ixon
Consider add this command to your /etc/profile.d/ixon.sh file.

Friday, April 30, 2010

Color Bash Prompt

User root doesn't have colored bash prompt by default, you can enable color prompt by adding the following to /etc/profile.d/colors.sh:
if [ "$BASH" ]; then
    # set a fancy prompt (non-color, unless we know 
    # we "want" color)
    case "$TERM" in
        linux | xterm-color) color_prompt=yes;;
    esac

    if [ "$color_prompt" = yes ]; then
        export PS1='\[\033[01;31m\]\h\[\033[00m\]:\
        \[\033[01;34m\]\w\[\033[00m\]\$ '
    else
        export PS1='\h:\w\$ '
    fi
    unset color_prompt
fi

alias ls='ls --color=auto'
alias grep='grep --color=auto'
Read more about powerful bash prompts here.

Thursday, April 22, 2010

Strong password generator

Here is a random password generator (put in your ~/.bashrc):
genpasswd() {
  local l=$1
  [ "$l" == "" ] && l=20
  tr -dc A-Za-z0-9_ < /dev/urandom | head -c ${l} | xargs
}
Run it:
genpasswd 8
Here is output:
Kh5FwyTi
See original article here (item #10).

Debian Packages

Debian provides makepasswd, apg and pwgen packages which provide programs (the name is the same as the package) that can be used for this purpose. Makepasswd will generate true random passwords with an emphasis on security over pronounceability while pwgen will try to make meaningless but pronounceable passwords.
deby01:~$ apg -m 14 -x 14 -M SNCl
dob4IpOfvosOp@
TrinVop
9twagNobryiph/
cewd4om\queOph

deby01:~$ makepasswd --chars 14
w7RWMqKxjMGgfV

deby01:~$ pwgen
Aezoo3ae doo1Phoe jac6eTai die4zeaG 
Tu1nee7P JioPh4wu the8Luub xeiNga3K 
...