Wednesday, February 24, 2016

EXIT status messages in NIX

Exit Status

By default in Linux if particular command/shell script is executed, it return two type of values which is used to see whether command or shell script executed is successful or not.

(1) If return value is zero (0), command is successful.
(2) If return value is nonzero, command is not successful or some sort of error executing command/shell script.

This value is know as Exit Status.

But how to find out exit status of command or shell script?
Simple, to determine this exit Status you can use $? special variable of shell.

For e.g. If I want to remove a non-existing file

$ rm ABCD
rm: cannot remove `ABCD': No such file or directory
and after that if you give command
$ echo $?
it will print nonzero value to indicate error. Now give command

$ ls
$ echo $?
It will print 0 to indicate command is successful.

Exercise
Try these exit status and check for yourself:

$ expr 1 + 3
$ echo $?

$ echo Welcome
$ echo $?

$ wildwest canwork?
$ echo $?

$ date
$ echo $?

$ echon $?
$ echo $?

Convert all text in a file from UPPER to lowercase on LINUX

Convert all text in a file from UPPER to lowercase

To translate or delete characters use tr command. The basic syntax is:

Lets create a simple text file with mix of lower and upper case chars :
oracle@Linux01:[/u01/app/oracle/admin/bin] $ vi TGTGTG.txt

oracle@Linux01:[/u01/app/oracle/admin/bin] $ cat TGTGTG.txt
as This IS in Upper

Here I am passing the TGTGTG.txt as inprt and converting the output to lower case:
oracle@Linux01:[/u01/app/oracle/admin/bin] $ tr '[:upper:]' '[:lower:]' < TGTGTG.txt > output.txt

oracle@Linux01:[/u01/app/oracle/admin/bin] $ cat output.txt
as this is in upper

Here I am passing the TGTGTG.txt as inprt and converting the output to UPPER case:
oracle@Linux01:[/u01/app/oracle/admin/bin] $ tr '[:lower:]' '[:upper:]' < TGTGTG.txt > output_UP.txt

oracle@Linux01:[/u01/app/oracle/admin/bin] $ cat output_UP.txt
AS THIS IS IN UPPER

oracle@Linux01:[/u01/app/oracle/admin/bin] $

Monday, February 22, 2016

Configure gmail to send emails from your Linux machine

In this how to, I assume you already have a running CentOS 6.7 server. I am going to share how to install and setup ssmtp.x86_64 0:2.61-22.el5 on CentOS 6.7 version. This how to has been tested with mutt-1.5.20-7 as a mail client.

First, we must ensure everything are up to date by running the following commands: –

[root@Linux01 Desktop]# wget -c http://dl.fedoraproject.org/pub/epel/5/x86_64/epel-release-5-4.noarch.rpm

Resolving dl.fedoraproject.org... 209.132.181.26, 209.132.181.25, 209.132.181.23, ...
Connecting to dl.fedoraproject.org|209.132.181.26|:80... connected.
HTTP request sent, awaiting response... 200 OK
Length: 12232 (12K) [application/x-rpm]
Saving to: “epel-release-5-4.noarch.rpm”

100%[==================================================================================================================>] 12,232      --.-K/s   in 0s    

2016-02-22 09:50:52 (93.2 MB/s) - “epel-release-5-4.noarch.rpm” saved [12232/12232]

[root@Linux01 Desktop]# rpm -ivh epel-release-5-4.noarch.rpm
Preparing...                ########################################### [100%]
   1:epel-release           ########################################### [100%]
[root@Linux01 Desktop]# yum -y install ssmtp
Loaded plugins: fastestmirror, refresh-packagekit, security
Setting up Install Process
Loading mirror speeds from cached hostfile
 * base: mirror-centos.hostingswift.com
 * epel: archive.linux.duke.edu
 * extras: mirror.sesp.northwestern.edu
 * updates: mirror-centos.hostingswift.com
base                                                                                                                                 | 3.7 kB     00:00    
epel                                                                                                                                 | 3.6 kB     00:00    
epel/primary_db                                                                                                                      | 2.9 MB     00:01    
extras                                                                                                                               | 3.4 kB     00:00    
updates                                                                                                                              | 3.4 kB     00:00    
updates/primary_db                                                                                                                   | 3.9 MB     00:03    
Resolving Dependencies
--> Running transaction check
---> Package ssmtp.x86_64 0:2.61-22.el5 will be installed
--> Finished Dependency Resolution

Dependencies Resolved

============================================================================================================================================================
 Package                            Arch                                Version                                     Repository                         Size
============================================================================================================================================================
Installing:
 ssmtp                              x86_64                              2.61-22.el5                                 epel                               55 k

Transaction Summary
============================================================================================================================================================
Install       1 Package(s)

Total download size: 55 k
Installed size: 93 k
Downloading Packages:
:
:
:
:                                                                                                             1/1

Installed:
  ssmtp.x86_64 0:2.61-22.el5                                                                                                                              

Complete!

[root@Linux01 Desktop]# help sendmail
bash: help: no help topics match `sendmail'.  Try `help help' or `man -k sendmail' or `info sendmail'.
[root@Linux01 Desktop]# man -k sendmail
sendmail             (1)  - Postfix to Sendmail compatibility interface
sendmail             (8)  - Postfix to Sendmail compatibility interface


[root@Linux01 Desktop]# cd /etc/ssmtp
[root@Linux01 ssmtp]# ls -ll
total 8
-rw-r--r--. 1 root root  200 Apr 16  2001 revaliases
-rw-r-----. 1 root mail 1371 Jan 12  2015 ssmtp.conf
[root@Linux01 ssmtp]# mv /etc/ssmtp/ssmtp.conf /etc/ssmtp/ssmtp.conf.ori
[root@Linux01 ssmtp]# touch /etc/ssmtp/ssmtp.conf
[root@Linux01 ssmtp]# mv /etc/ssmtp/revaliases /etc/ssmtp/revaliases.orig
[root@Linux01 ssmtp]# touch /etc/ssmtp/revaliases
[root@Linux01 ssmtp]# vi /etc/ssmtp/ssmtp.conf
[root@Linux01 ssmtp]# vi /etc/ssmtp/revaliases

Next, we will configure sSMTP to forward email to any shared hosting server either running CPanel or DirectAdmin.

Include the following below in /etc/ssmtp/ssmtp.conf file: –

root=postmaster
mailhub=mail.yourdomain.com:587
Hostname=localhost
FromLineOverride=YES
AuthUser=abcd@yourdomain.com
AuthPass=YourPassWord
UseSTARTTLS=YES

Include the following below in /etc/ssmtp/revaliases file: –

root:username@yourdomain.com:mail.yourdomain.com:587

Include the following below in /root/.muttrc file: –

set envelope_from=yes
set from="username@yourdomain.com"
set realname="Prefer From Display"

You can use GMAIL server to forward your mails. Use below seetings to configure the GMAIL SMTP

If you an option to forward email to Gmail server, you can configure using steps below.

Replace previous configuration to below in /etc/ssmtp/ssmtp.conf file: –

root=postmaster
mailhub=smtp.gmail.com:587
Hostname=abcd@gmail.com
FromLineOverride=YES
AuthUser=abcd@gmail.com
AuthPass=YourPassWord
UseSTARTTLS=YES

Replace previous configuration to below in /etc/ssmtp/revaliases file: –

root:username@gmail.com:smtp.gmail.com:587

Replace previous configuration to below in /root/.muttrc file: –

set envelope_from=yes
set from="abcd@gmail.com"
set realname="Prefer From Display"

Next, you can start sending email using your preferred email server using command below: –

[root@Linux01 ~]# echo "Testing outgoing email" | mutt -s "Testing" arvind.toorpu@nelnet.net
bash: mutt: command not found
[root@Linux01 ~]# yum install mutt
Loaded plugins: fastestmirror, refresh-packagekit, security
Setting up Install Process
Loading mirror speeds from cached hostfile
 * base: mirror-centos.hostingswift.com
 * epel: mirror.steadfast.net
 * extras: mirror.sesp.northwestern.edu
 * updates: mirror-centos.hostingswift.com
Resolving Dependencies
--> Running transaction check
---> Package mutt.x86_64 5:1.5.20-7.20091214hg736b6a.el6 will be installed
--> Processing Dependency: urlview for package: 5:mutt-1.5.20-7.20091214hg736b6a.el6.x86_64
--> Processing Dependency: libtokyocabinet.so.8()(64bit) for package: 5:mutt-1.5.20-7.20091214hg736b6a.el6.x86_64
--> Running transaction check
---> Package tokyocabinet.x86_64 0:1.4.33-6.el6 will be installed
---> Package urlview.x86_64 0:0.9-7.el6 will be installed
--> Finished Dependency Resolution

Dependencies Resolved

============================================================================================================================================================
 Package                             Arch                          Version                                                Repository                   Size
============================================================================================================================================================
Installing:
 mutt                                x86_64                        5:1.5.20-7.20091214hg736b6a.el6                        base                        1.2 M
Installing for dependencies:
 tokyocabinet                        x86_64                        1.4.33-6.el6                                           base                        428 k
 urlview                             x86_64                        0.9-7.el6                                              base                         24 k

Transaction Summary
============================================================================================================================================================
Install       3 Package(s)

Total download size: 1.7 M
Installed size: 5.6 M
Is this ok [y/N]: y
Downloading Packages:
(1/3): mutt-1.5.20-7.20091214hg736b6a.el6.x86_64.rpm                                                                                 | 1.2 MB     00:00    
(2/3): tokyocabinet-1.4.33-6.el6.x86_64.rpm                                                                                          | 428 kB     00:00    
(3/3): urlview-0.9-7.el6.x86_64.rpm                                                                                                  |  24 kB     00:00    
------------------------------------------------------------------------------------------------------------------------------------------------------------
Total                                                                                                                       1.5 MB/s | 1.7 MB     00:01    
Running rpm_check_debug
Running Transaction Test
Transaction Test Succeeded
Running Transaction
  Installing : tokyocabinet-1.4.33-6.el6.x86_64                                                                                                         1/3
  Installing : urlview-0.9-7.el6.x86_64                                                                                                                 2/3
  Installing : 5:mutt-1.5.20-7.20091214hg736b6a.el6.x86_64                                                                                              3/3
  Verifying  : 5:mutt-1.5.20-7.20091214hg736b6a.el6.x86_64                                                                                              1/3
  Verifying  : urlview-0.9-7.el6.x86_64                                                                                                                 2/3
  Verifying  : tokyocabinet-1.4.33-6.el6.x86_64                                                                                                         3/3

Installed:
  mutt.x86_64 5:1.5.20-7.20091214hg736b6a.el6                                                                                                              

Dependency Installed:
  tokyocabinet.x86_64 0:1.4.33-6.el6                                               urlview.x86_64 0:0.9-7.el6                                            

Complete!
[root@Linux01 ~]# vi /root/.muttrc
[root@Linux01 ~]# clear

[root@Linux01 ~]# echo "Testing outgoing email" | mutt -s "Testing" abcd@gmail.com
[root@Linux01 ~]# clear


Finally, with all the configuration above I hope you are able to use and enjoy your sSMTP to send an email. Thank you.

Friday, February 19, 2016

#!/bin/bash
# Purpose: Detecting ORACLE Errors from any log file and send email
# Author: Arvind Toorpu
# Note : The script must run as a cron-job.
# Last updated on : 15-Feb-2016
## edit FILE to log file u want to check for errors
# -----------------------------------------------

# Store path to commands
FILE=/u01/app/oracle/admin/bin/test_error.txt

# Store email settings

AEMAIL="abcd@anyorg.com"
ASUB="ORACLE Error - $(hostname)"
AMESS="Warning - ORACLE errors found on $(hostname) @ $(date). See log file for the details /u01/app/oracle/admin/bin/test_error.txt"
OK_MESS="OK: NO ORACLE Error Found."
WARN_MESS="ERROR: ORACLE Error Found."


# Check if $FILE exists or not
if test ! -f "$FILE"
then
        echo "Error - $FILE not found or mcelog is not configured for 64 bit Linux systems."
        exit 1
fi

# okay search for errors in file
error_log=$(grep -c -i "ORACLE error" $FILE)

# error found or not?
if [ $error_log -gt 0 ]
then    # yes error(s) found, let send an email
        echo "$AMESS" | mailx -s "$ASUB" $AEMAIL <<-EOF
		### Below line can be removed if you dont want to receive last few lines of that alert log
        `tail -n -50 /u01/app/oracle/admin/bin/test_error.txt` 
        EOF
else    # naa, everything looks okay
        echo "$OK_MESS"
fi
<p>#!/bin/bash # Purpose: Detecting ORACLE Errors from any log file and send email # Author: Arvind Toorpu # Note : The script must run as a cron-job. # Last updated on : 15-Feb-2016 ## edit FILE to log file u want to check for errors # -----------------------------------------------</p>

<p># Store path to commands FILE=/u01/app/oracle/admin/bin/test_error.txt</p>

<p># Store email settings</p>

<p>AEMAIL=&quot;abcd@anyorg.com&quot; ASUB=&quot;ORACLE Error - $(hostname)&quot; AMESS=&quot;Warning - ORACLE errors found on $(hostname) @ $(date). See log file for the details /u01/app/oracle/admin/bin/test_error.txt&quot; OK_MESS=&quot;OK: NO ORACLE Error Found.&quot; WARN_MESS=&quot;ERROR: ORACLE Error Found.&quot;</p>

<p> # Check if $FILE exists or not if test ! -f &quot;$FILE&quot; then  echo &quot;Error - $FILE not found or mcelog is not configured for 64 bit Linux systems.&quot;  exit 1 fi</p>

<p># okay search for errors in file error_log=$(grep -c -i &quot;ORACLE error&quot; $FILE)</p>

<p># error found or not? if [ $error_log -gt 0 ] then # yes error(s) found, let send an email  echo &quot;$AMESS&quot; | mailx -s &quot;$ASUB&quot; $AEMAIL <<-EOF  ### This can be removed if you dont want to receive last few lines of that alert log  &#96;tail -n -50 /u01/app/oracle/admin/bin/test_error.txt&#96;   EOF else # naa, everything looks okay  echo &quot;$OK_MESS&quot; fi </p>