Friday, 13 February 2015

Some useful scripts in my store (will keep updating this post from time to time):

-> print names of all files containing a pattern in current directory:

         for i in `grep -rl "PATTERN" *`;do echo $i; done

-> replace a pattern in all files in current directory:

         for i in `grep -rl "OLD_PATTERN" *`;do sed -i 's/OLD_PATTERN/NEW_PATTERN/' $i; done

-> replace all cpp style comments to c style comments :

            for i in `grep -rl "\/\/" *`;do cp $i $i.bak; sed -i '/\/\//s/.*/& \*\//' $i; sed -i 's/\/\//\/\* /' $i;done

Saturday, 20 April 2013

SOCAT EXAMPLES

Some useful SOCAT commands :

Introduction :
Socat is a command line based utility that establishes two bidirectional byte streams and transfers data between them. Because the streams can be constructed from a large set of different types of data sinks and sources (see address types), and because lots of address options may be applied to the streams, socat can be used for many different purposes.

In other words, socat sits in the middle of two different sources of IPC and passes data between them (also provides log features).

''psst'': SOCAT is an abbr. for "'''SO'''cket" and "'''CAT'''"


/*****************************************************************************/
socat for Simple file-transfer :

    On the server-side: socat TCP-LISTEN:port filename and
    To send file fro the server: socat TCP:hostname:port filename

    For eg. To transfer data from filename_1 of client to filename_2 of server
    (having ip 192.168.2.75) on port 7777.
    On the server-side:
        ./socat TCP-LISTEN:7777 ~/Desktop/filename_2
    To send file to the server:
        ./socat TCP:192.168.2.75:7777 ./filename_1

Similar commands for ipv6 :

    On the server-side:
        ./socat tcp6-l:7777 ~/Desktop/filename_2
    To send file to the server:
        ./socat tcp6:[fec0::1]:7777 ./filename_1


/*****************************************************************************/
socat for tcp to stdout :

    ./socat TCP-LISTEN:8083 stdout,crnl
    for ipv6 try : ./socat tcp6-l:8083 stdout,crnl

    This will print all the data incoming on tcp port 8083 on the terminal.
    To test this try sending a file using the above mentioned command or,
    simply open a web browser and type you ip adress with the port like,
    192.168.2.75:8083 (for ipv6 try '''[fec0::1]:8083''' ).
    the web browser send request to that port and all the incoming data
    will be displayed on the terminal.

/*****************************************************************************/
socat for serial to stdout :

    The below given commands route serial data to stdout:
   
    ./socat /dev/ttyUSB1,raw,echo 1,ispeed 9600,ospeed 9600 stdout
    ./socat /dev/ttyUSB1,raw,echo 0,ispeed 9600,ospeed 9600 stdout
    ./socat /dev/ttyUSB0,raw,echo 1,ispeed 9600,ospeed 9600 stdout

/*****************************************************************************/
socat for tcp port to serial port :

    ./socat TCP4-LISTEN:54322 /dev/ttyUSB1,raw,echo 0,crnl
    the above command establishes a two way connection between serial port /dev/ttyUSB0 and the tcp port 54322.
   
    similar command for ./socat tcp6-l:54322 /dev/ttyUSB1,raw,echo 0,crnl

    To test this try using the above command and send a file on the tcp port. The data will be seen on the serial terminal.
    Use a combination of above commands while for different cases.

    This command can be used to test with GTKTerm or other serial ports :
    ./socat TCP4-LISTEN:54322 /dev/ttyUSB1,raw,echo 0,ispeed 9600,ospeed 9600
   
    similar command for ipv6 : ./socat tcp6-l:54322 /dev/ttyUSB1,raw,echo 0,ispeed 9600,ospeed 9600

/*****************************************************************************/
socat with arm :

Pre-requisite :

arm toolchain should be previously installed.
On my desktop arm-none-linux-gnueabi-gcc is available, check on your system.
If not already present use '''ltib''' or other tools to install it.
Most importantly a linux system. (as I haven't tried out this with windows) :)


Steps to build socat for arm : (Similar can be used to compile it for any other embedded platform)

->  Create a folder named "socat" on your disc. I have created it on /home/sudan/work/. Further I have used this path as ref.
    changed this "/home/sudan/work" with your path.
->  Open a terminal and go to that directory

    # cd /home/sudan/work/socat

->  download the socat source code from "http://www.dest-unreach.org/socat/download/" or through command line

    # wget http://www.dest-unreach.org/socat/download/socat-1.7.1.2.tar.gz

->  extract the source code

    # tar -xvf socat-1.7.1.2.tar.gz

->  enter the extracted directory

    # cd socat-1.7.1.2/

->  Generate Makefile and config.h for arm.

    ./configure --host arm-none-linux-gnueabi

->  Patch required in config.h; open config.h :

    # vi config.h

and edit the following :

    if '''#define HAVE_LINUX_ERRQUEUE_H 1''' is not present then change '''/* #undef HAVE_LINUX_ERRQUEUE_H */''' to '''#define HAVE_LINUX_ERRQUEUE_H 1'''

    change '''/* #undef ISPEED_OFFSET */''' to '''#define ISPEED_OFFSET 13'''

->  save the file and the type '''make''' to compile.

->  after make is done a binary named socat will be generated; confirm that it is generated for arm by typing

    file ./socat

Its output should be :
   
    socat: ELF 32-bit LSB executable, ARM, version 1 (SYSV), dynamically linked (uses shared libs), for GNU/Linux 2.6.14, not stripped

->  then copy the file to your embedded board by scp

    scp ./socat root@192.168.2.71:/root/

Here 192.168.2.71 is the ip of ARM board.


/*****************************************************************************/
socat with ssl :

    ->  create two folders "server" (on server) and "client" (on client)

    ->  get generate_ssl_certificate.sh

    ->  go server folder

        cd /home/sudan/socat/server/

    ->  run the following command

        ./generate_ssl_certificate.sh server

    ->  check that three files should be generated in the foder server

        ./server.key
        ./server.pem
        ./server.crt

    ->  Now go to the client folder

        cd /home/sudan/socat/client (this should be on a different desktop for good testing, though it can be done on the same machine too)

    ->  run the following command

        ./generate_ssl_certificate.sh client

    ->  check that three files should be generated in the foder server

        ./client.key
        ./client.pem
        ./client.crt

    ->  Now copy the client.crt file to the "server" folder and server.crt file to the "client" folder.

    ->  To test this use the following commands:

        On server side :
        ./socat openssl-listen:4433,reuseaddr,cert /home/sudan/socat/server/server.pem,cafile /home/sudan/socat/server/client.crt stdio

        On client side :
        ./socat stdio openssl-connect:192.168.2.75:4433,cert /home/sudan/socat/client/client.pem,cafile /home/sudan/socat/client/server.crt


        After the above commands are typed a chat session can be established between server and client.


    ->  To route the port to serial port use the following command (works same as tcp command, just that this one is secure :) ) :

        ./socat openssl-listen:4433,reuseaddr,cert /server/server.pem,cafile /server/client.crt /dev/ttyUSB0,raw,echo 0,ispeed 9600,ospeed 9600,crnl

        ./socat stdio openssl-connect:192.168.2.75:4433,cert /client/client.pem,cafile /client/server.crt

/*****************************************************************************/
socat with pipes :

    To create fifo on command line :
        mkfifo /tmp/testfifo

    If the pipe "testfifo" is not already created then it is created.
    If already created you can delete it using command '''sudo rm /tmp/testfifo'''

    route usb0 to testfifo

        socat /tmp/testfifo /dev/ttyUSB0,raw,echo 0,ispeed 9600,ospeed 0


    to test this I connected two usb port using using usbto serial connectors
    open as fifo is writing into usb0 using socat we should see the output on usb1
    open serial terminal for ttyUSB1


    write on testfifo

        echo "WE COME IN PEACE" > /tmp/testfifo

    This output should be seen on serial terminal for usb1


    If we write on serial terminal usb1 then output can be seen if we do

        cat /tmp/testfifo

Tuesday, 26 February 2013

Macro Plugin for Gedit v3 (Ubuntu 11.10 and further)

->  Open terminal (press ctrl + alt + T if you are using unity interface).

->  Create a folder for gedit plugin.
     For that copy and paste these lines on the terminal:

        mkdir -p ~/.local/share/gedit/plugins

->  Download the macro files from :

        https://github.com/eguaio/gedit-macropy

->  Copy and paste the two files : "macropy.py" and "macropy.plugin" in
      ~/.local/share/gedit/plugins .

->  Restart gedit by closing all gedit instances and opening a new instance.

->  Go to "Edit -> Preferences -> Plugins"

->  Scroll down and find the entry for "Macropy"

->  Check the entry (click on the square box to select) and click on close.

The macro plugin has been successfully installed now. You will see three icons to "start recording macro", "stop recording macro", and "playback the recorded macro"

Hello World

My first blog. I will be posting things that I have learned till now.