tech

Send mail from command line from specific email address

in

In an email, from is set in the header. To set FROM in any email sent through the mail command, here is the syntax

echo 'Mail body' | \
mail -s 'this is the email subject' -a 'From: sender@example.com' receiver@example.com

MySQL command line backup and restore

A lot of beginners are very much dependent upon phpMyAdmin for their daily work. However a lot of them are not familiar with the basic command line tool like mysql and mysqldump. These two commands can help a lot when I am working on servers without phpMyAdmin installation.

I will show you how to quickly backup and restore database between your local computer and server.

We have a database called dbproject_local which I will migrate to server. The server already has database dbproject_server.

Using custom php.ini in a cPanel server

Recently in our office at YIPL, we needed to increase the upload_max_filesize and post_max_size of one of our clients. We run on a virtual private server with WHM and cPanel.

The default php runs as a FastCGI with suexec enabled.

Here is how I configured apache to run custom php.ini.

1. Know where your existing php.ini file exists.

Just create a phpinfo.php file in your public_html or www folder. Use the phpinfo() function know the current location of default php.ini.

/home/username/public_html/phpinfo.php

<?php
phpinfo();
?>

Access the file with http://www.sitename.com/phpinfo.php.

2. Copy the file inside your cgi-bin folder

$ cp /usr/local/lib/php.ini /home/username/public_html/cgi-bin

3. Create a custom cgi executable to run php.

$ vim /home/username/public_html/php.cgi

Add following lines

#!/bin/sh
/usr/local/cpanel/cgi-sys/php5 -c /home/username/public_html/cgi-bin/
# Note, the parameter after -c is the location of new php.ini

Allow executable permission to the new php executable

$ chmod +x /home/username/public_html/cgi-bin/php.cgi

4. Modify .htaccess to run php with our new executable by adding the following lines at the top

$ head /home/username/public_html/.htaccess
Action application/x-httpd-php5 /cgi-bin/php.cgi

#.. other commands

5. Make sure new configuration is loaded.

The http://www.sitename.com/phpinfo.php file should now show /home/username/public_html/cgi-bin/php.ini as the loaded setting file.

6. Remove phpinfo.php for security purposes

$ rm /home/username/www/phpinfo.php

References

Periodic monitoring connection to certain port of certain ip address

in

While running a kannel server, I need to make sure certain connections to telcos are up. And here is a simple technique taiyal from ##linux suggested me. It is pretty simple though.

$ netcat -z -w 2 192.168.10.1 5020 -v -n

Netcat is a very simple utility command to connect to a certain ip at certain port. The parameters are as follows.

-z              zero-I/O mode [used for scanning]
-w 2            timeout for connects and final net reads
                in our case try to connect for just 2 seconds
-v              verbose [use twice to be more verbose]
-n              numeric-only IP addresses, no DNS
                No need for dns resolution

Add this to a cron and redirect the output to log file and we're set.

Here is my crontab

$ crontab -e

# m h  dom mon dow   command
*/5 * * * * /home/monitor/bin/checksmsc.sh

And here is my checksmsc.sh code

$ cat /home/monitor/bin/checksmsc.sh
#! /bin/bash
datetime=`date`
log=`netcat -z -w 2 192.168.10.1 5020 -vn 2>&1`
echo $datetime $log >> /home/monitor/log/connection.log

Make sure you make the log folder

$ mkdir /home/monitor/log

Output is in the format as

Sat Jan 30 14:25:02 NPT 2010 (UNKNOWN) [192.80.10.1] 5020 (?) open

Besides netcat, you could also use nmap command

$ nmap 192.168.10.1 -p T:5020 -oG filename.txt

Where -oG specifies output in Greppable format and writes out to filename.txt

Simple tcp monitor :)

Converting .wav files to .mp3 files in Ubuntu 9.04

The process is a two step process:

First,
Install the required packages
$ sudo apt-get install libavcodec-unstripped-52

Second,
Use proper command line tools for the conversion
$ ffmpeg -i source.wav -acodec libmp3lame -ab 64k destination.mp3

Now although it looks simple, I wasted two hours trying out different tools lame, mencoder, etc and wrong options with ffmpeg. Unnecessary bheja fry!

Cheers

Syndicate content