Thursday, March 5, 2015

Resize partiton on SD card

How to resize the partition on SDcard?
Scenario: You have written an image onto sdcard and that image is not using all the space on the disk
using gparted and parted.

# parted /dev/mmcblk0
- unit chs
- print
(This will show capacity of the sdcard in terms of cylinder-head-sector)
Max value is capcity -1 cylinder.

-- Sample --
$ sudo parted /dev/sdd
(parted) unit chs
(parted) print
Disk /dev/sdd: 121535,3,31
Sector size (logical/physical): 512B/512B
BIOS cylinder,head,sector geometry: 121536,4,32.  Each cylinder is 65.5kB.
Partition Table: msdos

Number  Start      End         Type     File system     Flags
 1      16,0,0     1215,3,31   primary  fat32           lba
 2      1232,0,0   26671,3,31  primary  ext4
 3      26688,0,0  29743,3,31  primary  linux-swap(v1)
-- Sample end --

then use "resizepart" command to resize the partition.

Optionally i can use gparted to do the same thing graphically.

Wednesday, February 18, 2015

Useful Commandline

To copy files preserving parents use --parent
$ cp --parents source destination

Saturday, May 24, 2014

SSH Agent Forwarding


Scenario

  • - Password login is disabled on both the servers Node1 and Node2
  • - Your public key is added to both the servers Node1 and Node2 when they were created. (for example using OpenStack)
  • - Firewall rules only allow access to Node1


Question
- How to login to Node2

Solution: Enable agent forwarding


  •  Add your ssh private key to ssh forwarding agent

 ssh-add -c  ~/.ssh/your_private_key

  •  ssh to Node1 by enabling agent forwarding, like so:

 ssh user@Node1 -A

  •  Once you login to Node1, you can login again to Node2

 ssh user@Node2

  •  You can combine both steps into 1 command like so

 ssh user@Node1 -A -t ssh user@Node2


Friday, January 3, 2014

VIM Chores

1. Read the output of a vim command (ex mode).
Say for example to list all the loaded plugins in vim

:redir @q 
:scriptnames
:redir end 

The output is now saved into q register. 
To paste from register q just type:

 "qp 

 The output is in vim now.

2. Read the output of a external command in vim
Say for example, i want to get the list of all processes in linux in vim

:read !ps -ef

and the processlist is now in the vim buffer.

Thursday, August 8, 2013

Automatically mount Netgear ReadyShare folder in linux (fedora) at boot

Here is the sweet little /etc/fstab entry that mounts the Netgear ReadyShare folder at boot time to a local folder on my linux desktop(Fedora 18!). Theoretically, it mounts when you first access the mount point.

//192.168.1.1/USB_Storage /readyShare cifs x-systemd.automount,guest,noperm,sec=ntlm 0 0

Tuesday, June 4, 2013

Reverse search bash command history

Ctrl + r => start reverse command search

Ctrl +r/s again  to move up and down the search list.

Example:

Suppose the command history contains the following command that start with "sudo"

10. sudo yum install gvim
......a bunch of other commands
20. sudo yum update -y
...other commands
30. sudo umount /mnt/disk1

Pressing "Ctrl+r" and typing "sudo" will find the last used command that matches "sudo" i.e line #30. To move up and down the list press "ctrl+r" and "ctrl+s"


Sunday, April 28, 2013

How to parse xml in python using etree

Like the previous example How to parse xml in python using minidom, let us use etree to parse xml this time. We will be doing the same 4 operations:

- Read the content of an xml tag
- Read attribute value
- Add a node
- Delete a node

And we will be using the same xml file named 'file.xml'

<?xml version="1.0" encoding="UTF-8" ?>
<menu>
  <food id="1">
    <name>Pesto Chicken Sandwich</name>
    <price>$7.50</price>
  </food>
  <food id="2">
    <name>Chipotle Chicken Pizza</name>
    <price>$12.00</price>
  </food>
  <food id="3">
    <name>Burrito</name>
    <price>$6.20</price>
  </food>
</menu>

Read the content of xml tag

from lxml import etree

doc = etree.parse('file.xml')
nodes = doc.findall('food/name')
for node in nodes:
    print node.text

Output:
Pesto Chicken Sandwich
Chipotle Chicken Pizza
Burrito

Read attribute value

from lxml import etree

doc = etree.parse('file.xml')
nodes = doc.findall('food')
for node in nodes:
    print node.attrib['id']

Output:
1
2
3

Add a node

from lxml import etree
from lxml.etree import SubElement

doc = etree.parse('file.xml')
nodes = doc.findall('food')
for node in nodes:
    rating = SubElement(node, 'rating', value='5')
    rating.text = 'Average'

ofile = open('newfile.xml','w')
ofile.write(etree.tostring(doc))
ofile.close()

This produces the following output
<?xml version="1.0" ?>
<menu>
  <food id="1">
    <name>Pesto Chicken Sandwich</name>
    <price>$7.50</price>
    <rating value="5">Average</rating>
   </food>
  <food id="2">
    <name>Chipotle Chicken Pizza</name>
    <price>$12.00</price>
    <rating value="5">Average</rating>
   </food>
  <food id="3">
    <name>Burrito</name>
    <price>$6.20</price>
    <rating value="5">Average</rating>
   </food>
</menu>

Delete a node
Let us delete all <rating> tags from the newfile.xml file.

from lxml import etree

doc = etree.parse('newfile.xml')
nodes = doc.findall('food/rating')
for node in nodes:
    parent = node.getparent()
    parent.remove(node)

doc.write('file.xml')

This produces the original file we started with!

Change byte ordering in bash shell - Convert from BGR to RGB format

Say for example BGR color code is BBGGRR; to convert it into RGB format in bash shell:

$ color="BBGGRR"
$ echo ${color:4:2}${color:2:2}${color:0:2}

The format is
${variable:index:length}

This thing could also be used to convert big-endian to little-endian formats.

Friday, April 26, 2013

How to parse xml in python using minidom

The most common things i find myself doing when working with xml files are the following.

- Read the content of an xml tag
- Read attribute value
- Add a node
- Delete a node

Lets see how to do this in python using minidom. For the purpose of this post, lets assume that the name of the file is "file.xml" with following content.

<?xml version="1.0" encoding="UTF-8" ?>
<menu>
  <food id="1">
    <name>Pesto Chicken Sandwich</name>
    <price>$7.50</price>
  </food>
  <food id="2">
    <name>Chipotle Chicken Pizza</name>
    <price>$12.00</price>
  </food>
  <food id="3">
    <name>Burrito</name>
    <price>$6.20</price>
  </food>
</menu>

Read the content of xml tag

from xml.dom import minidom

doc = minidom.parse('file.xml')
nodes = doc.getElementsByTagName('name')
for node in nodes:
    print node.firstChild.nodeValue

Output:
Pesto Chicken Sandwich
Chipotle Chicken Pizza
Burrito

Read attribute value

from xml.dom import minidom

doc = minidom.parse('file.xml')
nodes = doc.getElementsByTagName('food')
for node in nodes:
    if node.attributes.has_key('id'):
        print node.attributes['id'].value

Output:
1
2
3

Add a node
Lets add a <rating> node with default value 5 to each of the food item to know its popularity.

from xml.dom import minidom

doc = minidom.parse('file.xml')
nodes = doc.getElementsByTagName('food')
for node in nodes:
    rating = doc.createElement('rating')
    rating.setAttribute('value','5')
    text = doc.createTextNode('Average')
    rating.appendChild(text)
    node.appendChild(rating)

ofile = open('newfile.xml','w')
doc.writexml(ofile)
ofile.close()

Output: The resulting xml looks like:
<?xml version="1.0" ?>
<menu>
  <food id="1">
    <name>Pesto Chicken Sandwich</name>
    <price>$7.50</price>
    <rating value="5">Average</rating>
   </food>
  <food id="2">
    <name>Chipotle Chicken Pizza</name>
    <price>$12.00</price>
    <rating value="5">Average</rating>
   </food>
  <food id="3">
    <name>Burrito</name>
    <price>$6.20</price>
    <rating value="5">Average</rating>
   </food>
</menu>

Delete a node
Lets now delete the <rating> tag from the food item.

from xml.dom import minidom

doc = minidom.parse('file.xml')
nodes = doc.getElementsByTagName('rating')
for node in nodes:
    parent = node.parentNode
    parent.removeChild(node)

ofile = open('newfile.xml','w')
doc.writexml(ofile)
ofile.close()
             
This result is xml file similar to the one we started with.

Saturday, July 14, 2012

Batch rename files from lower case file name to upper case filename or vice versa!

Suppose you have a lot of image files in lower case name and you want to change rename them to upper case filename or vice-versa


for file in *.jpg; do mv $file $(echo $file | tr a-z A-Z); done

or from upper case to lower case

for file in *.JPG; do mv $file $(echo $file | tr A-Z a-z); done

If filenames contains spaces in them then just wrap the variable names in "", like

for file in *.jpg; do mv "$file" "$(echo $file | tr a-z A-Z)" ; done

Monday, July 2, 2012

Sudo in Fedora - How can I use sudo even when username is not found in /etc/sudoers file!

So, I was reading some stuff about sudo and /etc/sudoers file, that only users who are in the /etc/sudoers file are able to run sudo command; but when I looked into /etc/sudoers file on my system(Fedora 16) I could not find my username in there, but still I was able to use the sudo command. There was also a folder named /etc/sudoers.d but that was empty. So, how was I able to run the sudo command? After searching for somtime on the internet I came to know that the trick was the following line in /etc/sudoers file

%wheel ALL=(ALL)       ALL

This means give sudo access to all users in the wheel group, and when i looked up my group(using groups command) I indeed was part of the wheel group.

Friday, June 29, 2012

How to Record and Play macro in Visual Studio

Macro comes in very handy if you have to repeat certain operation a lot of times.

Ctrl + Shift + R => Start
Ctrl + Shift + R => Stop
Ctrl + Shift + P => Play

I recently learned about them when i needed to put breakpoint on all the calls to function(SendEvent) and inspect the event id. At first i was manually putting the breakpoint but then i realised that the file was like 3K lines of code. So, instead what i did was search for "SendEvent" once and then after that pressing F3 will move to next occurrence of the searched string. Then move to the start of the file. Press Ctrl+Shift+R to start recording macro, press F3 to get to first matched string and then press F9 to put the breakpoint.  Press Ctrl+Shift+R to stop recording. Then keep pressing Ctrl+Shift+P till the end of the file.

Thursday, February 9, 2012

Yum cannot resolve $releasever - Fedora

Yesterday I was installing some packages with yum, but then later decided to cancel the install pressing Ctrl+C, after that i started noticing this issue. Whenever i tried to install anything yum spilled the following error.

Could not parse metalink http://mirrors.fedoraproject.org/metalink?repo=fedora-$releasever&arch=x86_64 error was
No repomd file

Yum was not able to resolve the value of $releasever variable. It turns out that it gets to know that value from the version of "fedora-release" package installed on the system.
After i figured that out, the solution was simple, just reinstall "fedora-release" for your current system. Since currently i am using fedora 15, I issued the following command

yum install fedora-release --releasever=15

Notice, you need to tell yum to use release version of the current fedora release using --releasever

Tuesday, November 1, 2011

What's the problem

No matter how much you think you know C/C++ there are things that you can always miss if you are not thorough. This small program is one of them. Can you tell me how many times "Java Rules" is printed ?
#include <stdio.h>

  int main()
  {
   int counter = -1;
   
   while( counter < sizeof(int))
   {
     printf("Java Rules!! \n");
     counter++;
   }
    
   return 0;
  }
The answer is none, because the fact is Java doesn't Rule :P
Alright alright, the reason is sizeof operator returns unsigned int :)

Sunday, October 16, 2011

Removing packages not supported by the Repository

Here's a small tip for rpm based systems (like fedora, centos etc) to remove all the obsolete packages that are not supported by the current repository.

  • List all packages not supported by the repository.
    $ package-cleanup --orphans


  • One can use xargs with the above command to remove them.
    $ package-cleanup --orphans | xargs yum remove -y
    

Monday, October 3, 2011

Calling C++ library function from C code

Recently one of my friend asked this question. I had done this in past but I had to struggle to do this again. So I am documenting this in case i need this again in future.

Let's first create a C++ shared library containing the functions we need.
Since, I am not good with names so I'll call it the "Person" class.


Person.h
#ifndef __PERSON_H__
#define __PERSON_H__

void globalMethod();

class Person
{
 public:
 void instanceMethod();
 static void classMethod();
};

#endif


Person.cpp
#include "Person.h"
#include <iostream>
using namespace std;

void globalMethod()
{
 cout<<"Global method"<<endl;
}

void Person::instanceMethod()
{
 cout<<"Instance method"<<endl;
}

void Person::classMethod()
{
 cout<<"Class method"<<endl;
}


After creating the source files we need to compile them into shared object.

$ g++ -fPIC -c Person.cpp
$ g++ -shared -o libPerson.so Person.o
$


At this point we have our C++ shared library.
A simple client in C++ for this library looks like this.

Client.cpp
#include "Person.h"

int main()
{
 Person p;
 p.instanceMethod();
 Person::classMethod();
 globalMethod();
}


We can compile and run this small program as such. We need to export LD_LIBRARY_PATH before running the binary, so that the loader can find the shared object.

$ g++ Client.cpp -o Client -L. -lPerson
$ export LD_LIBRARY_PATH=`pwd`:$LD_LIBRARY_PATH
$ ./Client
Instance method
Class method
Global method
$


Till here we know that everything works.
Now, lets call these methods from C code. But, as we know that C++ does name mangling we cannot directly call these methods from the C code. We first have to write some kind of wrapper over the existing library using C naming convention so that the names are not mangled.

Here is what the wrapper class looks like

Wrapper.cpp
#include "Person.h"

extern "C" void GlobalMethod()
{
 globalMethod();
}

extern "C" void InstanceMethod(Person *p)
{
 p->instanceMethod();
}

extern "C" void ClassMethod()
{
 Person::classMethod();
}

Note the InstanceMethod() takes a pointer to Person object. Why ??? Remember the *this* pointer in C++; since this method will be called from C code, we need to manually pass this variable.

This will be our wrapper shared library over the libPerson.so library. lets compile and create the library.

$ g++ -fPIC -c Wrapper.cpp
$ g++ -shared -o libWrapper.so Wrapper.o
$


Now, its time to write our Client in C; it looks like this

Client.c
struct Person
{
};

int main(){
 GlobalMethod();
 struct Person p;
 InstanceMethod(p);
 ClassMethod();
}

Pay attention that we have to create a Struct for the Person class. We need to link this binary against the wrapper library (libWrapper.so) and the actual library(libPerson.so), since the Wrapper depends upon it.


$ gcc Client.c -o C_Client -L. -lWrapper -lPerson
$ ./C_Client
Global method
Instance method
Class method
$


And, we're done !!

Any comments/suggestions are welcomed !!

Saturday, September 3, 2011

Package management in Fedora (or RPM based systems)

The two tools/command used for package management in a RPM based system (like Fedora or Redhat) are:

a). rpm
 - Originally standing for "RedHat Package Manager"
 - Now a recursive acronym for "RPM Package Manager"
 - No pacakge dependencies resolution.

b). yum
 - It stands for "Yellodog Updater, Modified".
 - Re-write of existing tool called "Yellodog Updater (YUP), hence the name.
 - Supports package dependencies resolution.


1. Search for a package named "foobar"
    yum search foobar
    yum search all foobar

2. Install the development files for "foobar"
    yum install foobar-devel

3. Install documentation for "foobar"
    yum install foobar-doc

4. Check if package "foobar" is installed
    rpm -q foobar

5. List all the files installed by package "foobar"
    rpm -ql foobar
    rpm -q foobar | xargs rpm -ql

6. If you have a file say "/etc/foobar.conf" and want to know which package does it belongs to
    rpm -qf /etc/foobar.conf

Sunday, February 27, 2011

SSH Tunneling to overcome Firewall rules.....

A Typical network scenario in an Organisation
So, what is SSH Tunneling ?
When one network protocol(delivery protocol) encapsulates another protocol over itself, it's called Tunneling (Wikipedia)
And when the delivery protocol is SSH its called SSH Tunneling. Simple !!

Usage Scenario
Consider a typical scenario as shown above.
We have a web server called Atlantis and a SSH server called Endeavour.
For some reason(company policy ??) users are allowed access only to SSH server and not to the webserver. What if a user wants to access the webserver ?? Without tunneling he cannot access the webserver because port 80 is blocked by the firewall rules; so what's next ?

SSH tunneling can come to rescue in such situations.
We can Tunnel HTTP protocol (web server) over SSH protocol. So, how do we do it ?
Connect to SSH server as:

$ ssh Endeavour -L8080:Atlantis:80
$ ssh [ssh server] -L[local port]:[remote machine]:[port on remote machine]


The only thing to keep in mind is that the remote machine must be accessible by your ssh server. What this does is, it opens a local port 8080 and forwards all the traffic on that port to Atlantis on port 80 through your SSH server. The SSH server acts as the relay between your machine and the webserver.

To access the webserver, just point your browser to http://localhost:8080

Other Advantages
Since SSH is a secure protocol, meaning all the communication between your machine and the server is encrypted; it helps to transfer unencrypted traffic(http) over the network through secure channel(ssh).

Wednesday, February 23, 2011

Windows: Of 32-bit and 64-bit application registry

I recently ran into a problem where i was doing some development work and tried to read the value of a registry from under HKLM:software\microsoft\"Myprogram" using a C# program. The program kept failing and was not able to read it; it was not even able to open the said registry key. Initially, i thought that it might be a permissions problem, but NO !! To add to my frustation, i can clearly "see" the key being present in the registry.

After banging my head in the wall for some time and googling, i finally found that on 64 bit machines (where i a was doing my development work) there are 2 versions of the application registry. One for 32 bit applications to use and another version for 64 bit appications to use. regedit for 32-bit version resides under C:\Windows\System32 and 64-bit version resides under C:\Windows\SysWOW64.

The installer which wrote those registry keys was a 32 bit application; hence the registry entries were made to 32 bit version of the registry. The C# application that I was developing was targeting "x86" architecture. When I ran the application it always tried to open the 64 bit version of the registry and kept failing. I then changed the "platform target" in the project properties page from "x86" to "AnyCPU" and wholla!! problem solved :)

-----
Q. Why is 6 afraid of 7 ?
A. Because 7 8 9 :)
-----

Sunday, February 20, 2011

Transform Linux to Mac OS X

Before I proceed, let me first state that i LOVE the simplicity of my Linux desktop (Gnome to be precise). I am writing this post just to demonstrate that how easy it is to make your Linux desktop look like a Mac 

The two things that are characteristic of Mac are :
1. It's Dock at the bottom.
2. Global menu bar at the top.

There are many projects (like cairo dock, avant window navigator etc) out there that provide Mac like Dock for Linux. But the one that i find more useful and easy to configure is Docky. To find a list of dock applications go to http://en.wikipedia.org/wiki/List_of_dock_applications.

Since i am using Fedora, the details will be specific to it, but it would be somewhat similar in other distros as well.
Before we start, lets see how the default desktop looks like in fedora 14.

Default Desktop in Fedora 14

























Let's get started

1. Installing the Dock
Fedora uses yum as the package manager. So to install docky, open a terminal and execute
$ yum install docky

2. Installing Global Menu
Open up a terminal and execute
$ yum install gnome-applet-globalmenu

Post Install configuration
Delete bottom panel
At this point there will be no visible changes, because our installed programs are not running. We need to get rid of the bottom panel. To do this right click on empty space on the bottom panel to bring the context menu and click on "Delete This Panel". Similarly delete the custom menu bar in the top left corner.





Add to Panel
The next step is to add the main GNOME menu. This is similar to custom menu bar but does not have "Application", "Place" and "System" as shown in the custom menu bar. To do this right click on empty space on the top panel and click on "Add to Panel".








Add Global Menu to Top Panel
Similarly add Global menu to the top panel. If you cannot find this item, try logging out and login back.














Enable Docky from StartUp applications list.

The next step is to enable Docky from startup applications list. To do that execute $ gnome-session-properties from a terminal and select Docky from the list.










Change the wallpaper if you like. I have also installed gnome-color-icon theme. It provides different colored icon themes; i have selected purple one. To install, open a terminal and execute $ yum install gnome-colors-icon-theme

Log-off and login again. Here is the final desktop look.

Pseudo Mac
























----- Insanity is hereditary, you get it from your kids :) -----