def chesssquare(square):
"""Return the color of a chessboard square."""
rank,file=square
return ord(file) & int(rank) & 1 and 'black' or 'white'
In [1]: chesssquare('a1')
Out [1]: 'black'
In [2]: chesssquare('A2')
Out [2]: 'white'
the only winning move is not to play
2010/01/24
Python code to recognize a color of a chessboard square
2009/11/29
Recursive procedures in expect for capturing out-of-order output.
Problem: You use expect and want to make sure that a number of units of output from a script exist but there is no guarantee in what order they would come. An example bash script:
#!/bin/bash
echo AAA &
echo BBB &
echo CCC &
echo DDD &
Can produce the following output:Solution: A recursive expect script.
$ ./script.sh
CCC
DDD
AAA
BBB
$
2009/11/28
Colorful output in expect.
If you want to change shell color attributes from expect make sure that you escape at least the first three characters of the ANSI color sequence. This is necessary due to the way expect interprets strings. For instance, if you try to declare the red color sequence like this:
set red "\x1b[1;31;40m"expect will think that you forgot to close the square bracket. If you escape the square bracket and continue like this:
set red "\x1b\x5b1;31;40m"expect will think that the following "1" belongs to the preceding"\x5b". The following trick can be used to quickly convert an un-escaped string to a fully hex-escaped one:
$ export RED="\x1b[1;31;40m"ANSI sequence strings escaped this way can be further used in except scripts.
$ python -c "print ''.join([r'\x%x' % ord(c) for c in \"$COLOR\"])"
\x1b\x5b\x31\x3b\x33\x31\x3b\x34\x30\x6d
#!/usr/bin/env expect
# Colorful output from expect.
# Slawek Ligus
proc stripe_write {text} {
# This procedure prints every second character of a given
# string argument $text in red.
# RED="\x1b[1;31;40m"
set red "\x1b\x5b\x31\x3b\x33\x31\x3b\x34\x30\x6d"
# NORMAL="\x1b[1;0;40m"
set normal "\x1b\x5b\x31\x3b\x30\x3b\x34\x30\x6d"
for {set i 0} {$i < [string length $text]} {incr i 2} {
puts -nonewline "$red[string index $text $i]"
puts -nonewline "$normal[string index $text [expr $i+1]]"
}
puts -nonewline $normal
}
stripe_write " Expect in color. \n"
stripe_write " http://blog.ooz.ie/ \n"
stripe_write "=====================\n"
2009/11/01
Network Security with OpenSSL - exercises.
Set of exercises to the book of Network Security with OpenSSL by J. Viega, M. Messier, P. Chandra.

- Explain the following terms: SSL, TLS, CA, CRL, OCSP, PKI, PRNG
- List goals of cryptography.
- What is the difference between symmetric and public key encryption?
- List three cryptographic hash functions. What are their strengths and weaknesses?
- Explain what the term "digital signature" means.
- What are the challenges of SSL?
- Build OpenSSL from source.
- Use openssl to compute SHA1 and MD5 message digests for a given file.
- Encrypt and decrypt a file using 3DES.
- Generate parameters for Diffie-Hellman key exchange.
- Create a pair of DSA and RSA keys.
- What is the major difference between RSA and DSA?
- Explain the purpose of a CRL.
- What are Certificate extensions and how to use them?
- Do some research about OCSP (RFC2560)
- Create a CA environment.
- Generate a self signed certificate.
- Generate a certificate request.
- Issue a few certificates from certificate requests.
- Revoke some of the generated certificates.
- Retrieve HTTPS certificates of some of the Internet giants, e.g.
$ echo|openssl s_client -connect www.google.com:443|\
sed -ne '/-BEGIN CERTIFICATE-/,/-END CERTIFICATE-/p' > cert.txt - Print the certificate in the text form
$ cat cert.txt|openssl x509 -text
2009/09/23
Install encrypted Gentoo in no time.
Having a spare moment I decided to revisit my older post about installing encrypted Gentoo Linux as fast and easy as possible. That post is well out of date and the scripts were not working anymore as pointed out by a kind anonymous reader. So over the last two days I worked out a new/better way of installing an encrypted base Gentoo, it's a single shell script using dialog for ncurses interface (included on the mini-install DVDs). It's configuration is contained in the header, just under the LICENSE. |
- Boot off the Gentoo MINI-install CD for your architecture and download cryptogen.sh
- You can customize the settings contained in the header of the script, most users however, will be just happy to run it.
- Follow the instructions.
Enjoy!
2009/07/30
Timestamp converter powered by Google AppEngine
2009/07/19
sms_exec.py - SMS controlled POSIX machine.
I have just released the beta of PyHumod 0.02 with improved event handling. It comes with a sample application, sms_exec.py, that allows the user to execute remote commands on a POSIX system by sending an SMS into the modem, that subsequently is interpreted by the shell on the privileges of the user running sms_exec.py.

Subscribe to:
Posts (Atom)
