Tuesday, July 21, 2009

My first perl script

This is a perl script to scan a list of folders with certain name pattern, and retrieve certain information from specific file, then put into a CSV file and send a email to user with attachment.


#!/usr/bin/perl -w
#fine file name list and put into an array
use MIME::Lite;
sub findFile($);

sub sendEmail
{
my ($to, $from, $subject, $message) = @_;
my $sendmail = '/usr/sbin/sendmail';
open(MAIL, "|$sendmail -oi -t");
print MAIL "From: $from\n";
print MAIL "To: $to\n";
print MAIL "Subject: $subject\n\n";
print MAIL "$message\n";
close(MAIL);
}

sub findFile($)
{
my $dirname = $_[0];
my @matches = ();
local *DIR;
opendir ( DIR, $dirname ) || print "failed to open dir";
while( ($filename = readdir(DIR))){
$entry = "$dirname/$filename";
if (-d $entry )
{
if ($filename =~ m/INBOX/ || $filename =~ m/^\d\d\d\d$/)
{
push @matches, findFile($entry);
}
}
if (-f $entry && $filename =~ m/^msg\d\d\d\d.txt$/)
{
# print"filename ==============","$filename\n";
push @matches, $entry;
}
}
closedir(DIR);
return @matches;
}

@file_list = findFile("/var/lib/asterisk/sounds/voicemail/default");
# print @file_list;
#open an output file
#$out_file = '/tmp/tmp_file'; #define the name of output file
#open(OUT_FILE, ">$out_file");

$output_str = '';
#put head
#print OUT_FILE "Origbox, CallerId, OrigDate, Duration\n";
$output_str = $output_str."Origbox, CallerId, OrigDate, Duration\n";

#loop through file list
foreach $in_file_name (@file_list)
{
#open input file
open(IN_FILE, $in_file_name);
@lines = <IN_FILE>; #read file into an array

#loop through lines
%props = ();
foreach $_ (@lines)
{
if (/^origmailbox=/)
{
@entry = split(/=/, $_);
chomp($entry[1]);
%props = (%props, origmailbox=>$entry[1]);
}
if (/^callerid=/)
{
@entry = split(/=/, $_);
chomp($entry[1]);
%props = (%props, callerid=>$entry[1]);
}
if (/^origdate=/)
{
@entry = split(/=/, $_);
chomp($entry[1]);
%props = (%props, origdate=>$entry[1]);
}
if (/^duration=/)
{
@entry = split(/=/, $_);
chomp($entry[1]);
%props = (%props, duration=>$entry[1]);
}

}
if (length($props{"origmailbox"}) > 0) {
#print OUT_FILE "$props{origmailbox},$props{callerid},$props{origdate},$props{duration}\n";
$output_str = $output_str."$props{origmailbox},$props{callerid},$props{origdate},$props{duration}\n";
}

#finished job and close input file
close(IN_FILE);
}
#close(OUT_FILE);

my $msg = MIME::Lite->new(
From => 'dev@abc.com',
To => 'tom@abc.com',
Cc => 'jerry@abc.com',
Subject => 'Fonality Voice Mail Box Statistics',
Type => 'multipart/mixed',
);

$msg->attach(
Type => 'TEXT',
Data => "Please see attached file for detail...",
);
$msg->attach(
Type => 'text/csv',
Data => $output_str,
Filename => "fonality.csv",
);

$msg->send;

A shell script to do a bunch of things

This is a shell script I wrote for Krim to do following things:
1. Add a mount point into /etc/fatab.
2. create user and mount the folder.


#!/bin/bash

# add lab mount point into /etc/fstab. then create user, mount the folder
# Usage: addlab <vlab1|vlab2> <user_number>
# eg: addlab vlab1 8978
if [ $# -eq 0 ]
then
echo "Usage: $0 <vlab1|vlab2> <user_number>"
exit -1
fi

echo "You are about to create a lab for user lifelabs_$2 [y/n]:"; read x

if [ $x != 'y' ]
then
echo "bye"
exit -1
fi

vlab1="//to-vlab1/LIFELABS_$2 /home/lifelabs_$2/data cifs rw,mand,file_mode=0666,gid=lifelabs_$2,uid=lifelabs_$2,nosetuids,user=TO-VLAB1/lifelabs,password=velvet 0 0"

vlab2="//to-vlab2/LIFELABS_$2 /home/lifelabs_$2/data cifs rw,mand,file_mode=0666,gid=lifelabs_$2,uid=lifelabs_$2,nosetuids,user=TO-VLAB2/lifelabs,password=velvet 0 0"


#search if this lab already in fstab
if grep -q LIFELABS_$2 /etc/fstab
then
echo "LIFELABS_$2 IS ALREADY EXIST IN FSTAB FILE !!! EXIT"
exit -1
fi

if [ "vlab1" == $1 ]
then
echo $vlab1
echo "$vlab1" >> /etc/fstab
elif [ $1 == "vlab2" ]
then
echo $vlab2
echo "$vlab2" >> /etc/fstab
else
echo "no such lab - $1"
exit -1
fi

#add a user and setup password
adduser lifelabs_$2
echo lifelabs_$2:LIFELABS$2 | chpasswd

su - lifelabs_$2 -c "mkdir data"

mount /home/lifelabs_$2/data

exit 0

Google+