Friday, December 7, 2012

Shell scripting - sleep for milliseconds

Ever tried to make your shell script sleep for milliseconds? I tried, and failed miserably in doing so. I didnt know that decimal numbers are not allowed after the sleep command.

I found the following command that could be used for "virtually simulating" the millisecond sleep command:

typeset -i count=0;
while [[ ${count} -le 5700 ]];
do
      count=${count}+1
done

decrease the value 5700 to a value which would suit your needs. 5700 gives you ~1 sec time.


Tuesday, November 20, 2012

Setting up LAMP on Ubuntu 12.04

LAMP - Linux Apache MySQL PhP.

Most of the stuff mentioned here is inspired from :: https://help.ubuntu.com/community/ApacheMySQLPHP

first to install apache use the following:

1> sudo apt-get install apache2

restart/start apache2 services using the following command:

2>  sudo /etc/init.d/apache2 restart

This will restart apache services.

Check whether the apache2 services have started again by visiting http://localhost. You should get the message "It works!" on your browser. If it actually works, move to point 3. else:

reasons:

a. check /var/log/apache2/error.log.** where ** is the most recent(largest) value. you will get some kind of error message. Though it is self explanatory (eg. could not access some file) which means that there is a mode problem (you have to make /var/www/index.html 744).

b. error log is empty - problem with browser caching - try removing recent content (if you have tried logging into localhost many times, browser caches your page). To remove this clear your browser cache.

3> "It works!" page is located at /var/www/index.html. This is the default root hosting directory for linux. for multiple sites or your own website, you do not want to clutter your /var/www directory. Therefore it is suggested you change your default browsing directory.

How do I do that you say?

for every website , apache creates a config file in the directory
/etc/apache2/sites-available/

with the name of the website. Opening the configuration file, you will notice a xml file like this:



Saturday, September 8, 2012

Ubuntu 12.04LTS Bug - Close, minimize and Maximize button becomes invisible in GNOME

Recently, I faced this issue of application window not showing the top most bar - containing close, minimize and maximize button.

I was using Ubuntu 12.04 LTS with GNOME Desktop Theme. As usual the problem was with ccsm (CompizConfig Setup Manager).

All I had to do was to go to Application -> Settings ->Compizconfig Setup manager (just type ccsm on the shell.)

once that is done, unmark (if already marked) and then mark again the Effects -> Window manager tab.



Try opening a shell. I hope this time you see the close minimize and maximize buttons.

==Don==

Wednesday, September 5, 2012

DPRINTF Debug Macro in C

Extremely useful for Debugging projects in C.


#define DEBUG   // comment if you do not want the debug statments to appear.

#ifdef DEBUG
#define DPRINTF(fmt, ...) \
    do { printf("my_file: " fmt, ## __VA_ARGS__); } while (0)
#else
#define DPRINTF(fmt, ...) \
    do { } while (0)
#endif




Usage -> DPRINTF("This is a debug statement");

if you want the debug statements to appear, then define DEBUG
#define DPRINTF
at start of the code.

if, on the other hand, you do not wish the debug statements to appear, comment it.

you can add additional information to your debug statement, for example -

DPRINTF("%p:%d Debug statement\n",__func__,__LINE__);

this gives both the function name and the line number of the DPRINTF statement along with the message "Debug statement".



Sunday, September 2, 2012

Get real clock time in C to nanosecs approximation

struct timespec start,end;

clock_gettime (CLOCK_REALTIME, &start);
<add code here>
clock_gettime (CLOCK_REALTIME, &end);
float time= (end.tv_sec - start.tv_sec) + (end.tv_nsec - start.tv_nsec) / 1000000000;

This code snippet would give you the time taken to execute the added code in real time.

Dont forget to compile your code with a -lrt flag. Not including this flag could  give you a ld error for not finding clock_gettime function.

Thursday, August 9, 2012

How to Start a stopped process in Linux

While running a process in Linux, Sometimes we press Ctrl+Z unintentionally (or by choice). This makes the program STOP.

For example while running a GDB Session, I accidentally pressed Ctrl + Z, which stopped the gdb and the process came out of the gdb session, giving the Stopped message :


I wanted to continue (not restart) the session, for restarting, i couldve killed the process checking the pid and killing the process by its pid, but that would require me to start debugging my program all over again, which is a pain.

So, with a little help from google, i discovered that when we stop a process (using Ctrl +Z ), it basically goes into the background, and goes into sleep mode.

This can be seen from bg command

bash$ bg



Now to continue the program from its sleep mode, we need to bring it to the foreground.this can be done using fg command.

bash $fg <JOB_ID>


where JOB_ID is obtained using the command jobs , which matches process name to job#.

bash $jobs


Thats all! fg will bring your stopped command back to life! 


Sunday, July 22, 2012

Core dump file not found

Has it ever occurred to you that you ran a program, it aborted saying 'core dumped' , but then when you go around looking for the core dump file, you cant find it anywhere? 0-o

Well it happened to me today. I tried finding the 'core' file in each of the possible directories - the pwd, the executable directory, all the directories in the $PATH variable.

To my dismay, none of them had the core file in them.

After some google search, I found that there may be cases where the core file is not created to save space. (Core files are usually large ~1GB ).

I also found out that in my system (Ubuntu 12.04 LTS), the default size allocated to core files is 0.

This can be found using the command

ulimit -a


 If you see ulimit -c 0 , that means 0 blocks have been allocated to core dump file , and hence core dump file is not formed.

To enable the core dump, we can do the following :


ulimit -c unlimited

This increases the coredump size to unlimited. Now, try executing the code that caused Aborted (Core dump formed).

and check the pwd (present working directory) for 'core'  file. You will notice that core file is very large. And hence I feel that in Ubuntu, as  a protective measure, the core dump file has been allocated 0 blocks by default.

NExT up -> tips on how to use objdump for debugging through core file. :)

Wednesday, July 18, 2012

binary files diff and patch

I wanted to send this large sized data from a server to a client multiple times.
Though the change in data in every iteration was negligible, it was essential that the copy be made exactly similar on both client and server.

The time taken to transfer this data was huge. We had to decrease this. Hence it was required that only changes made in the file be diff 'ed  and this file be scp'ed over the client.

Also, at the client end, the file had to be patch'ed, so that the client had the exact same copy as the server.

Heres how we go about this:

1> Diff the file

diff old_file new_file > patch_file   

// old_file was created in previous iteration and new_file created in current iteration.

2> copy the patch file to the client

scp patch_file client@client_IP:patch_file



3> AT CLIENT END :: patch the file in the client

patch old_file patch_file > new_file

for more information please see man patch and man diff.

In order to do this for binary files, you need to install bsdiff.

for Ubuntu use

sudo apt-get install bsdiff


then do ::

bsdiff old_file new_file patch_file
scp patch_file client@clientip:
AT CLIENT END:
bspatch old_file patch_file > new_file.

NOTE :: Check whether the checksum of the newly created file is same as that of the previous file.
This can be done using cksum command.

If they are not the same, you can use jdiff (source code given here :: http://jojodiff.sourceforge.net/)

Also, note that jdiff and jpatch, the two binaries that can be used from the source code are compiled on 32 bit kernel. (use file command for checking the binary type)

For kernel version 3 and above, this will work perfectly fine. However if you plan to use jdiff and jpatch for machines with kernel level 2.6 or so, you will need to recompile it for 64 bit machine.

This regenerates jdiff and jpatch with 1-2 trivial error resolutions.

bsdiff  OR  jdiff, which one is better?

bsdiff takes more time but uses internal compression algorithm for creating small sized patch. It goes without saying the time taken to generate the patch is more.

jdiff on the other hand takes less time, generates larger patch, but gives you the gurantee that the final file after patch is exact replica of the original binary file (verified using cksum ). Also, it is meant for 32 bit OS. 

Wednesday, June 27, 2012

Analyse network traffic using tcpdump and tcpstat

The other day I was doing a set of system programming experiments which required me to generate graphs of network traffic per second between two hosts.

Though I knew wireshark was one reliable automated tool with a pretty GUI, I was in search of some better tool that could give me exact stats, rather exact distribution of the packets recieved per second by one host from the other host.

Searching over the net I tumbled upon '''tcpdump''', which has the capability to "sniff" packets over LAN or any localized network.

At first I tried running it by making it catch hold of all the packets that it could get a hand on. And that included network traffic of Office of the HOD of my college :P , and packets of some three or four other Professors. Though I didn't try and read the contents of the packets, i strongly believe tcpdump can be used to do that in verbose mode using vvv option. But thats something I'll try some other day.  

Well it was fun using it in amorous mode (yes thats the exact term given to listening to all the packets that are circulating in the network), but due to shortage of time and the perennial project advisor's pressure, I had to abstain myself from getting adventurous.

Comming back to tcpdump option - I used the following command to analyze the traffice between two hosts ::

tcpdump -f -n -S host $host1 and host $host2 -w file1.cap

This results in the generation of a binary file that can be read by tcpdump using the following command ::


tcpdump -r file1.cap

So far so good. But there was one problem. My intention was to give a comprehensive time varied traffic rate between two hosts. and tcpdump gave me a bunch of packets with their sizes. So creating a assorted list of packets and bandwidth usage per second would have required me to write a new script.

 This is when tcpstat came to the rescue. Using tcpstat, we can read the binary file (created above by the tcpdump command) and get a time sliced version of the network traffic using the following command

tcpstat -r file1.cap $timeslice

timeslice denotes the time scale on which packets are  analysed. for my purpose I set it to 5.

This blog in no means is comprehensive and I would strongly advise the reader to look at man page for tcpdump and tcpstat.

Notes::

How do we run tcpdump as a non-sudo user?

groupadd tcpdump
addgroup <username> tcpdump
chown root.tcpdump /usr/sbin/tcpdump
chmod 0750 tcpdump
setcap "CAP_NET_RAW+eip" /usr/sbin/tcpdump


==Don Jaffer==

Friday, June 8, 2012

UBUNTU ctrl key doesnt work on Vncviewer

its a gnome problem.

goto :  System -> Configuration tools ->  /desktop/gnome/peripherals/mouse/locate_pointer

unmark the checkbox. done.

Well for a strange reason sometimes this may not work.. So for that you need to install kde in Ubuntu.

This can be done using apt-get. No need to install kubuntu or anything.

Sunday, May 20, 2012

Passwordless ssh into a server

You have a machine to which you have to login all the time. and that includes entering your password to authenticate yourself to the server. Taxing! isint it??

A simple quirk is to make yourself "known" to the server so that it doesnt ask you to authenticate yourself everytime you login to the server...

2 steps , thats all you need baby!

* ssh-keygen
* ssh-copy-id <servername>

Consider Host A and Host B. Host A is your local computer and Host B is your Server.

'''On Host A'''
 ssh-keygen
this will create two keys id_rsa and id_rsa.pub in your local machine.

'''On Host A'''
ssh-copy-id mcs112578@palasi.cse.iitd.ac.in
this will copy the id_rsa.pub file into the authenticate keys. Done!

Now try ssh ing into the Server... see the difference !! (No password required !!)

==Don Jaffer==

Sunday, April 1, 2012

Linux Shortcuts : Open terminal - Maximize Terminal

<note>
I am mentioning the default keyboard shortcuts as available in Ubuntu, you can change it going into System > Preferences > Keyboard Shortcuts
</note>

Open the terminal using the shortcut
            Ctrl + Alt + T
Maximise by pressing the 3 keys in the following sequece (Note that timing and sequence is important here, you might take a little time to get a hang of it)

Press
             Alt + SpaceBar
one after the other in quick succession

Press
             'X'
after releasing Spacebar .



Thursday, March 29, 2012

Vim Command to split the screen

To Split the screen in vim:

open the file in vim
   vim abc.txt

to split the screen

ESC
:sp

This is the default way to split the screen and results in the screen being split horizontally.

ESC
:vsp

This is used to split the screen vertically.
 
to switch Control between two screens

Ctrl + W +W (Hit W twice)

Wednesday, March 21, 2012

FIREBUG - Addon of Firefox to View WebPage Source

I just started developing a webpage for my college project and was browsing through the net for some really good web interfaces.
I even came across quite a few web-pages, and tried to get the source code from the standard Tools>> WebDeveloper >> Source Code Menu.

Though getting the code was easy, getting which part of the code corresponded to which part if the webpage was very taxing.

After racking my brain through a number of websites, one of my friends suggested an awesome addon of firefox that can help you to understand the code of a webpage.

its called FIREBUG!!

All you need to do is add the Add-On to firefox, and click on whichever object's code you wish to see in the webpage.

The code corresponding to the tool gets displayed at the bottom of the page, and even if there is .css associated with the page, you can view that in the other page that is opened.

Ill elaborate on my findings after exploring more on Firebug.

Till then ... bbye!