Entries tagged "tip and tricks"

IPv6 for the masses, meet Teredo

Nov 19, 2008 11:33:00 AM

Teredo is a tunneling protocol designed to grant IPv6 connectivity to nodes that are located behind IPv6-unaware NAT devices. It defines a way of encapsulating IPv6 packets within IPv4 UDP datagrams that can be routed through NAT devices and on the IPv4 internet.

Enabling Teredo on your device is quite simple:

  • In Mac OS X, you can use Miredo OSX port
  • In (Debian/Ubuntu) Linux, just apt-get install miredo
  • In Windows Vista, Teredo is enabled by default
  • In Windows XP, open a shell, and netsh interface ipv6 install ; netsh interface ipv6 set teredo client
  • Or read how to enable Teredo on other platforms

With Teredo enabled you can use IPv6 practically everywhere you have IPv4 connectivity, happy IPv6 hacking!

 

Python LazyDict

Nov 4, 2008 2:42:00 PM

I'm a great fan of the dot notation, instead of foo['bar'] hash notation. Also because the Django and Jinja templating engines use dot notation for all their object attribute/key lookups.

You can also use assignments and lookups on a dictionary, using the __setattr__ and __getattr__ methods:

class LazyDict(dict):
    def __getattr__(self, attr):
        if attr in self:
            return self[attr]
        else:
            raise AttributeError, "'%s' object has no attribute '%s'" \
                % (self.__class__.__name__, attr)

    def __setattr__(self, attr, value):
        if hasattr(super(LazyDict, self), attr):
            raise AttributeError, "'%s' object already has attribute '%s'" \
                % (self.__class__.__name__, attr)
        self[attr] = value

This allows you to do:

>>> from lazydict import LazyDict
>>> foo = LazyDict()
>>> foo.bar = 42
>>> foo.bar
42
>>> foo.biz = LazyDict()
>>> foo.biz.qux
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "lazydict.py", line 6, in __getattr__
AttributeError: LazyDict instance has no attribute 'qux'
>>> foo.biz.qux = object()
>>> foo.biz.qux
<object object at 0xb7d26468>

Update Nov 12, 1008

Incorporated Floes' suggestions from the comment below, thanks!

 

SSH visual fingerprints

Oct 2, 2008 12:43:00 PM

OpenSSH version 5.1 comes with a great new feature, namely visual fingerprint identification. This helps you in recognizing and identifying changes in fingerprints.

To enable visual fingerprinting for all your SSH sessions, add this to your ~/.ssh/config file:

Host *
    VisualHostKey yes

This gives you a pretty piece of ASCII art when connecting to a remote host:

$ ssh labs.tehmaze.com
Host key fingerprint is fb:24:0a:db:f3:10:d2:33:20:14:fb:43:52:1e:05:50
+--[ RSA 2048]----+
|  ++Eo.          |
| . + .           |
|  + +            |
|   = o           |
|    + = S        |
|     o + .       |
|    . . o .      |
|     +.o +       |
|    . oo. .      |
+-----------------+

Last login: Thu Oct  2 12:38:55 2008 from *.nl
wijnand@drone:~$ 

Trying another host...

$ ssh base
Host key fingerprint is 99:a3:6f:96:78:9c:ef:d7:83:26:33:1f:bd:b0:c7:be
+--[ RSA 2048]----+
|                 |
|                 |
|                 |
|         o       |
|        S        |
|       . .   .   |
|      .o o  o+.  |
|      ..B + ++=. |
|       +.ooBooEo |
+-----------------+

Last login: Thu Oct  2 10:47:23 2008 from *.nl
wijnand@base:~$ 

Ain't it pretty?

 

Stacking Django Q lookups in OR/AND fashion

Oct 1, 2008 12:49:00 AM

Just a quick one so I won't forget, I was wondering how to compile a OR lookup stack from a list of Q objects.

>>> # Import the operator built-in module
>>> import operator
>>> from django.db.models import Q
>>> from mytest.models import Test
>>> # Create an arbitrary set of test-data
>>> for x in xrange(0, 99): Test(test=x).save()
>>> # The lookups
>>> lookups = [Q(test=1), Q(test=2), Q(test=23), Q(test=42)]
>>> # Here we use reduce()
>>> print Test.objects.filter(reduce(operator.or_, lookups))
[<Test test=1>, <Test test=2>, <Test test=23>, <Test test=42>]

This also works for operator.and_.

 

Remote tabcompletion using OpenSSH and zsh

Aug 3, 2008 3:04:00 AM

The zsh shell comes with (more than one) great feature(s), such as remote tabcompletion. If you for example want to copy a file over scp, simply hit tab at any part of the filename on the remote host. zsh is able to establish an ssh session on the background, and fetch the related information for you, so you can tabcomplete trough the remote files.

Read on..., 0 comments.

 

Visitors

Locations of visitors to this page