SCP and Pass Variable in Bash Script


#!/bin/bash

read -p 'Enter the Files to be Send to Remote Host: ' files
destination_directory='/home/oracle'

scp -i /Users/shadab/wha.pem $files oracle@192.168.1.200:$destination_directory

## SAMPLE USAGE ##
# shadabs-MacBook-Pro:~ shadab$ ./scp_files.sh
#Enter the Files to be Send to Remote Host: *.sh
#scp_files.sh 100% 344 39.1KB/s 00:00
#scp_files_gone.sh 100% 344 52.3KB/s 00:00
#
#
#shadabs-MacBook-Pro:~ shadab$ ./scp_files.sh
#Enter the Files to be Send to Remote Host: scp_files.sh
#scp_files.sh 100% 434 80.5KB/s 00:00

You can now add alias for this script in .bashrpc file

alias scpo=’sh /Users/shadab/scp_files.sh’

Multiple Homes and Instances Oracle Database Bash Profile


# .bash_profile

# Get the aliases and functions
if [ -f ~/.bashrc ]; then
. ~/.bashrc
fi

# User specific environment and startup programs
export CATALINA_HOME=/home/oracle/apache-tomcat
PATH=/bin:/usr/bin:/etc:/usr/sbin:/usr/ucb:$HOME/bin:/usr/bin/X11:/sbin:/bin/bash:$CATALINA_HOME/bin.
export PATH
EDITOR=vi
export EDITOR=vi

if [ -s "$MAIL" ] # This is at Shell startup. In normal
then echo "$MAILMSG" # operation, the Shell checks
fi # periodically.

echo " "
echo " "
echo " ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"
echo " Database options"
echo " "
echo " "
echo " 1) DATABASE 12cR2"
echo " "
echo " 2) DATABASE 12cR1"
echo " "
echo " "
echo " ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"
echo " "
echo " "
echo " Enter Choice:\c"
read env
if [ $env -eq 1 ] ; then
export ORACLE_HOME=/u01/app/oracle/product/12.2.0/dbhome_1
export ORACLE_BASE=/u01/app/oracle
export ORACLE_SID=ora12cr2
PS1='$PWD:12cR2-->'

else
if [ $env -eq 2 ] ; then
export ORACLE_HOME=/u01/app/oracle/product/12.1.0/dbhome_1
export ORACLE_BASE=/u01/app/oracle
export ORACLE_SID=ora12cr1
PS1='$PWD:12cR1-->'
fi
fi
export LD_LIBRARY_PATH=$ORACLE_HOME/lib:/usr/lib:/lib
export LIBPATH=$ORACLE_HOME/lib32:$ORACLE_HOME/lib:/usr/lib:/lib
export PATH=$PATH:$ORACLE_HOME/bin:/u01/app/12.1.0.2/grid/bin:$ORACLE_HOME/OPatch
stty erase ^?
TMOUT=0;TIMEOUT=600;export readonly TMOUT TIMEOUT
set -o vi

echo ''
echo ''
clear
echo
echo "ORACLE_SID=$ORACLE_SID"
echo

alias tsmd='cd /opt/tivoli/tsm/client/oracle/bin64/'
alias ob='cd $ORACLE_BASE'
alias oh='cd $ORACLE_HOME'
alias tns='cd $ORACLE_HOME/network/admin'
alias ch='cd $CATALINA_HOME'
alias envo='env | grep ORACLE'
alias sqld='rlwrap sqlplus "/as sysdba"'
export PATH
umask 022

envo

A Very Cool Oracle Bash Profile


# .bash_profile

# Get the aliases and functions
if [ -f ~/.bashrc ]; then
. ~/.bashrc
fi

# User specific environment and startup programs

PATH=$PATH:$HOME/.local/bin:$HOME/bin

export PATH

# Oracle Settings
export TMP=/tmp

export ORACLE_HOSTNAME=easyoradba.com
export ORACLE_UNQNAME=ora12c
export ORACLE_BASE=/u01/app/oracle
export ORACLE_HOME=$ORACLE_BASE/product/12.2.0/dbhome_1
export ORACLE_SID=ora12c
export CATALINA_HOME=/home/oracle/apache-tomcat

PATH=/usr/sbin:$PATH:$ORACLE_HOME/bin:$CATALINA_HOME/bin

export LD_LIBRARY_PATH=$ORACLE_HOME/lib:/lib:/usr/lib;
export CLASSPATH=$ORACLE_HOME/jlib:$ORACLE_HOME/rdbms/jlib;

alias ob='cd $ORACLE_BASE'
alias oh='cd $ORACLE_HOME'
alias tns='cd $ORACLE_HOME/network/admin'
alias ch='cd $CATALINA_HOME'
alias envo='env | grep ORACLE'
alias sqld='rlwrap sqlplus "/as sysdba"'

umask 022

envo

Shell Script to Ping Hostnames and Send SMS if Host is not Available

We use the below procedure to ping hosts and based on availability send SMS to our team. The SMS is send by calling a web service from the BASH Shell script using curl 

The hosts are defined in a file called hosts.txt

— Shell Script —

#!/bin/bash
current_time=$(date “+%Y%m%d%H%M%S%3N”)
#### Above Date Format is in MilliSeconds Accuracy ###
cd /home/online
rm -rf /home/online/SMSHOST.TXT
date
#### Below Code Will Loop the Hosts.TXT file for the Ping Probes ####
cat /home/online/hosts.txt | while read output
do
ping -w 30 -c 1 $output > /dev/null
if [ $? -ne 0 ]
then
SMSTEXT=’The+Host+’$output’+is+not+reachable.+Please+Contact+the+Database+Team.’
echo “Host $output is Down”
curl “http://192.168.10.100:8080/SMSConnect/SendServlet?application=http_gw2&password=http_gw2&content=$SMSTEXT&destination=9998887190&source=97249&mask=COMPANY”
else
echo “Host $output is Up”
fi
done

 

— hosts.txt file containing list of hostnames —

proddb01

proddb02

proddb03

drdb01

drdb02

drdb03

Shell Script to Add Date to Filenames

Below Shell Script will append Date to all FileNames in Directory with extension .TXT. Also it will create a folder with the Date and move the files to it.

#!/bin/bash
FILE=”$(date +%Y%m%d)”
cd /bi_dataload
mkdir -p /bi_dataload/BI_$FILE
for f in *.TXT
do
mv -n “$f” /bi_dataload/BI_$FILE/”${f%%.*}”_”$FILE”.TXT
done

Linux Matrix Effects Shell Script

Sometimes to have a funky time on the BASH shell you can run the below shell script

[code language=”bash”]
#!/bin/bash
clear
virtcent=$((`tput lines`/2))
horcent=$((`tput cols`/2-10))
tput setf 6
tput cup $virtcent $horcent && echo "entering the matrix"
sleep 1
tput cup $virtcent $horcent && echo " "
sleep 1
tput cup $virtcent $horcent && echo "entering the matrix"
sleep 1
tput cup $virtcent $horcent && echo " "
sleep 1
tput cup $virtcent $horcent && echo "connecting. "
sleep .1
tput cup $virtcent $horcent && echo "connecting.. "
sleep .1
tput cup $virtcent $horcent && echo "connecting… "
sleep .1
tput cup $virtcent $horcent && echo "connecting…. "
sleep .1
tput cup $virtcent $horcent && echo "connecting….. "
sleep .1
tput cup $virtcent $horcent && echo "connecting…… "
sleep .1
tput cup $virtcent $horcent && echo "connecting……. "
sleep .1
tput cup $virtcent $horcent && echo "connecting……. "
sleep .1
tput cup $virtcent $horcent && echo "connecting……. "
sleep .1
tput cup $virtcent $horcent && echo "connecting…….. "
sleep .1
tput cup $virtcent $horcent && echo "connecting………"
sleep 1
tput cup $virtcent $horcent && echo " CONNECTED "
sleep 1
clear

echo -e "\033[2J\033[?25l"; R=`tput lines` C=`tput cols`;: $[R–] ; while true
do ( e=echo\ -e s=sleep j=$[RANDOM%C] d=$[RANDOM%R];for i in `eval $e {1..$R}`;
do c=`printf ‘\\\\0%o’ $[RANDOM%57+33]` ### http://easyoradba.com ###
$e "\033[$[i-1];${j}H\033[32m$c\033[$i;${j}H\033[37m"$c; $s 0.1;if [ $i -ge $d ]
then $e "\033[$[i-d];${j}H ";fi;done;for i in `eval $e {$[i-d]..$R}`; #[mat!rix]
do echo -e "\033[$i;${j}f ";$s 0.1;done)& sleep 0.05;done #(c) 2016 — [ ShadabMohammad ]
[/code]

Just chmod 750 on the script and execute

$ vi matrix.sh
$ chmod 750 matrix.sh
$ matrix.sh

This is a re-worked version of the shell script available below.

http://bruxy.regnet.cz/web/linux/EN/matrix-sh/

Shell Script to Monitor Oracle Tablespace and Send Email Alert Only If Threshold Exceeds

[code language=”bash”]

export MMSG=/tmp/$$.mail
export ADDR="email address here"
export ORACLE_HOME=/u02/app/oracle/product/11.2.0/dbhome_1
export ORACLE_SID=appdev1

file=/tmp/${$}_`date +%Y%m%d`

sqlplus -s system/password << .eof > $file
set pages 0
select df.tablespace_name tspace,
round(sum(fs.bytes)/(df.bytes) * 100) "%_free",
round(sum(fs.bytes)/(1024*1024)) free_ts_size,
df.bytes/(1024*1024) tot_ts_size
from dba_free_space fs, (select tablespace_name, sum(bytes) bytes from dba_data_files
group by tablespace_name ) df
where fs.tablespace_name = df.tablespace_name
group by df.tablespace_name, df.bytes
having round(sum(fs.bytes)/(df.bytes) * 100) < 20;
exit;
.eof
#list all datafile below
egrep "SOE|APEX|SYSAUX|SYSTEM|TEMP|UNDOTBS1|USERS|AUDIT_TBS|APP_ENCRYPTED" /tmp/${$}_`date +%Y%m%d` >/tmp/table${ORACLE_SID}.txt
check_stat=`cat /tmp/table${ORACLE_SID}.txt|wc -l`;
oracle_num=`expr $check_stat`
if [ $oracle_num -ne 0 ]
then
echo "tablespace less than 20% for $ORACLE_SID : $oracle_num" > $MMSG
mail -s "TABLESPACE WITH LESS THAN 20% FREE SPACE" $ADDR < $MMSG
mail -s "TABLESPACE WITH LESS THAN 20% FREE SPACE IN DATABASE $ORACLE_SID" $ADDR < /tmp/table$ORACLE_SID.txt
fi

rm -f $MMSG > /dev/null 2>&1
rm $file

[/code]

Simple Shell Script to Check Diskspace and Send Email on AIX

The below simple shell script will check all filesystems and create output file for only filesystems which are more than 80% full and send the log by email.

The below has been checked on AIX7.1 , not too Sure if it works on Solaris. I suppose the -P flag does not work on Solaris.

[code language=”bash”]

#!/bin/ksh
df -P | grep -v Capacity | awk ‘{if ($5 >= 80) {print $5 $6;}}’ > /home/root/diskspacelog
mailx -s "DiskSpace for Server Esb1" </home/root/diskspacelog sysadmin@company.com

[/code]

Shell Script to Monitor AIX Filesystem and Send Email

The Below Shell Script checks the Filesystem mount points and using AWK outputs all filesystem exceeding 90% space to a file called diskspacepoll. Once that is done the sed command removes any special character like ‘%’ from the output file and cleans it to a file called output.log

The next important logic is in the AWK block. Here a variable called pattern is defined using the threshold of 90%. Another variable called var is defined. This is your baseline metric. So it value of pattern exceeds var then the mail is dispatched else the script does nothing. You can put this in crontab as a every 5 minute job to continuously poll the filesystems and incase the threshold is exceed it will dispatch an email immediately to the admin

[code language=”bash”]

#!/bin/ksh
df -P | grep -v Capacity | awk ‘{if ($5 >= 90) {print $5;}}’ > /home/root/diskspacepoll
sed ‘s/[!@#\$%^&*()]//g’ /home/root/diskspacepoll > /home/root/output.log
####### AWK LOGICAL BLOCK #########
pattern=$(awk ‘$1 > 90 {print $1}’ /home/root/output.log)
var=90
if [[ $pattern > $var ]]
then
echo "Please Check with System Administrator" | mailx -s "90% Threshold of DiskSpace exceeded on Server 1 (ESB1)" sysadmin@company.com
fi

[/code]

How many cores is a process using in Linux

Script to check no. of cores being utilized by a process in linux. Save the script below as a shell script and execute:

[root@~]#  sh check_cpu_cores.sh  1111

#!/bin/bash
pids=()
while [ $# != 0 ]; do
        pids=("${pids[@]}" "$1")
        shift
done
if [ -z "${pids[0]}" ]; then
        echo "Usage: $0 <pid1> [pid2] ..."
        exit 1
fi
for pid in "${pids[@]}"; do
        if [ ! -e /proc/$pid ]; then
                echo "Error: pid $pid doesn't exist"
                exit 1
        fi
done
while [ true ]; do
        echo -e "\033[H\033[J"
        for pid in "${pids[@]}"; do
                ps -p $pid -L -o pid,tid,psr,pcpu,comm=
        done
        sleep 1
done