the only winning move is not to play_

Linux (41) LPIC (27) LDAP (12) Samba (9) Python (8) Security (7) Huawei (6) misc (6) Assembler (5) IPv6 (3) Crypto (2) expect (2) Windows (1) cryptogen (1)

2010/04/29

Command line BBC news reader.

My BBC RSS feed XML parser:

#!/usr/bin/env python
"""
Command line BBC News reader, by oozie <root ooz ie>

$ ./bbcnews.py
"""
import urllib2
from xml.parsers import expat

GREEN = '\x1b[32m'
BLUE = '\x1b[34m'
NORMAL = '\x1b[30m'

BBC_NEWS_RSS = 'http://newsrss.bbc.co.uk/'+\
               'rss/newsonline_world_edition/front_page/rss.xml'

class NewsItem(object):
    """Class representing a news item."""
    category = None
    description = None
    link = None
    title = None
    def summary(self):
        """Summarize news item in color."""
        return '%s%s: %s%s %s%s' % (BLUE, self.title,
                                    GREEN, self.description,
                                    NORMAL, self.link)
    def headline(self):
        """Print news item title and corresponding link."""
        return '%s%s %s%s' % (BLUE, self.title,
                              NORMAL, self.link)

def get_news(rss_feed):
    """Get a list of news items."""

    class _CurrentData(object):
        """Class holding a set of current attributes."""
        item = None
        text = None

    def _start_element_handler(name, attrs):
        """Handle XML start-elements."""
        if name == 'item':
            # Allocate a new item.
            current.item = NewsItem()

    def _end_element_handler(name):
        """Handle XML end-elements."""
        if name == 'item':
            news_items.append(current.item)
        elif name in ('title', 'description', 'link', 'category'):
            try:
                setattr(current.item, name, current.text)
            except AttributeError:
                # The parser has run into a non-news item.
                pass

    def _char_data_handler(data):
        """Handle XML element character data."""
        current.text = data

    news_items = list()
    current = _CurrentData()

    parser = expat.ParserCreate()
    parser.StartElementHandler = _start_element_handler
    parser.EndElementHandler = _end_element_handler
    parser.CharacterDataHandler = _char_data_handler

    news_handle = urllib2.urlopen(rss_feed)
    xml_data = news_handle.read()
    
    parser.Parse(xml_data)

    return news_items


if __name__ == '__main__':

    for news_item in get_news(BBC_NEWS_RSS):
        print news_item.summary()
Digg Technorati Delicious Facebook slashdot

2010/01/24

Python code to recognize a color of a chessboard square

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'

Digg Technorati Delicious Facebook slashdot

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:

$ ./script.sh
CCC
DDD
AAA
BBB
$
Solution: A recursive expect script.
Digg Technorati Delicious Facebook slashdot

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"
$ python -c "print ''.join([r'\x%x' % ord(c) for c in \"$COLOR\"])"
\x1b\x5b\x31\x3b\x33\x31\x3b\x34\x30\x6d
ANSI sequence strings escaped this way can be further used in except scripts.

#!/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"
Digg Technorati Delicious Facebook slashdot

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.
  1. Explain the following terms: SSL, TLS, CA, CRL, OCSP, PKI, PRNG
  2. List goals of cryptography.
  3. What is the difference between symmetric and public key encryption?
  4. List three cryptographic hash functions. What are their strengths and weaknesses?
  5. Explain what the term "digital signature" means.
  6. What are the challenges of SSL?
  7. Build OpenSSL from source.
  8. Use openssl to compute SHA1 and MD5 message digests for a given file.
  9. Encrypt and decrypt a file using 3DES.
  10. Generate parameters for Diffie-Hellman key exchange.
  11. Create a pair of DSA and RSA keys.
  12. What is the major difference between RSA and DSA?
  13. Explain the purpose of a CRL.
  14. What are Certificate extensions and how to use them?
  15. Do some research about OCSP (RFC2560)
  16. Create a CA environment.
  17. Generate a self signed certificate.
  18. Generate a certificate request.
  19. Issue a few certificates from certificate requests.
  20. Revoke some of the generated certificates.
  21. 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
  22. Print the certificate in the text form
    $ cat cert.txt|openssl x509 -text
Digg Technorati Delicious Facebook slashdot

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.
The installation process goes as follows:
  1. Boot off the Gentoo MINI-install CD for your architecture and download cryptogen.sh
  2. You can customize the settings contained in the header of the script, most users however, will be just happy to run it.
  3. Follow the instructions.

Enjoy!
Digg Technorati Delicious Facebook slashdot

2009/07/30

Timestamp converter powered by Google AppEngine


I wrote a semi-useful AppEngine application, a Unix/AD-LDAP timestamp converter. It's approximately 130 lines of code, plus an HTML template.


It converts Unix and Active Directory/LDAP timestamps to human readable UTC strings. It does it also from the command line...

Digg Technorati Delicious Facebook slashdot