Tuesday, 28 January 2020

running script in remote machine using ssh and redirecting output to local


If i want to run a script in remote server using ssh, the best method is copy using scp to any temp location in remote server then run using ssh, Lets say if you have numer of DB servers you want to check backup log status of all the servers and generate a report to get mail everyday. this post will be usefull.

in this case i want to run ora_fullbkp_check.sh in remote server and redirect output to local. this script will send hostname, db name, date, status

#!/bin/bash
Logfilecheck() {
DBNAME=`cat $LOGFILE|grep DBID|awk '{ print $5 }'`
DAY=`ls -lrt /u01/RMAN/logs/*FULL*|tail -1 |awk '{ print $6 $7}'`
ERRORLIST=`egrep "^RMAN-[0-9]*:|^ORA-[0-9]*:" $LOGFILE`
if [ -n "$ERRORLIST" ]
    then
        echo -e "`hostname`  $DBNAME  $DAY    Full_Backup    Failed"
    else
        echo -e "`hostname`  $DBNAME  $DAY    Full_Backup    success"
    fi
}

LOGFILE=`ls -lrt /u01/RMAN/logs/*FULL*|tail -2|head -1 |awk '{ print $9 }'`
if [ -n "$LOGFILE" ]; then
        Logfilecheck;
        else
        echo -e "`hostname`  NA  NA  Full_Backup    No_FUll_Backups"
        exit;
fi

we can run above script as below first scp then run using ssh, example as below:

scp ora_fullbkp_check.sh 192.168.52.101:/tmp/
ssh 192.168.52.101 "sh /tmp/ora_fullbkp_check.sh" > ora_full_bkp_report.log


output file ora_full_bkp_report.log will create locally and content as below.

test_serv oradb Jan28 success

oyou can put it in differnt shell script and run both commands in at a time as below.

cat  get_bkp_status.sh
scp ora_fullbkp_check.sh 192.168.52.101:/tmp/
ssh 192.168.52.101 "sh /tmp/ora_fullbkp_check.sh" > ora_full_bkp_report.log

No comments:

Post a Comment