Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to run multiple commands on a remote linux server using bash script

Tags:

bash

I am currently writing the following script that logs into a remote server and runs couple of commands to verify the performance of the server and prints a message based on the output of those commands .But the ssh doesn't work and returns the stats of the server that hosts the script instead .

Script

#!/bin/bash
#######################
#Function to add hosts to the array
#the following function takes the ip addresses provided while the       script is run and stores them in an array
#######################
Host_storing_func () {
  HOST_array=()
  for i in $@ ;do
    HOST_array+=(${i});
  done
  #echo ${HOST_array[*]}
}

#######################
#Calling above function
#######################
Host_storing_func "$@"

############################################################
#Collect Stats of Ping,memory,iowait time test function
############################################################
b=`expr ${#HOST_array[*]} - 1 `
for i in `seq 0 $b` ;do
  sshpass -f /root/scripts/passwordFile.txt /usr/bin/ssh  student35@${HOST_array[${i}]}   <<   HERE
    echo `hostname`
    iowaittm=`sar 2 2|awk '/^Average/{print $5};'`
    if [ $iowaittm > 10 ];then
     echo "IO ==> BAD"
    else
     echo "IO ==> GOOD"
    fi
    memoryy=`free -m |grep Swap|awk '{if($2 == 0) print 0;else print (($4 /  $2 ) * 100)}'`
    if [ ${memoryy} < '10' ] ;then
     echo "memory ==> good"
    elif [[ "${memory}" -ge 0 ]] && [[ "${memory}" -le 10 ]];then
     echo "No Swap"
    else
     echo "memory ==> bad"`enter code here`
    fi
    ping -w2 -c2 `hostname` | grep "packet loss"|awk -F, '{print $3}'|awk -F% '{print $1}'|sed 's/^ *//'|awk '{if ($1 == 0) print "Yes" ;else print "No"}'
    HERE
done

Output : oc5610517603.XXX.com is the name of the source server

[root@oc5610517603 scripts]# ./big_exercise.sh 9.XXX.XXX.XXX 9.XXX.XXX.XXX
Pseudo-terminal will not be allocated because stdin is not a terminal.
oc5610517603.XXX.com 
IO ==> GOOD
No Swap
ping: oc5610517603.ibm.com: Name or service not known
Pseudo-terminal will not be allocated because stdin is not a terminal.
oc5610517603.XXX.com
IO ==> GOOD
No Swap
ping: oc5610517603.XXX.com: Name or service not known
like image 447
mourya nadella Avatar asked Jul 10 '26 00:07

mourya nadella


1 Answers

thanks for checking the script , I figured out a way to solve the problem

It is the sshpass command that is causing issue , you just have to put the opening HERE in single quotes if you want to use variables with in the HEREdoc but if the variables are calculated before ssh then you don't have to put opening HERE in single quotes

sshpass -f /root/scripts/passwordFile.txt /usr/bin/ssh -T student35@${i} << 'HERE'

after I changed the sshpass command as above my script worked

like image 72
mourya nadella Avatar answered Jul 12 '26 04:07

mourya nadella



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!