Thursday, July 2, 2015

Perl Gmail SMTP

Credit to :
http://robertmaldon.blogspot.sg/2006/10/sending-email-through-google-smtp-from.html

# Remember to enable to allow less secure app to login the account

#!/usr/bin/perl -w

use Net::SMTP::SSL;

sub send_mail {
my $to = $_[0];
my $subject = $_[1];
my $body = $_[2];

my $from = 'cybertech@company.com';
my $password = 'Pass';

my $smtp;

if (not $smtp = Net::SMTP::SSL->new('smtp.gmail.com',
                            Port => 465,
                            Debug => 1)) {
   die "Could not connect to server\n";
}

$smtp->auth($from, $password)
   || die "Authentication failed!\n";

$smtp->mail($from . "\n");
my @recepients = split(/,/, $to);
foreach my $recp (@recepients) {
    $smtp->to($recp . "\n");
}
$smtp->data();
$smtp->datasend("From: " . $from . "\n");
$smtp->datasend("To: " . $to . "\n");
$smtp->datasend("Subject: " . $subject . "\n");
$smtp->datasend("\n");
$smtp->datasend($body . "\n");
$smtp->dataend();
$smtp->quit;
}

# Send away!
&send_mail('cybertech@cybertechcom', 'Testing is working', 'Should try this out!');


Wednesday, July 1, 2015

Configuring Samba RHEL

Credit references to:
http://www.debuntu.org/samba-how-to-share-files-for-your-lan-without-userpassword/

http://www.techotopia.com/index.php/Sharing_Files_between_RHEL_6_and_Windows_Systems_with_Samba

https://access.redhat.com/documentation/en-US/Red_Hat_Enterprise_Linux/5/html/Deployment_Guide/s2-samba-configuring-cmdline.html

http://www.techotopia.com/index.php/Sharing_Files_between_RHEL_6_and_Windows_Systems_with_Samba

https://www.youtube.com/watch?v=yDA8DOHDaRg

https://aaronwalrath.wordpress.com/2011/03/26/install-samba-server-on-red-hat-enterprise-linux-6scientific-linux-6/

http://www.linuxexplorers.com/2014/04/configure-samba-share-in-red-hat-enterprise-linux-7-rhel7/

https://access.redhat.com/documentation/en-US/Red_Hat_Enterprise_Linux/3/html/Reference_Guide/s1-iptables-saving.html

Now we need to make changes to the “iptables” firewall startup config file. Backup the file and edit:
# cp /etc/sysconfig/iptables /etc/sysconfig/iptables.bak
# vi /etc/sysconfig/iptables
Add the first line accepting packets on TCP/445. Be sure and add it above the last line of the “input” chain with the “Reject” target, that way the rule will be processed.
-A INPUT -p tcp --dport 445 -j ACCEPT-A INPUT -j REJECT --reject-with icmp-host-prohibited

Then RESTART IPTABLE service
# service iptables restart

Configuring the smb.conf File

# Edit smb.conf
[cyber@cybertech ~]$ sudo vi /etc/samba/smb.conf
[sudo] password for cyber:

# Share with defined user to read and write permission
        [ITshare]
        comment = IT scripts folder
        path = /home/itshare/
        valid users = cyber
        public = yes
        writeable = yes
        printable = no
        create mask = 0777
        browseable = yes

# Share without prompt login and able to read and write permission
       [ITshare]
        comment = IT scripts folder
        path = /home/itshare/
        write list = guest  # This allow guest to write without login
        read only = No
        create mask = 0777
        guest ok = Yes


        [WWW]
        comment = Web Hosting Folder
        path = /var/www/
        valid users = cyber root
        public = yes
        writeable = yes
        printable = no
        create mask = 0777
        browseable = yes


# Restart SMB service
[cyber@cybertech ~]$ sudo service smb restart
Shutting down SMB services:                                [  OK  ]
Starting SMB services:                                     [  OK  ]

Perl SQL query looping in Range of dates

Credited to http://stackoverflow.com/users/725418/tlp and http://stackoverflow.com/users/338059/sid-burn

http://stackoverflow.com/questions/19724688/perl-loop-through-months-in-a-specified-range

http://stackoverflow.com/questions/6622818/what-is-the-optimal-way-to-loop-between-two-dates-in-perl


# The Perl script below is trying to loop each day in a range of dates and perform SQL date query

#!/usr/bin/perl -w
use strict;

use Net::SSH::Any;
use DateTime;
use feature 'say';

my $hostname = "10.x.x.x";
my $username = "USER";

my $password = "PASS";

my $cmd = ""; 
my $output = "";

my $target_date = "";

my $start_date = DateTime->new(year => 2015, month => 1, day => 1);
my $end_date = DateTime->new(year=>2015, month => 7, day => 1);

my $ssh = Net::SSH::Any->new($hostname, user => $username, password => $password);
$ssh->error and die $ssh->error;

while ($start_date <= $end_date)
 {
# Get date format
say $start_date->strftime("%d-%b-%Y");
$target_date = $start_date->strftime("%d-%b-%Y");

# convert to uppercase for the month 
$target_date = uc $target_date;

# forming right SQL message
$cmd = "perl getSQLdata.pl \"SELECT  \\\"Project\\\" FROM \\\"Result\\\" where to_char(to_timestamp(\\\"Start_Time\\\",\'MM/DD/RRRR HH:MI:SS AM\'),\'DD-MON-RRRR\') = \'".$target_date."\'\"";
print $cmd;

# Get SQL data
$output = $ssh->capture($cmd);

# print output 
print $output."\n";

# increment a day
$start_date->add(days => 1);
}

exit 1;

Perl SQL example

http://www.dba-oracle.com/t_dbi_perl_sql_execution.htm

# HERE is the script that accept SQL command and get return from Oracle DB

#!/usr/bin/perl

# include oracle driver folder
BEGIN {
$ENV{ORACLE_HOME} = "/home/cybertech/app/cybertech/product/12.1.0/client_1";
$ENV{LD_LIBRARY_PATH} = "/home/cybertech/app/cybertech/product/12.1.0/client_1";
}

use lib Bundle;
use Bundle::DBI;
use DBI;
use strict;

use POSIX qw(strftime);

if ($#ARGV > 1)
{
print "\nUsage: perl getSQLdata.pl \"[SQL statement]\"\n\n\Example: perl getSQLdata.pl \"select * from tablename\"";
exit -1;
}

my $now_string = strftime "%Y%b%e_%H%M%S", localtime;
my $script_log= "SQL_log_$now_string.log";
my @sqlReturn = ();
chomp($ARGV[0]);


my $temp = "";

# Perform Oracle connection
    my $dbh = DBI->connect('DBI:Oracle:host=HOSTNAME;sid=SIDNAME;port=1521', 'USERNAME', 'PASS')
                or die "\n\nCouldn't connect to database: " . DBI->errstr;
    
# Forming SQL query
my $SQL_CMD = join("",@ARGV);

my $sth = $dbh->prepare($SQL_CMD)
                or die "\n\nCouldn't prepare statement: " . $dbh->errstr;

# Run SQL command
$sth->execute() || try_again();

# open file handler
open (WF, ">$script_log") || die "\n$!\n";

# Loop each row of data
while(my @row = $sth->fetchrow_array())
{
@sqlReturn = ();
# display each column
foreach(@row)
{
$_ = "\t" if !defined($_);
#print "$_\t";
print WF "$_".",";
push (@sqlReturn, $_.",");
}
print @sqlReturn;
print "\n";
print WF "\n";
$temp = join("",@sqlReturn);
$temp = "";
}

END
{ $dbh->disconnect if defined($dbh); }
exit 1;