How to use Macports version of gcc

OSX Mavericks only support gcc version 4.2.1. Macports, however supports gcc from 4.2 to 4.9. However, when you install gcc through Macports, your new version will not be enabled automatically. Here is a little trick:

sudo port select --list gcc
Available versions for gcc:
	mp-gcc44
	none (active)

The following command will enable your desired version:

sudo port select --set gcc mp-gcc44

You will need to do an extra step: Open a new Terminal and type hash -r to register the change with your system.

Happy coding!

Source: StackOverflow

How to install Oracle Database 12c on RedHat Enterprise Linux 6

IMHO, installing Oracle Database is not straight forward. There are lots of prerequisites packages and configurations that need to be installed and configured before you can actually install Oracle Database. Especially if you have a headless server that does not have any physical display attached to it, because you cannot install Oracle Database on a headless server without a response file (a response file is basically a configuration file that automate the installation process – however in order to create a response file, you will need another machine with display). Oh yes, I don’t have any other machine, and for god sake, I only want to install a single instance of Oracle Database, so why so complicated?

By the way, here is the full instruction that will help you install a single instance Oracle Database, straight forward from the beginning to the end.

A. Install prerequisites packages and configuration

Oracle has a very handy package called oracle-rdbms-server-12cR1-preinstall however it’s only available for Oracle Linux (yes, Oracle Linux). However, we can easily add yum repository to RHEL and install that package:

sudo wget http://public-yum.oracle.com/public-yum-ol6.repo -O /etc/yum.repos.d/public-yum-ol6.repo
sudo wget http://public-yum.oracle.com/RPM-GPG-KEY-oracle-ol6 -O /etc/pki/rpm-gpg/RPM-GPG-KEY-oracle

After this, installing prerequisites packages is straight forward:

yum install oracle-rdbms-server-12cR1-preinstall

B. Install Oracle Database

Since our server does not have any physical display attached to it, we need to create a virtual display and use X11 forwarding to setup Oracle Database. First step is to create virtual display

xorg-x11-server-Xvfb

Login under “oracle” account (this account should be created when we install prerequisites packages) and create a virtual display:

Xvfb :1 -screen 0 1280x1024x24

Fire another SSH session with parameter -X (to enable X11 forwarding) and login under “oracle” account. Then, download and extract 2 installation files into the same directory and run the Oracle Universal Installer

unzip linux.x64_11gR2_database_1of2.zip
unzip linux.x64_11gR2_database_2of2.zip
cd database
./runInstaller -ignorePrereq

Note that you will have to use -ignorePrereq parameter to bypass prerequisites check (since we’ve already installed it by the way)

OUI will open up a Installation window and things will be pretty straight forward from now on.

How to solve “Too many authentication failures for…” problem

Recently I am having trouble connecting to my home server from my laptop. Every time I connect, it said:

Received disconnect from my_ip: 2: Too many authentication failures for my_username

The strange thing is, connect from any other machine did not give me this problem. Is there anything wrong with my laptop? Even with multiple failures, my home server should reset login count sometimes.

Turn out the problem belongs to my SSH keys. I recently added 2 keys to my newly deployed servers. Each time I connect to my home server, my laptop will offer public key one by one, and apparently I have more private keys than the maximum number of retries, which is 6 on my home server.

Solution:

1. Explicitly specify the correct private key in host entry in the ~/.ssh/config file by adding IdentitiesOnly:

Host my_home_server_ip
  IdentityFile ~/.ssh/some_rsa_key
  IdentitiesOnly yes
  Port 22

2. Specify the correct private key:

ssh -i some_id_rsa -o 'IdentitiesOnly yes' username@my_home_server_ip

3. The last resort, disable Public Key Authentication completely:

ssh -o PubkeyAuthentication=no username@my_home_server_ip

Happy connecting!

Extract pages from PDF in Linux

I am not usually have to extract pages from a pdf file, but when I do, I used Adobe(R) Acrobat 7.0 that resides on my old Windows XP machine (believe me, it’s still work at this time). Today one friend asked me to extract a page from his pdf file, and I only have my MAC and Linux workstation with me. Fortunately, I figured out I can just use a single command to do this. Kudos to pdftk. For example:

pdftk A=inputfile.pdf cat A1-10 output outputfile.pdf

This command will extract page 1 to 10 from inputfile.pdf to outputfile.pdf

Very handy. Happy extracting!

How to install rtorrent and rutorrent

In order for rutorrent to work, rtorrent must be compiled with xmlrpc-c support. Here is the full instruction.

1. First, check to make sure the following libraries are installed on your system. At any step below, if you cannot run ./configure script correctly, you might have to install -dev or -devel packages of these libraries

  • gcc
  • ncurses
  • libsigc++-2.0
  • libssl
  • xmlrpc-c
  • subversion

2. Download and install xmlrpc-c if your distro does not support it

svn co http://xmlrpc-c.svn.sourceforge.net/svnroot/xmlrpc-c/advanced xmlrpc-c
cd xmlrpc-c
./configure
make
make install

3. Download and install libtorrent and rtorrent

wget http://libtorrent.rakshasa.no/downloads/libtorrent-0.13.3.tar.gz
wget http://libtorrent.rakshasa.no/downloads/rtorrent-0.9.3.tar.gz
tar xzfv libtorrent-0.13.3.tar.gz
tar xzfv rtorrent-0.9.3.tar.gz
cd libtorrent-0.13.3
./configure
make
make install
cd ../rtorrent-0.9.3/
./configure --with-xmlrpc-c=/usr/local/bin/xmlrpc-c-config
make
make install

4. Execute this line to copy rtorrent config to your home directory

cp /usr/share/doc/rtorrent/examples/rtorrent.rc .rtorrent.rc

5. Add the following line to the end of your .rtorrent.rc

scgi_port = 127.0.0.1:5001

6. Create download and session directories as pointed out in .rtorrent.rc. You might also change these directory as desired.

7. Download, extract rutorrent and plugins and placed them anywhere under your web root so that you can access it via HTTP

wget http://rutorrent.googlecode.com/files/rutorrent-3.5.tar.gz
wget http://rutorrent.googlecode.com/files/plugins-3.5.tar.gz
tar -xf rutorrent-3.5.tar.gz
cd rutorrent
tar -xf ../plugins-3.5.tar.gz
cd ..
mv rutorrent /var/www/

8. Delete /var/www/ruttorrent/.htaccess to disable htaccess authentication (you can always enable it in the future)

9. Fire up your web browser and open http://yourdomain.com/rutorrent. You will see the GUI very similar to uTorrent.

Happy torrenting. Download responsibly.

How to disable comment box from Facebook like widget

It have been a long time since the last time I used Facebook like widget on my blog. At that time, I feel that Facebook scripts take a lot of time to load, hence slowdown my site and affect its performance. Today, I changed my mind and add it back. With just 2 pieces of code (one load all.js at bottom and one load the like button), Facebook like button appears again on my site. Sweet and simple!

But wait. When I test by click on Like button, an ugly comment box appear:

As this is unexpected, I want to disable it. But Facebook Developer page does not give me any information about this. Fortunately, with just one simple CSS trick, I can hide that ugly comment box so the Facebook like become beautiful (like it should)

.fb_edge_widget_with_comment span.fb_edge_comment_widget iframe.fb_ltr {
    display: none !important;
}

Happy hacking!

How to find all files of a particular size in a particular directory

Last weekend, I worked on a Linux malware cleaning project. After 30 minutes of finding, I found 2 suspected scripts that are not included in Drupal by default. I soon realized that there are many of them, and I have a very limit access to his website (just normal account SSH access). Fortunately, after doing some research, I found that I can easily find all files of a particular size in his home directory. Simple like that: Suppose we want to find all files that has size = 1234 bytes and in /home/codepie directory, just type the following command

find /home/codepie -type f -size 1234c -exec ls {} \;

You might wonder what does ‘c’ in command mean. As units you can use:

  • b – for 512-byte blocks (this is the default if no suffix is used)
  • c – for bytes
  • w – for two-byte words
  • k – for Kilobytes (units of 1024 bytes)
  • M – for Megabytes (units of 1048576 bytes)
  • G – for Gigabytes (units of 1073741824 bytes)

Additionally, you can also search for bigger (+) or smaller (-) files. Just add a plus (+) or minus (-) for bigger and smaller files, respectively.

find /home/codepie -type f -size +1234c -exec ls {} \;

Have fun finding!

How to format bytes to kilobytes, megabytes, gigabytes in PHP

This is the best function I found on StackOverflow:

function formatBytes($bytes, $precision = 2) { 
    $units = array('B', 'KB', 'MB', 'GB', 'TB'); 

    $bytes = max($bytes, 0); 
    $pow = floor(($bytes ? log($bytes) : 0) / log(1024)); 
    $pow = min($pow, count($units) - 1); 

    // Uncomment one of the following alternatives
    // If 1 kilobytes = 1024 bytes
    // $bytes /= pow(1024, $pow);
    // If 1 kilobytes = 1000 bytes
    // $bytes /= (1 << (10 * $pow)); 

    return round($bytes, $precision) . ' ' . $units[$pow]; 
}

How to know exactly which Linux distribution you are using

I myself usually have to connect to a lot of Linux server to troubleshot. As you know, different Linux distribution has different config file location. For example, Apache config file in CentOS server is /etc/httpd/httpd.conf but in Ubuntu/Debian is /etc/apache2/apache2.conf

It would be easier for us if we know exactly the distribution name from the beginning. Here are several solutions:

1. From the boot time message

dmesg | head -1

You might see something like this:

Linux version 2.6.31-14-generic (buildd@crested) (gcc version 4.4.1 (Ubuntu 4.4.1-4ubuntu8) ) #48-Ubuntu SMP Fri Oct 16 14:05:01 UTC 2009

2. Using /proc/version

cat /proc/version

You might see something like this:

Linux version 2.6.31-14-generic (buildd@crested) (gcc version 4.4.1 (Ubuntu 4.4.1-4ubuntu8) ) #48-Ubuntu SMP Fri Oct 16 14:05:01 UTC 2009

3. Using /etc/issue

cat /etc/issue

You might see something like this:

Debian GNU/Linux 6.0 \n \l

4. Using /etc/*-release

cat /etc/*-release

You might see something like this:

Gentoo Base System release 1.12.9

Disclaimer: The results are different because I’m using different machine when testing. have fun :)