SSH Tunneling for Ruby on Rails app

It is quite annoying to develop and test an app which is likely to receive data/notification from a third party site if your development computer isn’t globally reachable. You have to deploy to a server and test it and do this each time until it completely works. Examples of such cases include:

  • Facebook app development
  • PayPal or any other payment gateway integration

While there are probably several options, SSH Tunneling is the simplest, at least to me. It needs less than 2 minutes to configure. Let’s do it.

I’m assuming:

  • You’ve a dedicated server/VPS with root access or somehow access to sshd_config file (we’re going to call this remote server)
  • Your domain is dev.awesomeapp.com and which points to the above server with IP 198.198.115.47
  • You wanted the remote server to listen to 80 port
  • You’re running your rails server at port 3000 in your development computer (we gonna call this local server)

(You may have to change the values marked in italic font as your own settings)
Continue reading →

Auto updating (svn up) working copy on commit

I am working on a project that can’t be tested locally. So, I need to upload files to server. However, committing and then uploading is rigorous job (as there are lots of files to upload as a directory while changes were made only a few files under different directory. so browsing directory and uploading specific file(s) also annoying.) ! That’s why I used ‘svn up’ command to update all changed files from svn repository.

Continue reading →

Upgrading Lucid Lynx to Maverick Meerkat

Upgrading Ubuntu to Maverick Meerkat

Wow new Ubuntu, as said on the site perfect Ubuntu 10.10 (Maverick Meerkat) is now out :D. I could not resist myself from upgrading to it even I am using Ubuntu Lucid Lynx on Macbook Pro 7.1. I am upgrading specially to know whether it also works fine on Macbook.

Upgrading Ubuntu is always easy and fun. All you know that. However, if there are someone who does not know it, here it is. Just run the following command from terminal when you are online:

sudo update-manager -d

Then it should automatically detect that a new version is available. Just click on Upgrade button (show on the image above) when it appears. Follow instructions on screen and then sit relax while it is upgrading for you. However, carefully notice the alert (before main phase starts) it provides which includes the packages are going not to be supported anymore, packages that are going to be deleted etc.

Enjoy and pray for me so that it works fine on Macbook :P. I will write detail experience with it later.

I’ve installed Ubuntu on my Macbook Pro 7.1

Well, since buying the Macbook Pro, I was trying to install my most favorite operating system (Ubuntu/LinuxMint). I have failed with LinuxMint and other Ubuntu. However, this manual helped meto get Ubuntu Lucid Lynx installed on my Macbook Pro.

Before installation, I run Ubuntu live. After I’ve confirmed that basic things are working, I have started installation process. The installation was seamless though I was worried about the boot loader. Because I already have rEFIt installed. So, I was not sure if grub will overwrite it or not but it didn’t!. However, I am still wondering, as grub is loaded after I select Linux from rEFIt, will it work if I remove rEFIt? The grub is showing both Ubuntu and Mac Os. Continue reading →

Converting Youtube videos to MP3!

Yesterday I have downloaded two fantastic songs from Youtube. Later, I wanted to move them to my mobile as MP3. As the downloaded vidoes are in FLV format, I would need to convert them to MP3. I thought, no problem, I have ffmpeg install in my LinuxMint :).

I hit the following command:

Continue reading →

Passing filename arguments to PHP from shell and space issue…

Today, while working with a project to import thousands of articles from text files to wordpress,  I had faced a trouble while I am executing a shell script to iterate through all text files and call a PHP script with sending the filename as argument. The PHP script would then process the file as our requirement. The shell script looked like:

for i in `find . -type f -name '*.txt'`;

do

php import_posts.php $i

done

Here, $i would be the filename and it is passed to PHP script for processing. But the problem is that, when the filename (including directory name) consists any whitespace in its name, the script would broken. For example, some files path looked like
/home/user/articles/Internet Business/Web Hosting/how_to_start_your_company.txt

Here, notice the space after Internet and Web. The shell script was breaking just after Internet. It was sending /home/user/articles/Internet as argument to PHP script and obviously PHP script was failed to find this file.

After searching a lot, finally I found a solution.

It was really simple. I am quoting from his blog:

If none of your files or folders have spaces in their names, then this isn’t a problem, but that is increasingly unlikely nowadays.  If your songs do have spaces in their filenames, then the solution is to change how the shell splits that output.  This is controlled by the variable $IFS, which is set to spaces, tabs, and newlines by default.  We’d like it set to just newlines, so we do:

So, here is the solution. I just put the following line at the top of my shell script.

export IFS=$'\n'

After that everything is fine. The script is running exactly how I intended it to run.

Thanks for Anthony for his solution.

How to install AES Crypt in linux to encrypt and decrypt your files

AES Crypt is a simple tool to encrypt end decrypt your files. You can do it without being expert in either linux and/or cryptography. If you are simply familiar with linux shell, you have more than enough knowledge to use AES Crypt.

AES Crypt is a file encryption software product available on several operating systems that uses the industry standard Advanced Encryption Standard (AES) to easily and securely encrypt files.

However, you might have needed root privilege if AES Crypt is not installed in your computer and you want to install it. The installation is also as easy as pie. Just look below:

Installation

Visit download page of AES Crypt and copy the download link of AES Crypt source code for linux.

SSH to your server as root and run the following commands:

wget http://www.aescrypt.com/cgi-bin/download?file=v3/aescrypt305_source.tar.gz

tar -zxf download?file=v3%2Faescrypt305_source.tar.gz

Continue reading →

How to unzip multiple archives quickly?

If you have a lots of zips in your directory, you may be tired of unzipping them one by one. You should not do it manually. You can do it by a single command:

First time I have tried using:

unzip *.zip

However, it did not work. It gifted me error.

You can do it by any of the following methods:

Method One:

unzip ‘*.zip’

Method Two

for i in *.zip; do unzip $i; done

পাসওয়ার্ড ছাড়াই শেল লগিন

যাদের বার বার এসএসএইচ এ লগিন করতে হয় প্রত্যেকবার পাসওয়ার্ড দেয়া তাদের জন্য বেশ বিরক্তিকর। তাছাড়া সেটা যদি রুট হয় তাহলে তো কথাই নাই। কারণ সাধারণত রুট পাসওয়ার্ড বড় ও কঠিন হয়। সেজন্যই এর বিকল্পটি ব্যবহার করতে পারতেন যাতে আপনাকে আর প্রত্যেকবার পাসওয়ার্ড দিতে না হয়।

প্রথমে আপনার কম্পিউটারে টার্মিনাল চালু করুন। যদি আপনি সার্ভারে রুট হিসেবে ঢুকতে চান, তাহলে আপনার কম্পিউটারেও রুটে পরিবর্তন করে নিন।

$ sudo -i

এখন রুট পাসওয়ার্ড দিলে আপনি রুট প্রম্পট পাবেন।

# ssh-keygen
Generating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa):

যেভাবে আছে এন্টার দিন।

Enter passphrase (empty for no passphrase):
Enter same passphrase again:

খালি রেখেই দুবার এন্টার দিন। এখানে পাসওয়ার্ড দিলে আপনাকে প্রত্যেকবারই লগিনের সময় আবার এই পাসওয়ার্‌ডটি দেয়া লাগবে।

Your identification has been saved in /root/.ssh/id_rsa.
Your public key has been saved in /root/.ssh/id_rsa.pub.
The key fingerprint is:

দেখুন ‘পাবলিক কি’ কোন ফাইলে (এখানে id_rsa.pub) সেভ হয়েছে। ঐ ফাইলটি এখন আমাদেরকে সার্ভারে ট্রান্সফার করতে হবে।

# scp /root/.ssh/id_rsa.pub root@SERVER_IP:/root/.ssh/

SERVER_IP এর বদলে আপনার সার্ভারের আইপি বসান। এরপর পাসওয়ার্ড চাইবে। পাসওয়ার্ড দিন। ট্রান্সফার হয়ে গেলে এখন সার্ভারে আগের মতই লগিন করতে হবে। কিভাবে করতে হবে সেটা নিশ্চয়ই বলার দরকার নেই।

# ssh root@SERVER_IP

লগিন হওয়ার পর নিচের মত করে কমান্ড দিন।

# cd /root/.ssh/
# cat id_rsa.pub >> authorized_keys

ব্যাস! কাজ শেষ। এরপর লগ আউট করে আবার লগিন করলে আর পাসওয়ার্ড চাইবে না।

লক্ষণীয়:
১. ssh-keygen কমান্ডটি দুটো কি তৈরি করে। একটা পাবলিক এবং একটা প্রাইভেট। আমরা পাবলিকটি সার্ভারে আপলোড করেছি। কিন্তু প্রাইভেট কি আপনার কম্পিউটারেই আছে। এই প্রাইভেট কি যদি কেউ পেয়ে তাহলে সেও বিনা বাধায় আপনার সার্ভারে প্রবেশ করতে পারবে। সুতরাং এর নিরাপত্তা বিধান করা খুবই জরুরী।
২. authorized_keys এর পারমিশন সেট করুন এরকম:

# chmod 644 authorized_keys

যদিও শুরুতে বলেছিলাম বার বার পাসওয়ার্ড দেয়ার হাত থেকে রক্ষা পাওয়ার জন্য কিন্তু এটা সাধারণ অটোমোটেড কাজ করার ব্যবহার করা হয়। যেমন ধরুন একটা স্ক্রিপ্ট এর সাহায্যে আপনার ফাইলের ব্যাকআপ অন্য সার্ভারে রেগুলার আপলোড করবেন। সেক্ষেত্রে তো আপনার পাসওয়ার্ড দেয়ার সুযোগ থাকছে না।

৩. আপনি যে কোন সার্ভারে পাবলিক কি টা দেখানো পদ্ধতিতে রাখলেই সেখানে পাসওয়ার্ড ছাড়াই লগিন করতে পারবেন।

cPanel database administering class

It is long time since I have worked with cPanel. My last work was cPanel Email Management  script that enables us to create/delete email accounts from outside of cPanel.

Today I have worked with another cool thing. I have created similar class for database. Using this class, anyone can easily create/delete database and database users.

Current Features:

  1. Create database
  2. Create User
  3. Grant Privileges
  4. Delete Users -> Supports deleting multiple users at a time. See the example in test.php
  5. Delete Database -> Supports deleting multiple databases at a time. See the example in test.php
  6. Check if database exists
  7. Check if user exists

Requirements:

  1. cPanel server
  2. English as language of cPanel
  3. cURL

Please let me know your suggestion if you use this class.

Download V1.0

Special thanks to Sajjad Hossain for helping me during the development.

Update: 02-07-2009
– The class is approved in phpclasses.org. Click here to visit.