Saturday, 18 October 2014

How to enable authentication in MongoDB?

I've been asked this question several times, it is simple indeed, but apparently it is causing some problems!
Here are some simple steps:
First fire mongo shell and create the user account

use admin
db.addUser('admin', '12345')
Then shutdown the server and exit:
db.shutdownServer()
exit
Then you need to start the service with --auth parameter or add auth = true entry to the config file (e.g. in /etc/mongodb.conf). You're done! To see how this works, you can lunch MongoDB shell in two way:

mongo localhost:27017/admin -u admin-p 123456

Notice the admin (which is the db name).
Alternatively you can run the shell and then authenticate there:


use admin
db.auth('admin','123456');

Also in your code, you can specify the username and password e.g. this way in your Python code:

conn = pymongo.MongoClient("mongodb://username:password@localhost:27017")

Wednesday, 20 August 2014

A very brief comparison of few Python tools for working with Excel files

As a part of a small Python script, it was required to export some data to Excel spreadsheet. I had a quick search to see what are the options and found these two packages:

  • xlutils (xlrd for reading and xlwt for writing): it was simple and straightforward to use it, but I encountered a couple of issues with it: if your output has more than 256 columns, then you're partially in trouble! This is a limitation of older Excel formats (xls), Excel 2003 and greater (with xlsx format), does not have that problem. But this package is not very happy with xlxs, e.g. it does not support keeping the format of xlxs files. So I'd recommend to try the next package.
  • openpyxl is more compatible with xlxs and does not have separate reader/writer classes and is simpler than xlutils to use. Keeping formatting and other issues with xlxs files are better supported here and ended up using this package.
As the title says, this is a very brief and most probably not too much accurate comparison of the two packages, but just wanted to share this short experience!

Saturday, 2 August 2014

Using OpenSSL in Windows and PowerShell

OpenSSL has no official binary distribution for Windows, however there is a semi-official version from Shining Light Productions (a compiled + installer version of the official OpenSSL which is free to download and use). It can be downloaded from this page. The very simple problem of using that binaries is that it can't find the config file (it tried to load it from /usr/local/ssl/openssl.cnf and fails with this message WARNING: can't open config file: /usr/local/ssl/openssl.cnf)
If you're using Windows' default shell (Command Prompt), then you can specify the config file manually by running this command: Alternatively if you're on PowerShell, run this command to add the correct path of the config file to the environment variable name OPENSSL_CONF:
No need to mention that you need to specify the correct path to the openssl.cfg file according to the installation path of OpenSSL.
An alternative solution is to edit the source code manually and change the default path of the config file and then compile it and so forth.

Wednesday, 28 May 2014

Disable automatic printer adding in Ubuntu

In Ubuntu there is a service which scans the network for printers and adds them automatically. If your computer is in a large network and you really do not use most of the network printers, but just a few of them, then this feature really becomes annoying. To disable this feature, the easiest way is to prevent the service from running automatically.
So first stop the service:
Then edit/add the new configuration file for the service:

Then put 'manual' in the new config file (the config file should have only a single line with single word: 'manual' - of course without quotes) and start the service again:

Saturday, 10 May 2014

How to mount a remote file system through an intermediate machine?

Suppose that you've a remote machine from which you want to mount a file system on your local machine. However, your local machine can not connect directly to the remote machine. If you could connect directly, then you could have used sshfs to mount the file system without any problem.
In the current situation all you need to do is to create a tunnel to a third machine which will act as the middle-man. The tunnel is created with ssh command, as shown below. Before that, for further clarification on this scenario, the machines which are involved are: localpoint (which is your current local machine), endpoint (which is you machine of interest that you want to mount a file system from it) and middlepoint (which is the machine in between that have access to both machines).
First you need to create the tunnel:

ssh -f username@middlepoint -L 111:endpoint:22 -N

which basically tells to forward the local port 111 to port 22 of endpoint machine. You can use any port on your local machine as far as it is available for use.
Then mounting is performed through the tunnel:

sshfs -p 111 username@127.0.0.1:/remote_path /local_path

That's it! You've now mounted endpoint:/remote_path on your local machine localpoint:/local_path via middlepoint.

Mac users may have some issues when actually trying to access the mounted file system, most probably 'Permission Denied' problems. That could happen when Mac tries to infer the permission. To resolve this issues, when mounting the fs, ensure to pass -defer_permissions as a parameter to sshfs. Also it would be beneficial to add noappledouble option as well to tell the Mac not to write the .DS_Store files on the remote file system. In short, the final call to sshfs would be something like:

sshfs -p 111 -defer_permissions,noappledouble username@127.0.0.1:/remote_path /local_path

Further note for Mac users: it is assumed that FUSE for OS X is being used.

Monday, 28 October 2013

How to add JAR library to the Android project in Android Studio?

Some times trivial things like adding a lib references becomes a big pain! Adding jar lib to an Android project in Android Studio on Mac was very problematic for me and of course, many other guys!
After checking different web sites and reading through a bunch of questions in stackoverflow, I managed to get it working.
The steps are:
Create a lib folder in your project folder (the inner one) and copy your jar file there.
Edit the build.gradle file (the inner one) and add: compile files ('lib/ks2.jar')
Close the project and open a terminal and go to the folder of your project. There you'll find a file named gradlew, change that to be executable (chmod +x gradlew), then execute it: ./gradlew clean
It would take a while to finish and you should see a 'BUILD SUCCESSFUL' message upon completion. Once done, open the project, and you're done!

Thursday, 10 October 2013

LaTeX: Make Bibliography appear in "Table of Contents"

Normally in report/book classes of documents, Bibliography will not appear in "Table of Contents", there are many workarounds for that; the easiest would be using the "tocbibind" package in preamble...