Friday, November 11, 2011

Generation of Random Numbers from PING

When looking for a flawless Random number generator I realized ping would be it.  This worked well because we were already testing the connectivity between hosts using scripted ping, so we could repurpose that data for random number generation.

We could also save the random numbers to disk and use them one time as a pad then delete them.

sokol@server:~/test$ ./getrand
97598993
sokol@server:~/test$ ./getrand
27EC0C20
sokol@server:~/test$ ./getrand
F31CE56C


sokol@server:~/test$ cat getrand
#!/bin/bash
target=google.com
#c=0
#while [ $c -lt 8 ]
#do
#c=$[$c+1]

{
echo "obase = 16"
ping -c 8 -i 0.2 $target |\
grep --line-buffered "time=" |\
sed -u \
-e 's/[a-z \.(),_:+=-]//g' \
-e 's/\(.\)/\1+/g' \
-e 's/^\(.*\)+$/(\1)% 16/'
} |\
bc |\
tr -d '\n'

echo ""
#done

In the code commented out is the ability to run this in a loop, also if your not going to enjoy the output in real-time you can remove the --line-buffered from grep and -u from sed, it may make a small performance improvement    Keep in mind each 4 bit hex work is the result of one ping. So this is taking up network resources.

Generation of 8x8 tile Random Hex Digits

sokol@server:~/test$ sudo ./getrand2
7BDBF0AA
6BC6BC42
B63F2397
D55A5A5C
F10A1A7D
112329BC
110099F7
C1F53C98

sokol@server:~/test$ cat getrand2
#!/bin/bash
target=google.com
c=0
while [ $c -lt 8 ]
do
c=$[$c+1]

{
echo "obase = 16"
ping -c 8 -i 0.02 $target |\
grep "time=" |\
sed \
-e 's/[a-z \.(),_:+=-]//g' \
-e 's/\(.\)/\1+/g' \
-e 's/^\(.*\)+$/(\1)% 16/'
} |\
bc |\
tr -d '\n'
echo ""
done

This is easy enough to adapt for any purpose, and even redirect output to a file.

you need to be root to set the ping Interval time to below 0.2. Sudo does this.

Under the Hood


Below I peel off each layer to show how it's being done.


This is what just ping generates

sokol@server:~/test$ sudo ping -c 8 -i 0.02 google.com | grep "time="
64 bytes from 173.194.64.99: icmp_seq=1 ttl=47 time=8.86 ms
64 bytes from 173.194.64.99: icmp_seq=2 ttl=47 time=8.87 ms
64 bytes from 173.194.64.99: icmp_seq=3 ttl=47 time=8.86 ms
64 bytes from 173.194.64.99: icmp_seq=4 ttl=47 time=8.08 ms
64 bytes from 173.194.64.99: icmp_seq=5 ttl=47 time=8.08 ms
64 bytes from 173.194.64.99: icmp_seq=6 ttl=47 time=8.85 ms
64 bytes from 173.194.64.99: icmp_seq=7 ttl=47 time=8.97 ms
64 bytes from 173.194.64.99: icmp_seq=8 ttl=47 time=8.93 ms


After everything but the number have been stripped out, added together and modulo 16.

sokol@server:~/test$ sudo ping -c 8 -i 0.02 google.com | grep "time=" | sed -e 's/[a-z \.(),_:+=-]//g' -e 's/\(.\)/\1+/g' -e 's/^\(.*\)+$/(\1)% 16/'
(6+4+1+7+3+1+9+4+6+4+9+9+1+4+7+8+9+4)% 16
(6+4+1+7+3+1+9+4+6+4+9+9+2+4+7+8+9+2)% 16
(6+4+1+7+3+1+9+4+6+4+9+9+3+4+7+8+9+1)% 16
(6+4+1+7+3+1+9+4+6+4+9+9+4+4+7+7+9+8)% 16
(6+4+1+7+3+1+9+4+6+4+9+9+5+4+7+7+9+8)% 16
(6+4+1+7+3+1+9+4+6+4+9+9+6+4+7+7+9+8)% 16
(6+4+1+7+3+1+9+4+6+4+9+9+7+4+7+7+9+6)% 16
(6+4+1+7+3+1+9+4+6+4+9+9+8+4+7+8+9+3)% 16

What bc generates for it's output. But I send it the command "obase=16" first that makes it's output Hex.

sokol@server:~/test$ sudo ping -c 8 -i 0.02 google.com | grep "time=" | sed -e 's/[a-z \.(),_:+=-]//g' -e 's/\(.\)/\1+/g' -e 's/^\(.*\)+$/(\1)% 16/' | bc
5
4
6
9
6
7
13
10


Read also: Testing ping based Random number generator

No comments: