Saturday, November 02, 2013

OSX Maverick and Vmware fusion 4.1


 My copy of Vmware fusion 4.1 works with OSX maverick. 




It is quite understandable why vmware would spread FUD to sell more copies of newer versions of Fusion. However, we had already burnt the vmware support bridge when we had updated to 3.x kernels. Long live open-vm-tools!


Tcpflow Network traffic capture in three simple steps


Three simple steps to capture network traffic capture on your linux box


  • On the Debian Box, just install tcpflow using apt-get
  • $ifconfig to figure out the network interface (mine was eth0)
  • $sudo tcpflow -c -e -i eth0


Friday, August 16, 2013

HTTP Traffic monitor and capture tools

Here is a quick list of tools that can capture and monitor http traffic between your browser and server. yeah, I know wireshark exists. it is just that I still do not know how to use it!



  1. tcpflow - works at card level. Much better if you can script things on your own box.
  2. Fiddler - Nice tool but Windows only (who is going to download mono to install a http capture tool on linux box?)
  3. httpry - Not tried yet, on the list though!
  4. TCPCatcher - Would need Java. Downloadable as a stanadlone jar.
  5. Charles Proxy - commercial (50$)  but people have lot of praise for the tool.
  6. WebScarab - Our security team was using it. Ugly as hell but does the job.


Tuesday, May 14, 2013

Eight queen problem in Java

Here is a solution to eight queen puzzle in Java. You should note the following

  • This is a brute force solution for NxN board
  • The way we solve it is - first solve it for eight rook - i.e. first take care of horizontal and vertical lines only (the way a rook moves) and then omit the solutions having collision on diagonals 
  • To solve the N-rook problem we generate all possible permutations of N (corresponding to the fact that one queen occupies one column) - so this solution is not at all going to scale
  • The only way to learn anything in life is to do it yourself - even though this is a simple brute force solution or whatever - doing it gives me more pleasure than reading other's elegant solutions.

Next step - is probably to port this to javascript and generate the boards using HTML5 canvas. 



/*
 * 8-queen problem  using brute force searching
 * This solutions uses following strategies 
 *
 * 1 - fix one queen in one column and generate all
 * non-conflicting permutations - N! in total  
 * this is akin to solving the N-rook problem
 * 2- eliminate from N! permutations - that do not 
 * pass the additional diagonal test 
 * 
 * @author Rajeev Jha
 * @version 1.0
 *
 */

import java.util.Set;
import java.util.HashSet;

public class queen8 {

    private int[] columns ;
    private char[] colNames ;
    private int size ;
    private int solutions ;
    
    public queen8(int N) { 
        this.columns = new int[N] ; 
     for(int i=0 ; i < N ; i++) this.columns[i] = i+1 ;

        this.colNames = new char[N] ; 
        char a = 'A' ;
     for(int i=0 ; i < N ; i++) this.colNames[i] = (char) (a + i) ;

        this.size = N ;
        this.solutions = 0 ;
    }

    private void solve() {
        this.generate(this.size);
    }

    /* 
     * permutation generator using a backtracking algo
     * from http://www.cs.princeton.edu/~rs/talks/perms.pdf */

    private void generate(int N){
     int c ;
        /* factorial 1, 1! case,just one possibility,print that ..*/
        if ( N == 0 ) test_diagonal(); 
        //algorithm adjusted for zero-based indexes ..
        for(c = 0 ; c < N ; c++){
            swap(c,N-1);
            generate(N-1);
            swap(c,N-1);
        }
    
    } 

    /* swap for permutation */
    private void swap(int x, int y){
     int tmp = this.columns[x] ;
        this.columns[x] = this.columns[y] ;
        this.columns[y] = tmp ;
    
    }

    /* for position of a queen in a column (a permutation)
     * diagonal positions are given by moving 
     * one row up in next column and one row down in 
     * previous column */

    private void test_diagonal(){
        int x ;

     for(int i = 0 ; i < this.columns.length ; i++) {
            x = this.columns[i] ;
            for(int j = i+1, k = 1; j < this.columns.length ; j++, k++) {
                if((x+k) == this.columns[j]) return ;
                if((x-k) == this.columns[j]) return ;
            }
        }

        // diagonal test passed
        print_board();
    }

    private void print_board() {

     for(int i = 0 ; i < this.columns.length ; i++) {
            System.out.print(this.colNames[i]);
            System.out.print(this.columns[i] + " ");
        }

        System.out.println();
        this.solutions++ ;

    }

    private int getNumSolutions() {
        return this.solutions ;
    }
   
    public static void main(String[] args) throws Exception {
        queen8 board = new queen8(8);
        board.solve();
        System.out.println(" \n Total " + board.getNumSolutions() + " solutions " );

    }
}




And here are the solutions


rjha@mint13 ~/code/fun $ javac queen8.java 
rjha@mint13 ~/code/fun $ java -classpath . queen8 
A4 B2 C7 D3 E6 F8 G5 H1 
A5 B2 C4 D7 E3 F8 G6 H1 
A3 B5 C2 D8 E6 F4 G7 H1 
A3 B6 C4 D2 E8 F5 G7 H1 
A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
A6 B3 C1 D8 E5 F2 G4 H7 
A4 B2 C8 D6 E1 F3 G5 H7 
A6 B3 C5 D7 E1 F4 G2 H8 
A6 B4 C7 D1 E3 F5 G2 H8 
A4 B7 C5 D2 E6 F1 G3 H8 
A5 B7 C2 D6 E3 F1 G4 H8 
 
 Total 92 solutions 


Using wordpress export data with PHP simpleXML

I had a site running in word press. This was a 256 MB slice and WP 3.2+, I must say (in a relative sense of course) is not light on resources. So I decided to move this site to my own code. That also meant moving the word press data to my own schema. So I took an XML dump using word press export tool and imported it back using my own scripts that use PHP and SimpleXML.

XML from Wordpress export tool has namespaces and multiple elements of same name so I reckoned my skeleton script can be of use to someone. Here we try to grab the  title, publication date, link (permalink), categories, tags and content from original wordpress post.

The code follows



 
error_reporting(-1);
    libxml_use_internal_errors(true);

    function process_post($title,$category,$tags,$createdOn) {
        if(empty($content)) { return ; }
        // process post

    }


    // start:script 
    // wp.xml contains dump of wordpress posts

    if (file_exists('wp.xml')) {
        $doc = simplexml_load_file('wp.xml');

        if($doc === false) {
            echo "Failed loading XML\n";
            foreach(libxml_get_errors() as $error) {
                echo "\t", $error->message;
            }
        }

    } else {
        echo('Failed to open wp.xml.');
        exit ;
    }


    foreach($doc->channel->item as $item) {

        $title = $item->title ;
        // content and other elements can be  wrapped inside 
        // a separate namespace. To deal with such elements we 
        // use item->children on the namespace given in wp.xml 

        $ns_wp = $item->children("http://wordpress.org/export/1.1/");
        $attachment = $ns_wp->attachment_url ;

        if(empty($attachment)) {
            $ns_content = $item->children("http://purl.org/rss/1.0/modules/content/");
            $content =  (string) $ns_content->encoded;
            $link = $item->link ;

            $pubDate = $item->pubDate ;
            $createdOn = date("Y-m-d", strtotime($pubDate));

            $tags = "" ;
            $category = "" ;

            // tags and category
            // we can have multiple category elements inside an item

            foreach($item->category as $elemCategory) { 

                if(strcmp($elemCategory["domain"],"category") == 0 ) {
                    $category = $category." ".$elemCategory["nicename"] ;
                } 

                if(strcmp($elemCategory["domain"],"post_tag") == 0 ) {
                    $tags = $tags." ".$elemCategory["nicename"] ;
                } 
            }

            printf("title = %s, category = %s ,tags = %s , pub_date = %s  \n",$title,$category,$tags,$createdOn);
            process_post($title,$category,$tags,$createdOn);
        }

    }





Thursday, March 14, 2013

Export Nokia 5800 contacts to Gmail on macosx

So I have an old Nokia 5800 express phone and there is no Ovi suite for macosx. There is something called Nokia multimedia transfer app but there is no option to save contacts with that. Even Ovi was butt-ugly, I do not remember if I ever tried synchronizing my contacts with Ovi suite.

Now my phone is dying on me and I wanted to transfer all the contacts online to Gmail in a hassle free manner. Thankfully, I found the youlu app in symbian store. ( http://www.youlu.com )

youlu is quick, simple and easy. I installed it via Ovi store app on my phone. The download was around ~0.84MB. The installation was smooth. Afterwards,


  • you synchronize your contacts to youlu.com website.
  • Download contacts from youlu in csv format
  • Upload csv file to gmail


problem solved. Thanks youlu :)

Wednesday, February 27, 2013

KDE 4.10 looks nice and sophisticated


After reading rave reviews of KDE 4.10 I was tempted to install it. I had one mint 13 VM and some kind souls have already published instructions to pull the required packages from Kubuntu repository (all hail the mighty Internet :D )

The install was rather painless and 3D support also worked without a hitch :)  what surprised me most  is the fact that how polished the latest KDE desktop looks! ( I have rather fond memories of my KDE 1.0 desktop).



Polished is the right word. The only pain on eyes could be some strange looking icons.  Performance wise, it works fine  on my 1 GB VM.  There are some niggles that I would rather put to my limited RAM.

 I do not hate Gnome 3.6 with a passion but they have clearly taken a more tablet oriented  route while people on desktop have been left to work with ugly extensions.  The last panel extension I had was so ugly that finally I switched to tint2 for a panel + dock.

So It was a nice change to see an integrated nice looking dock and an icon for showing desktop.  Customization were easy and I was able to make whatever changes I wanted without reading manuals. So for a moment I am sticking to KDE as desktop of choice on Linux.



Tuesday, February 26, 2013

Options to run PHP scripts as daemons


PHP is mostly used as a front-end scripting language in a stateless fashion. What if you need to run a
PHP script like a daemon? An example use case would be a gearman worker or a PHP client for a message queue.

First, why is this an issue? You can just write a PHP program and start it from command line (that is what we will do in DEV env). However in real life this is an issue because the program started from command line
  •  + Should survive reboots
  •  + Should have ability to handle signals
  •  + Should spawn/kill workers depending on system load
What are our options? Let me list them below. My money would be on supervisord.

1) Write your own program that can do monitoring and spawn more child.
  •  + Example in PHP power programming
  •  + Example in Steven's Unix programming

Also see
The problems is that to pull it off you really need to be an accomplished Unix programmer.

2) using init.d standard scripts . init.d script would call a shell script that will start our PHP worker

Problems
  1.  - we have to understand / do infrastructure pieces
  2.  - graceful handling of signals
  3.  - restart when child dies

3) DJB's daemon tools at http://cr.yp.to/daemontools.html

people swear by it. However I have not used it.


4) nohup and screen technique - Run scripts in GNU screen and come out of SSH.

  1.  + issue: no supervision - job may do bad thing and die 
  2.  + some watch script is needed. Too flaky.

5) python daemons at http://pypi.python.org/pypi/python-daemon/

Also see sander marechal's script (python2)
http://www.jejik.com/articles/2007/02/a_simple_unix_linux_daemon_in_python/

6) Try upstart with respawn on Ubuntu
 + should work, atleast theoretically. That is how I was running php-fcgi daemon in Ubuntu Lucid.

7) Gearman Manager
PECL extension did not work for me. I have used PEAR version in my DEV enviroment.

7) Perp
http://b0llix.net/perp/

8) supervisord
http://supervisord.org/

@see also using gearman with supervisord
http://stackoverflow.com/questions/8217848/running-gearman-workers-in-the-background

9) libslack daemon (used by kestrel) - http://libslack.org/daemon/


10) Others

phpdeamon - http://phpdaemon.net/
Fat controller - http://fat-controller.sourceforge.net/getting-started.html

Daemonize - http://software.clapper.org/daemonize/
restartd - https://launchpad.net/ubuntu/+source/restartd/

PEAR system_deamon


PHP-daemon
https://github.com/shaneharter/PHP-Daemon

© Life of a third world developer
Maira Gall