Bash Scripting

Bash (Bourne Again SHell )is a Unix shell or we can say it’s a command line interpreter which provides a traditional user interface for UNIX Operating System and for other similar Unix systems. This was the replacement for Bourne shell and was released in year 1989. This is distributed GNU OS as a default shell on Unix or LINUX operating system. This is POSIX shell with more number of extensions.

We can execute the BASH using command line or writing a series of commands on a text file and save it with “.sh” extension. If we want to execute any BASH script then first we have to provide the execute permission and if we don’t provide the permission or forgot to provide then it will simply given an error message by saying “Permission denied”.

For example here is my very simple BASH script which is named as “hello.sh” .

#!/bin/bash
# Sample Bash script
echo Hello Sloba!

Now let us see if we have the execute permission or not for the above script.

login as: sloba
sloba@**************'s password:
Welcome to Linux Mint 17.2 Rafaela (GNU/Linux 3.16.0-38-generic x86_64)

Welcome to Linux Mint
 * Documentation:  http://www.linuxmint.com
Last login: Sun Oct 11 18:29:48 2015 from 192.168.2.8
sloba@sloba-VirtualBox ~ $
sloba@sloba-VirtualBox ~/Desktop/BASH $ ls -lrt
total 4
-rw-rw-r-- 1 sloba sloba 50 Nov 17 09:25 hello.sh
sloba@sloba-VirtualBox ~/Desktop/BASH $

 

And below is the screen shot from the server using WinScp tool.

image

 

Now we see that we don’t have the execute privileges on the script but let us go ahead and try to run it.

sloba@sloba-VirtualBox ~/Desktop/BASH $ ./hello.sh
-bash: ./hello.sh: Permission denied
sloba@sloba-VirtualBox ~/Desktop/BASH $

Now we can see that we get an error with “ Permission denied ”. Now let us try to provide the execute privilege  to our script and then run it.

ls -lrt
total 4
-rwxrwxrwx 1 sloba sloba 50 Nov 17 09:25 hello.sh
sloba@sloba-VirtualBox ~/Desktop/BASH $ ./hello.sh
Hello Sloba!
sloba@sloba-VirtualBox ~/Desktop/BASH $

Finally we are able to get the result of the simple hello script after giving the execute privilege, in general 755 permission is provide to execute the script or modify by the owner .

To execute the script we have seen that we use “./” before the script . The Dot means it is pointing to the current directory to find the script and execute it. And if we keep the script any other directory and we can use something like this “./<<path>>/hello.sh” .

We also see from the script that the first line starts with “#!” , this is called Shebang . The hash and exclamation mark is the character sequence which is basically referred as shebang , after that what we see is the interpreter path which should be used to execute the program for the next set of lines in the file.

 

Thank you for reading my blog . Please feel free to leave me some feedback or to suggest any future topics.

Looking forward to hear from you – Swadhin Ray (Sloba) -( LinkedIn ) ( Twitter )

SFTP from PLSQL

In this blog I am going to use a simple program to SFTP files from my database server to any target machine using SFTP. To demonstrate this I am using two servers one where I have Oracle installed and another server where SFTP is enabled.

SFTP basically means a protocol in which we can move one file to any other server in secured way. The full form is “Secure File Transfer Protocol” , this is protocol which is packaged with SSH.

To achieve this we have to do some prerequisites like creating one shell script , and follow to create a function called “msi_extprocsh” from my earlier blog.

Link as to follow the steps to create the function:

https://slobaexpert.wordpress.com/2015/09/10/executing-linux-command-from-oracle-plsql/

Once you have complete the steps shown on the link , create a folder called “sftp”  on your database server “/home/oracle/app/” .

And create a file called “sftp.sh” under path “”  with the below code:

#!/usr/bin/expect

set timeout -1

if { $argc != 6 } {
    puts "Usage $argv0 host user pass cd lcd file "
    exit 1
}

set host [lindex $argv 0]
set user [lindex $argv 1]
set pass [lindex $argv 2]
set cd [lindex $argv 3]
set lcd [lindex $argv 4]
set file [lindex $argv 5]

spawn sftp -oStrictHostKeyChecking=no -oCheckHostIP=no $user@$host
expect *assword:

send "$pass\r"
expect sftp>

send "cd $cd\r"
expect sftp>

send "lcd $lcd\r"
expect sftp>
send "put $file\r"

send "exit\r"
expect eof

The above file contains 6 parameters where we need to pass the host name of target server where we want to move the file along with username & password .  Set the local and report directory where we want to place the file.

Now under path “/home/oracle/app/sftp” create a sample file called “Hello.txt” and save the file with some sample text like :


[oracle@DBserversftpsftp]$ pwd
/home/oracle/app/sftp
[oracle@DBserversftp]$ cat Hello.txt
This is a sample file[oracle@DBserversftp]$

Now lets login to our target server where we want to place the file :

login as: root
root@TargetServer's password:
Last login: Thu Sep 10 11:27:44 2015 from TargetServer
[root@TargetServer ~]# cd Desktop/sftp/
[root@TargetServer sftp]# pwd
/root/Desktop/sftp
[root@TargetServer sftp]# ls
[root@TargetServer sftp]#

We can see that there is no file present on the location

Now let’s run the PLSQL block to place the “Hello.txt” file on the target server.

DECLARE

        l_file_name       VARCHAR2(4000) := 'Hello.txt'; --file name 
        retval            NUMBER;
        l_sftp_prog       VARCHAR2(100) := 'cd /home/oracle/app/sftp; ./sftp.sh';
        l_sftp_host       VARCHAR2(100) := 'hostname';
        l_sftp_user       VARCHAR2(100) := 'username';
        l_sftp_pass       VARCHAR2(100) := 'password';
        l_sftp_path       VARCHAR(500) := '/root/Desktop/sftp/'; -- target directory to put the file
        l_local_file_path VARCHAR2(500) := '/home/oracle/app/sftp/'; -- local file path 
        l_sftp_command    VARCHAR2(500);

BEGIN

        l_sftp_command := l_sftp_prog || ' ' || l_sftp_host || ' ' || l_sftp_user || ' ' ||
                          l_sftp_pass || ' ' || l_sftp_path || ' ' || l_local_file_path || ' ' ||
                          l_file_name;

        dbms_output.put_line(l_sftp_command);

        retval := msi_shell(l_sftp_command);

        dbms_output.put_line(retval);

END;

Check the target server :

[root@TargetServer sftp]# ls
[root@TargetServer sftp]# ls
Hello.txt
[root@TargetServer sftp]# pwd
/root/Desktop/sftp
[root@TargetServer sftp]#
[root@TargetServer sftp]# cat Hello.txt
This is a sample file[root@TargetServer sftp]#

Now we can see that the file is moved using SFTP protocol through PLSQL code. Basically we are calling the bash script to do the SFTP and PLSQL code is calling the script by passing right set of parameter from Oracle.

Thank you for reading my blog , the reference is taken from OTN . Please feel free to leave me some feedback or to suggest any future topics.

Looking forward to hear from you – Swadhin Ray (Sloba) -( LinkedIn ) ( Twitter )

Sending Email from Linux

 

Here is the simple way to write an email script which can be utilized to send emails to recipients.

Login to your Linux box , I am using a virtual machine with Linux Mint operating system installed running on Oracle Virtual Box.

login as: sloba
sloba@**********'s password:
Welcome to Linux Mint 17.2 Rafaela (GNU/Linux 3.16.0-38-generic i686)

Welcome to Linux Mint
 * Documentation:  http://www.linuxmint.com
Last login: Fri Jul 24 12:52:24 2015 from msi-l1028.metricstream.com
sloba@sloba-VirtualBox ~ $ 

Below is a very basic and simple script which can be used to send email :

 

##################################################################
# Email notification
##################################################################
SUBJECT="TEST EMAIL FROM UNIX"
# use form to send from whom the email address has sent
FROM="automail@slobaexpert.wordpress.com"
DESCRIPTION="THIS IS AN AUTO-GENERATED MESSAGE"
#Put the email address of the person to whom you want to send with comma seperator
EMAIL="<<to email address>> @<<hostname >>.com"
EMAILMESSAGE=/tmp/emailmessage

 echo "From: $FROM" > $EMAILMESSAGE
 echo "To: $EMAIL" >> $EMAILMESSAGE
 echo "Subject: $SUBJECT " >> $EMAILMESSAGE
 echo "************************************************************************" >> $EMAILMESSAGE
 echo "$DESCRIPTION : DO NOT REPLY TO THIS EMAIL MESSAGE. " >> $EMAILMESSAGE
 echo "************************************************************************" >> $EMAILMESSAGE
 echo " " >> $EMAILMESSAGE
 echo " " >> $EMAILMESSAGE
 echo "Test file email " >> $EMAILMESSAGE
 echo " " >> $EMAILMESSAGE
 echo " " >> $EMAILMESSAGE
 echo "Thanks, " >> $EMAILMESSAGE
 echo "$FROM" >> $EMAILMESSAGE
 cat $EMAILMESSAGE | /usr/sbin/sendmail -t

 

You can change the TO address from the above script and test it. But we need to make sure “sendmail ” is already installed on your server to use this , if not then you can install using the below command.

sudo apt-get install sendmail

Enable SSH server in Linux Mint

Normally we do connect to any server from WinScp or Putty tool from Windows platform to connect any Linux environment to do various activities. 

Here I am going to show how we can enable SSH on Linux Mint operating system. I am using a virtual machine was built on Oracle Virtual Box. 

image

The above image show my virtual machine running on Oracle VM virtual box. Now let me open my putty and try connect to my virtual machine. To know the hostname of the VM you can open the terminal and type “hostname” as shown like below:]

image

Open Putty :

image

Click on Open.

image

After this I get the above error. Now let try to enable SSH on our Linux machine we need to first install “OpenSSH” .  

There are two options that we can do to install “OpenSSH” :

Option 1:

  • Open the main menu, select the Software Manager
  • Search for “Openssh ” on the search box
  • Click on the package openssh-server, then select install.
  • Once the installation is completed then start SSH by executing “/etc/init.d/ ssh start”  command.

image

image

image

image

Click on “install”

Option 2: 

As Linux Mint Operating system is based on Ubuntu , so we can install “openssh” from terminal or console.

 

image

image

image

image

Check if port 22 is enabled or not under “sshd_config” file , we can also modify the port in this file to access SSH on another port but I am using the default setup i.e. port 22.

image

Now let’s try connecting through putty from my local system to the VM .

image

 

image

 

image

Now we can see that I am able to connect to my Linux machine using Putty after enabling SSH on VM server.