Wednesday, May 7, 2014

Virtual Box: Sharing folders with Windows 8.1 host and Ubuntu guest

I like to do my development on Linux platform, though I love to have my Windows do all the front-end.
For this I run multiple Virtual Boxes with Ubuntu terminal on it. One of the main problems, I faced was sharing of code between the different Virtual Boxes.

To do this, I have shared my Windows user/<user-name>/documents/ directory.

Doing this was not trivial and I have spent many hours trying to fix this.

Step 1:
Share the folder on VBox Settings

Step 2:
You need to install VBox-guest-additions.
This is the place I had the maximum problem.


This should be straight forward but it was not for some reason.
Step 2-a:  This command should have worked
/media/cdrom/VBoxGuestAdditions/BoxLinuxAdditions.run

However no matter what I did, it was not able to detect the CD.

So I looked at alternative steps.
Step 2-b:
sudo apt-get install virtualbox-guest-additions-iso

To check this, we should find that the files are in /opt/VBoxGuestAdditions-4.3.10.
But these files were not there even though the package was showing installed. Tried multiple times to re-install this but no luck.

Step 2-c:
However what this did was that the virtualbox itself got installed on my instance.
With this, I could now find the guestAddition CD on instance itself. So I mounted this
mount -o loop /usr/share/virtualbox/VBoxGuestAdditions.iso  /mnt/vbox/

With this, I could now go to
/mnt/vbox/VBoxLinuxAdditions.run
/mnt/vbox/VBoxLinuxAdditions.run
and thankfully this worked.

Step 3
Mount the disk
sharename="whatever.you.want.to.call.it"; 
sudo mkdir /mnt/$sharename
sudo chmod 777 /mnt/$sharename
sudo mount -t vboxsf -o uid=1000,gid=1000 $sharename /mnt/$sharename
ln -s /mnt/$sharename $HOME/Desktop/$sharename

However I still found a problem and this was
mount: wrong fs type, bad option, bad superblock on hostshare, missing codepage or helper program, or other error (for several filesystems (e.g. nfs, cifs) you might need a /sbin/mount.<type> helper program) In some cases useful info is found in syslog - try dmesg | tail  or so

The problem was that the mount.vboxsf was not linked properly.
ln -s /opt/VBoxGuestAdditions-4.3.10/lib/VBoxGuestAdditions/mount.vboxsf /usr/sbin/mount.vboxsf
ln -s /opt/VBoxGuestAdditions-4.3.10/lib/VBoxGuestAdditions/mount.vboxsf /usr/sbin/mount.vboxsf

Finally the mount worked.

Step 4: Automount.
In final step, go to
vi /etc/rc.local
sudo mount -t vboxsf -o uid=1000,gid=1000 documents /mnt/<sharename>


Some useful links
https://help.ubuntu.com/community/VirtualBox/SharedFolders
https://help.ubuntu.com/community/VirtualBox/GuestAdditions
http://superuser.com/questions/736024/cannot-share-host-directory-with-virtualbox-guest-mint-16-64-bit

Friday, January 31, 2014

Protect your server with Fail2Ban

Fail2Ban is one of the must have security features for your server. It is a nifty peice of code, which detects annomalies from the log files and bans IP address for a period o time.

However I do not like users trying to access my server and if they break a rule, I would liked them banned forever. Fail2Ban does this but at system reset or reboot these IP addresses are lost.

But this can be easily fixed by saving these files.
In my server I have added these lines to  /etc/fail2ban/filter.d/iptables.conf

# Option:  actionstart
# Notes.:  command executed once at the start of Fail2Ban.
# Values:  CMD
#
actionstart = iptables -N fail2ban-<name>
              iptables -A fail2ban-<name> -j RETURN
              iptables -I INPUT -p <protocol> --dport <port> -j fail2ban-<name>
              cat /opt/ip.blacklist-<name> | while read IP; do iptables -I fail2ban-<name> 1 -s $IP -j DROP; done

# Option:  actionban
# Notes.:  command executed when banning an IP. Take care that the
#          command is executed with Fail2Ban user rights.
# Tags:    <ip>  IP address
#          <failures>  number of failures
#          <time>  unix timestamp of the ban time
# Values:  CMD
#
actionban = iptables -I fail2ban-<name> 1 -s <ip> -j DROP
            echo '<ip>' >> /opt/ip.blacklist-<name>

Sunday, January 19, 2014

Changing Windows Wifi key for the multiple machines in Network

In one of the places I have been working, the IT team was using the Network Wifi WPA Key for a couple of years. The biggest problem was that there were perhaps hundreds of devices connected on Wifi but no central way of managing these centrally. At the end they decided to bite the bullet and changed the keys.

Now as people started coming to office, the IT department was supposed to set the key instead of giving it away. In Windows 8, MS has removed the feature if "Managing Wireless networks". So to give a new Key, it would take some time.

To make things simple, we found out a very elegant solution which works for Windows 7 and 8.

Step 1: Set one Laptop with the Wifi key manualy
Step 2: Export the Key
netsh wlan export profile key=clear

Step 3: Write a simple batch file that would delete the old profile and set the new one.
upgrade.bat
netsh wlan delete profile name=[profile-name]
netsh wlan add profile filename="WiFi-[profile-name].xml"
netsh wlan connect [profilename]

Copy the profile-file and the batch file into a USB key and simply run it on every machine.

I am sure this could be extended to OSX and Linux but will update you if I find out.

Saturday, January 18, 2014

Working with Google Spreadsheet API and PHP Arrays.

Over the last few years, I have been writting scipts for a lot of functionality. I often find it a pain to use a database. Most often the database are not even databases in true sense but really a table I need to store and refer to.  The tables are not more than 40-50 rows and never exceed few hundreds rows. It was usually accessed from one program at a time and did not need concurrancy. Using a database is like trying to kill a mosquito with a bazooka :-). 

In past I have used simple CSV files that I have stored in my local file system. The problem with using a CSV file on the local or remote server is that it becomes very difficult to edit manually or een view it. (I am old fashinoned and like coding on command line most of the times)

Over the last couple of projects I have found using GoogleDocs Spreadseheet as very useful.
Advantage are
1. Kept on the cloud and hence always available
2. Built in Version control :-)
3. Can be editted and viewed on UI
4. Easy for the layman to see and work on it.

This can come especially useful in cases like when you are trying to store a list of users who have filled up the form etc. However looking at Google Data API was a pain. There was no direct way of easily accessing the data. What I needed was that I should be
a) Able to load the complete data into my memory
b) After doing the manipulation, be able to write it back
c) Able to open multiple documents at once

Here is a simple API. Hope this helps and let me know if there is something I can do to improve
(You will need to install zend-framework which is very simple on ubuntu sudo apt-get install zend-framework
and may need to remove a comment from the line on /etc/php5/conf.d/zend-framework.ini )

gSSClass.php
<?php

/**
 * @see Zend_Loader
 */
require_once 'Zend/Loader.php';

/**
 * @see Zend_Gdata
 */
Zend_Loader::loadClass('Zend_Gdata');

/**
 * @see Zend_Gdata_ClientLogin
 */
Zend_Loader::loadClass('Zend_Gdata_ClientLogin');

/**
 * @see Zend_Gdata_Spreadsheets
 */
Zend_Loader::loadClass('Zend_Gdata_Spreadsheets');

/**
 * @see Zend_Gdata_App_AuthException
 */
Zend_Loader::loadClass('Zend_Gdata_App_AuthException');

/**
 * @see Zend_Http_Client
 */
Zend_Loader::loadClass('Zend_Http_Client');

class gSS {

private $email = "****@*********.com";
private $password = "***************";

public function __construct($gDocName,$sheet) {

try {
 $client = Zend_Gdata_ClientLogin::getHttpClient($this->email, $this->password,
Zend_Gdata_Spreadsheets::AUTH_SERVICE_NAME);
} catch (Zend_Gdata_App_AuthException $ae) {
 exit("Error: ". $ae->getMessage() ."\nCredentials provided were email: [$email] and password [$password].\n");
}
$this->gdClient = new Zend_Gdata_Spreadsheets($client);
// Find spreadsheet Key
$feed = $this->gdClient->getSpreadsheetFeed();
//var_dump($feed); exit;
$i =0;
foreach($feed->entries as $entry) {
if ($entry->title->text == $gDocName) break;
$i++;
}
if ($feed->entries[$i]->title->text == $gDocName) {
$currKey = explode('/', $feed->entries[$i]->id->text);
$this->ssKey = $currKey[5];
} else {
exit("Error: ". $ae->getMessage() ."Document $gDocName not found\n");
}
//Assuming that worksheet will be always 0
$query = new Zend_Gdata_Spreadsheets_DocumentQuery();
$query->setSpreadsheetKey($this->ssKey);
$feed = $this->gdClient->getWorksheetFeed($query);
$currWkshtId = explode('/', $feed->entries[$sheet]->id->text);
$this->ssWkshtId = $currWkshtId[8];
}
public function gSSGetRows() {
//Get Data
$query = new Zend_Gdata_Spreadsheets_ListQuery();
$query->setSpreadsheetKey($this->ssKey);
$query->setWorksheetId($this->ssWkshtId);
$dataFeed = $this->gdClient->getListFeed($query);
// var_dump($dataFeed);exit;
$i = 0;
foreach($dataFeed->entries as $entry) {
if ($entry instanceof Zend_Gdata_Spreadsheets_ListEntry) {
$rowData = $entry->getCustom();
foreach($rowData as $customEntry) {
 $rows[$i][$customEntry->getColumnName()] = $customEntry->getText();
}
$i++;
}
//var_dump($rows);
return $rows;
}

public function gSSSetRow($index,$row) {
echo "gSSSetData\n";
//Get Data
$query = new Zend_Gdata_Spreadsheets_ListQuery();
$query->setSpreadsheetKey($this->ssKey);
$query->setWorksheetId($this->ssWkshtId);
$dataFeed = $this->gdClient->getListFeed($query);
{
$entry=$this->gdClient->updateRow($dataFeed->entries[$index], $row);
if ($entry instanceof Zend_Gdata_Spreadsheets_ListEntry) {
//echo "Success! \n"; 
$entry->save();
}
return $row;
}

public function gSSSetRows($rows) {
echo "gSSSetData\n";
//Get Data
$query = new Zend_Gdata_Spreadsheets_ListQuery();
$query->setSpreadsheetKey($this->ssKey);
$query->setWorksheetId($this->ssWkshtId);
$dataFeed = $this->gdClient->getListFeed($query);
// var_dump($dataFeed);exit;
$i = 0;
foreach($rows as $row) {
var_dump($row);
$entry=$this->gdClient->updateRow($dataFeed->entries[$i], $row);
if ($entry instanceof Zend_Gdata_Spreadsheets_ListEntry) {
echo "Success! \n"; 
$entry->save();
$i++;
}
//var_dump($rows);
return $rows;
}

public function gSSInsertRow($row) {

var_dump($row);
$entry = $this->gdClient->insertRow($row,$this->ssKey,$this->ssWkshtId);
if ($entry instanceof Zend_Gdata_Spreadsheets_SpreadsheetsEntry) {
echo "Success! \n"; 
$entry->save();
}
}
}

/*
$ss = new gSS("Interval Log",0);
$rows = $ss->gSSGetRows();
$ss->gSSSetData($rows);
*/
?>

Sunday, September 15, 2013

Python API for beginners

Python is most probably the most elegant language to do quick programming.

For all those who are starting to learn Python, look at this
http://www.pythonforbeginners.com/development/list-of-python-apis/

Thursday, April 25, 2013

Starting with Lumina 520 and windows 8

So I finally took the plunge. Got the new Lumia 520 with Windows 8. (In past I have had, blackberry,  iPhone 1.0 and Samsung Galaxy).  So this is my 4th Mobile OS in the few years.

I have never liked the blackberry and found it very tedious to use. iPhone and iOS when it came out was a revolutionary. People had to get and and rethink about mobile phones. I remember when I got it, I had significant opposition from many folks in my company (who kept advising me to get a Blackberry) and even after I used it, would keep comparing their BB to my iPhone. In today's date, I do not think there is any debate about who won the argument there.

After 4 years of iPhone I finally moved to Android. For one-third the price, I got better hardware than the iPhone had. But the OS (Android Gingerbread) was far from seamless experience you had with iPhone. Yes, most of the iPhone type of feature were there but it never came close to the UX of iPhone. The Android still functioned as a set of independent applications, while iOS worked as one device.

In the last 6 months, I have now seen the new Android Jelly bean (Micromax A110) and the iOS 6.1. (iPad 3.0). The difference of the two OS has vastly reduced. Android still functioned as a independent applications but the number of applications were much more seamless. On the other hand the number of free applications had increased significantly and almost anything you needed, you could find a free application. Lot of applications were using open-standards and hence it made interfacing with multiple devices much easier. On the other hand, the iPad experience was not as great. Basically, there were some new applications and feature from the time off iOS 1.0 but nothing significantly had changed. There was nothing really to talk home about. Yes, the number of iOS apps were huge but almost all them had a crappy lite version for free and for anything else you needed to pay.

So, when it came time to buy a new phone, I decided to go for Lumia 520 with Windows 8.0.
I always believed that Microsoft was a software powerhouse and it best fights with its back against the wall. However over the last 10 years the market dominance made it arrogant and it was more concerned with keeping its market share than building disruptive technology. Nokia on the other hand was a great hardware company. Their initial software was also great but they choose the wrong legacy Symbian OS.  Again with them also the market dominance had made their heads spin. I had a great friend in Nokia Product Management when I got the iPhone. I remember him trashing the iPhone with not enough features, not the right software, with limited form-factor etc. At the end none of them used the iPhone and were so caught up in their own Nokia world, they too forgot that the paradigms could change.

However when these two giants with their back against the mountain joined their hands, I was very hopeful about the products they will come out with. And truely I am not disappointed.

(More to come later)

Wednesday, October 5, 2011

Mangento exporting SQL issue

While exporting mySQL if you every get into the problem]


ERROR 1064 (42000) at line 688: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'USING BTREE,
  KEY `FK_ATTRIBUTE_VARCHAR_ENTITY` (`entity_id`),
  KEY `FK_CATALO' at line 9

and the mySQL is 
UNIQUE KEY `IDX_BASE` (`entity_type_id`,`entity_id`,`attribute_id`,`store_id`) USING BTREE

You need to replace all such instance by 

UNIQUE KEY `IDX_BASE` USING BTREE(`entity_type_id`,`entity_id`,`attribute_id`,`store_id`)