Archive for January, 2010

Compare binary files with bash

January 15, 2010 3 comments

While I was working on a reverse-engineering project trying to find out the structure of a data file, I wrote this simple BASH script to do the job:

# script to compare two binary files
[ $# -lt 2 ] && echo "Usage: $0 file_a fileb" && exit 0
R1=`mktemp -u -p /tmp XXXXXXXX`
R2=`mktemp -u -p /tmp XXXXXXXX`
if [ -f $1 -a -f $2 ]; then
   hexdump $1 > $R1
   hexdump $2 > $R2
   diff $R1 $R2
   rm $R1 $R2

Duplicate (transfer) MySQL database between hosts

January 3, 2010 Leave a comment

A simple step:

On the source host, enter the following command:

mysqldump -u root -ppasswd1 dbname | ssh -p rmt_ssh_port rmt_usr@dsthost mysql -u root -ppassword2 dbname

[ Reference ]

Categories: mysql, Tip

My favorite VIM tips

January 2, 2010 Leave a comment

I love using vim as my text editor. It’s almost always the first program I would try to check if it’s installed on a newly installed Linux system. Below are some of the tips that I find quite useful and use quite often:

1. Moving cursor

gg – move cursor to the beginning of the file
G – move cursor to the end of the file
nG – move cursor to the nth line
Ctrl + o – move cursor to the previous cursor position

2. Multiple lines commenting

1) Shift + v to select the first line
2) move up or down arrow key to select more lines
3) :
4) status line will become :'<,’>
5) complete the line as below, followed by the Enter key

3. Multiple Lines deletion

Select and delete

1) shift + v to select the first line
2) move up or down arrow key to select more lines
3) hit letter d

Use direct command

:.,$d – Delete all lines starting from current line (inclusive) to the end of the file
:0,.d – Delete all lines starting from beginning of the file to current line (inclusive)
:1,3,5d – Delete lines 1, 3 and 5
:.,+Nd – Delete current line plus N lines below, N>=1
:.,-Nd – Delete current line plus N lines above, N>=1, you will be prompted ‘Backward range given, ok to swap (y/n)?‘ hit y to accept.

4. Typo correction

This is better explained by using the “the” example: say you want to enter the but instead you type teh. To correct the typo:
1) exit edit mode
2) move cursor to the letter e so it becomes teh
3) enter commands xp

5. Line swapping

Suppose you have the following two lines at the beginning of your bash script

echo "hello"

Obviously you want to swap those two lines so it becomes a valid bash script. Here’s how you do it:
1) make sure you are not in edit mode
2) move cursor to (anywhere) the first line
3) now enter commands ddp

6. File opening tips

vim file1 file2 … -o opens file1, file2, … in stacked windows
vim file1 file2 … -O opens file1, file2, … side by side
vim file + opens file and move the cursor to the last line.

That’s all for now. I will add more if I find more interesting enough to the above list. Of course there are a lot more useful tips using vim. As always I welcome comments and feedback.

Categories: Tip, vim

Generate ssl certificate in 1 quick step

January 1, 2010 1 comment

I found most instructions on configuring SSL under web servers are a bit too complicated, especially on the part that generates the self-signed certificate. I’ve discovered a pretty simply way to do it [ reference ]

openssl req -new -x509 -days 3650 -keyout key.pem -out cert.pem -newkey rsa:2048 -subj "/"

Substitute with the actual domain name, or even * if the certificate will be used in a virtual hosts setting.

Take apache for example, the resulting cert.pem and key.pem from the above commands can then be used in the ssl-enabled vhost configuration as below:

    SSLCertificateFile /path/to/cert.pem
    SSLCertificateKeyFile /path/to/key.pem

Starting the apache service will ask for pass phrase used to generate the above certificate. To remove the pass phrase, perform one more step:

openssl rsa -in key.pem -out key.pem

Categories: apache, ssl