Tail -f in python, truncate aware

So while doing a little coding I tried to find some tail -f class in python that will recognize when file that we tailing is been truncated. All I found was some tail -f classes that brakes on file truncate or rotate.

Eventually I came up with this:

import time
from os import stat
from os.path import abspath
from stat import ST_SIZE
 
class LogTail:
    def __init__(self, logfile):
        self.logfile = abspath(logfile)
        self.f = open(self.logfile,"r")
        file_len = stat(self.logfile)[ST_SIZE]
        self.f.seek(file_len)
        self.pos = self.f.tell()
    def _reset(self):
        self.f.close()
        self.f = open(self.logfile, "r")
        self.pos = self.f.tell()
    def tail(self):        
        while 1:
            self.pos = self.f.tell()
            line = self.f.readline()
            if not line:
                if stat(self.logfile)[ST_SIZE] < self.pos:
                    self._reset()
                else:
                    time.sleep(1)
                    self.f.seek(self.pos)
            else:
                """print, return or otherwise manipulate
                the tailed line"""

                print line

Maybe it’s a too much overhead to check each time for file size, but you get the general idea.

So anyways here’s a usage exampe:

tail = LogTail("/var/log/messages")
tail.tail()

This will print out any new line appended to /var/log/messages file. If the file gets truncated or log rotated, class will detect it and will return to the start.

Firewalling xen bridge

Occasionally you will wish to block certain ports to your DomUs from Dom0. By default you wish to allow any traffic from and to DomU but for some security considerations, I found it to be wise to block some ports to and from my clients DomUs. One such port range is for example IRC. Although it can be routed trough alternate ports, most of those automated nasty malicious scripts use default ones. It’s quite handy to block them so they ain’t able to contact home.

As said by default Xen bridge is open for all traffics towards and from DomUs. It’s up to DomU admin to firewall their own virtual machine. Unfortunately some just forget to do the proper securing of the system, and as a result you get compromised DomU contacting various botnets, and executing all kind of nasty stuff.

To prevent this we can make a firewall rules in DomU that will by default block some traffic. Since I’m using bridged network firwalling must be done on bridge. I found this great article on shorewall manuals how  to setup bridged network firewall. I installed it and tested it on 32bit Centos 5.2 should work on any system though but I didn’t tested it on any other.
Read more

Secure synergy setup

Synergy is a nifty tool for cross platform clipboard, keyboard and mouse sharing. It’s reasonably easy to configure synergy server for use with multiple synergy clients. Doing so will spare you some time while working on multiple computers at your desk at once. I use it at office to connect my laptop’s and office computer mouse, keyboard and clipboard and thus reducing or completely eliminating need to lean over my laptop every time I need to use it. Anyway, most of the people use it with quicksynergy wrapper allowing even easier setup, but what the synergy lack is a means of authentication and security in data transfers. I’ll try to guide you how to make a secure synergy setup on untrusted networks.

So for a starter you will need to setup a synergy config file to use it with your synergy server.
While using a quicksynergy may be easier we won’t use it since it lacks some flexibility.

I’m using my laptop named blap and my office computer named kex. Blap is located to the left of kex so I will need a conf file looking like this:
Read more

Bypassing corporate firewall with reverse ssh port forwarding

Probably lots of you are behind some sort of very restrictive corporate firewall. Unable to access your office pc from home because of firewall policies. In normal cases this scenario is more than welcomed. No outsiders should be allowed to access internal parts of secure network! Ideally companies will setup secure VPN access thus allowing its employees to access their work computers and do some work remotely. What if you aren’t one of the lucky ones having such option? You desperately need to access your office pc?

The problem

current

Read more

Ubuntu Intrepid on Dell XPS M1530

By default my Dell xps M1530 came with preinstalled windows vista, Media direct and all those fancy stuff, naturally it all had to go away. For some time I used it with dual boot, and installed Dell Media direct. Naturally I used anything else then Ubuntu so rarely it didn’t make sense in keeping those stuff around no more. I decided to reformat everything and dedicate every last byte of available resources on my laptop to Ubuntu. While doing so I will try to keep a track in this post of everything that I do/install on my system so it would work perfectly with all available features.

Please note that following this guide from beginning to the end will most certainly erase all your data. So if you aren’t ready to make a full clean install please skip few first steps.

Step 1. Preparing for fresh install

1.A backing up existing data.

If you already used Ubuntu on your laptop and you actually want to reinstall your system but remain all current functionality you should backup your data.
Read more