shell scripts
How to extract a IP from a string?
#!/usr/bin/php
<?php
$subject="<SIP:assds@74.222.12.186@;tag=50e258996b53e309";
$r="/(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)/";
preg_match($r,$subject,$matches);
print_r($matches[0]);
?>
How to increase ULIMIT?
* hard nofile 4096
How to Create mp3 Files using google api?
<?php
/*---------------------------------------------------
Text To Speech using Google API
----------------------------------------------------*/
// Google Translate API cannot handle strings > 100 characters
$words = substr($_GET['words'], 0, 100);
$words = urlencode($_GET['words']);
$file = date('Ymdhis') . ".mp3";
if (!file_exists($file)) {
$mp3 = file_get_contents(
'http://translate.google.com/translate_tts?q=' . $words;
file_put_contents($file, $mp3);
}
?>
// Embed the MP3 file using the AUDIO tag of HTML5
<audio controls="controls" autoplay="autoplay">
<source src="<? echo $file; ?>" type="audio/mp3" />
</audio>
Delete files older then n days
Process to Delete files from a directory where file is older than 'n' days.
Recommended you to the list the files and check before deleting them, by running the following command first:
find /path/to/files* -mtime +2 -exec ls {} \;
Run the following command to delete
find /path/to/files* -mtime +2 -exec rm {} \;
Note:
- There are spaces between rm, {}, and \;
- +2 means files older than 2 days
You can also use the following command to schedule the cron job(in this example the command will run every night at 1am.
00 01 * * * find /path/to/files* -mtime +2 -exec rm {} \;
A PHP program to find the CPU and Memory Load
<?php
function get_server_memory_usage(){
$free = shell_exec('free');
$free = (string)trim($free);
$free_arr = explode("\n", $free);
$mem = explode(" ", $free_arr[1]);
$mem = array_filter($mem);
$mem = array_merge($mem);
$memory_usage = $mem[2]/$mem[1]*100;
return $memory_usage;
}
function get_server_cpu_usage(){
$load= shell_exec("ps aux|awk 'NR > 0 { s +=$3 }; END {print \"cpu %\",s}'");
return $load;
}
echo 'Server Memory Usage:' . get_server_memory_usage() ;
echo PHP_EOL;
echo 'Server CPU Usage:' . get_server_cpu_usage();
?>
A Program to convert WAV file to GSM and upload it to ftp server.
#!/bin/bash
for x in /home/odn1/jakarta-tomcat-5.5.7/webapps/ROOT/voicetap/rec/callrec/*.wav
do
recdir=/home/odn1/jakarta-tomcat-5.5.7/webapps/ROOT/voicetap/rec/callrec/
gsmdir=/home/odn1/jakarta-tomcat-5.5.7/webapps/ROOT/voicetap/rec/callrec/uploaded/
i=${#x}
j=`expr ${#recdir} + 1`
l=`expr $i - $j`
k=`expr $l - 3`
filename=`expr substr $x $j $k`
#len =`expr $i -$j`
exten=".gsm"
f=$gsmdir$filename$exten
sox $x $f
done
cd /home/odn1/jakarta-tomcat-5.5.7/webapps/ROOT/voicetap/rec/callrec/uploaded/
HOST='10.201.1.114'
USER='sandy'
PASSWD='banti'
FILE='*.gsm'
ftp -n $HOST <<END_SCRIPT
quote USER $USER
quote PASS $PASSWD
prompt off
mput $FILE
bye
END_SCRIPT
rm *.gsm
========================================
A PHP script to merge to wav file
Command to execute /usr/bin/php filemerge.php /sandip/prompts/sourcepath1 /sandip/prompts/sourcepath2 /sandip/prompts/destination
script.name sourcepath1 sourcepath2 destinationpath
Code
-----------------------------
#!/usr/bin/php -q
<?php
$sourcepath1=$argv[1];
$sourcepath2=$argv[2];
$destinationpath=$argv[3];
echo $sourcepath1."\n".$sourcepath2."\n".$destinationpath."\n";
//exit();
if ($handle1 = opendir($sourcepath1))
{
if ($handle2 = opendir($sourcepath2))
{
if ($handle3 = opendir($destinationpath))
{
/* This is the correct way to loop over the directory. */
while (false !== ($file = readdir($handle1)))
{
$phoneno=substr($file,0,10);
echo $phoneno;
if(strlen($file)<4)
{
echo "No File available to process\n";
exit();
}
$command="sox ".$sourcepath1."/".$phoneno.".wav ".$sourcepath2."/".$phoneno.".wav ".$destinationpath."/".$phoneno.".wav";
echo rtrim($command,2)."\n";
shell_exec(trim($command));
}
closedir($handle3);
}
else
{
echo "Either Destination Path doesn't exist or We don't have the permission to access the path";
}
closedir($handle2);
}
else
{
echo "Either Source Path 2 doesn't exist or We don't have the permission to access the path";
}
closedir($handle1);
}
else
{
echo "Either Source Path 1 doesn't exist or We don't have the permission to access the path";
}
?>
========================================
Getting CURL working on XAMPP
- Go to xampp install directory.
- Go to php.ini in php directory.
- Open php.ini and find curl, uncomment the first find you see. Save and close file.
- Go to php.ini in apache directory.
- Repeat Step Three.
- Done.
A program to check whether a process is running or not by PHP program.
#!/usr/bin/php -q
<?php
$cmd = "ps aux|grep vsftpd";
exec($cmd, $output, $result);
print_r($output);
if(count($output) > 2){
echo 'process is running';
}else
{
echo 'process is not running';
}
?>
PHP file not executing from command line
Error : Extension './searchdbkenya.php' not present.
Solution: edit file with vi
Press :(colon) Type set ff=unix
Google Email account for Free.
https://www.google.com/a/cpanel/domain/new
How to reset the nagiosadmin password
sudo htpasswd -c /usr/local/nagios/etc/htpasswd.users nagiosadmin
HOW TO CREATE A SOFTLINK
ln -s <source path> <destination>
HOW TO CHANGE THE TIME ZONE IN CENTOS/LINUX?
- mv /etc/localtime /etc/localtime.bak
- ln -s /usr/share/zoneinfo/Africa/Nairobi /etc/localtime
- date -s 21:38:00 (To set the date)
vi /etc/security/limits.conf
* soft nofile 4096