Friday, March 30, 2007

random number generator

i just happen to know that we can easily generate random number using $RANDOM
$ echo $RANDOM
12517
$ echo $RANDOM
18362
$ echo $RANDOM
26219
$ echo $RANDOM
26056
$ echo $RANDOM
30305
$ echo $RANDOM
9606
but if you want to get smaller random number eg. less than 100, you can do this:
$ number=$((RANDOM%100))
$ echo $number
75
20 random numbers less than 100:
$ count=1
$ while [ $count -le 20 ] ; do number=$((RANDOM%100)); echo $number; let "count += 1"; done
93
14
19
96
61
86
63
48
45
26
11
0
89
54
11
84
17
10
99
12
random number less than 10:
$ number=$((RANDOM%10))
$ echo $number
4
5 random numbers less than 10:
$ count=1
$ while [ $count -le 5 ] ; do number=$((RANDOM%10)); echo $number; let "count += 1"; done
3
4
9
6
7
and so on...

Thursday, March 15, 2007

keychain rocks!

do you ever feel tired to key in your password or passphrase every time you want to ssh into your servers OR you need to run automate jobs (cron job) to ssh into specific servers but unfortunately unable to do it since you still need to key in your password or passphrase manually. well worry no more, here come the solution: use keychain!

first of all you'll need to setup the authentication using public key instead. then download the keychain from http://dev.gentoo.org/~agriffis/keychain/ & install the keychain into your Linux workstation.

ashterix@desktop:/home/ashterix $ ls -l bin/keychain
-rwx------ 1 ashterix u_026776 53740 Feb 27 08:25 bin/keychain


put this into your .profile or .bash_profile
keychain ~/.ssh/id_rsa
. ~/.keychain/`hostname`-sh


and you're done! the first time your login you'll ask to enter your passphrase of course.

KeyChain 2.6.8; http://www.gentoo.org/proj/en/keychain/
Copyright 2002-2004 Gentoo Foundation; Distributed under the GPL

* Initializing /home/ashterix/.keychain/desktop-sh file...
* Initializing /home/ashterix/.keychain/desktop-csh file...
* Initializing /home/ashterix/.keychain/desktop-fish file...
* Starting ssh-agent
* Adding 1 ssh key(s)...
Enter passphrase for /home/ashterix/.ssh/id_rsa:
Identity added: /home/ashterix/.ssh/id_rsa (/home/ashterix/.ssh/id_rsa)

ashterix@desktop:/home/ashterix $


if you log off & login again, the ssh-agent already there & you no need to key in your passphrase again.

KeyChain 2.6.8; http://www.gentoo.org/proj/en/keychain/
Copyright 2002-2004 Gentoo Foundation; Distributed under the GPL

* Found existing ssh-agent (19507)
* Known ssh key: /home/ashterix/.ssh/id_rsa

ashterix@desktop:/home/ashterix $


then from here you can ssh to any servers without key in the passphrase.
the same if you want to run automate ssh cron jobs, the passphrase automatically loaded, there's no need to key in the passphrase manually. cool eh :)

ashterix@desktop:/home/ashterix $ ssh server

ashterix@server :/home/ashterix $


for more info read here:
http://www-128.ibm.com/developerworks/library/l-keyc2/
http://www.gentoo.org/proj/en/keychain/index.xml
http://www.gentoo.org/doc/en/keychain-guide.xml