Running command using SSH

Hi All,

I am trying to run my script using ssh. Below is the code snippet

 
 
#!/bin/bash
ssh username@useipapd03 'bash -s'
#To ensure If JMSdirectory exists or not
if [ -d /opt/shared -a -w /opt/shared/data/jms ] 
then 
echo "JMS mounts................exist'>>OutputResult.txt
else 
echo "JMS mounts................not exist">>OutputResult.txt
fi

It is not working. Could you please suggest me how to run the SSH command using above code?

your issue is that the script is checking for the directories on the local server ...

ssh -n user@host "hostname;[ -d /opt/shared -a -w /opt/shared/data/jms ] && echo 'JMS mounts exist' || echo 'JMS mounts do not exist'" >> OutputResult.txt
1 Like

Thanks Just Ice,

One question, Is this command log the OutputResult.txt on the local server?

Thats what I can see after running the command.

---------- Post updated at 12:46 AM ---------- Previous update was at 12:01 AM ----------

Hi Just Ice,

One more help, how to put below snippet of code for making it work for SSH command

 
CLECount=$(ps -ef |grep ELINKDEV |grep CLE |grep -v grep| wc -l)
PurgeDataCount=$(ps -ef |grep ELINKDEV |grep PurgeData |grep -v grep| wc -l)
if [[ "$CLECOUNT" = "3" && "$PurgeDataCount" = "1" ]]
then
echo "CLE Components................Running">>OutputResult.txt
else
echo "CLE Components................Not Running">>OutputResult.txt
fi

the ssh command as written writes the log file locally so access to the log file is quicker ... you can always leave the log file on the remote server by moving the >> OutputResult.txt within the double-quotes of the ssh command ...

ssh -n user@host "some_test && echo ok || echo not_ok >> OutputResult.txt"  # <-- logfile on local server

ssh -n user@host "some_test && echo ok || echo not_ok" >> OutputResult.txt  # <-- logfile on remote server
1 Like

Thanks again expert.

I have written like

 
 
ssh -n username@useipapd03 "hostname; Count=$(ps -ef |grep ELINKDEV |grep Purge |grep -v grep| wc -l); PurgeDataCount=$(ps -ef |grep ELINKDEV |grep PurgeData |grep -v grep| wc -l) && echo '$COUNT'" >> OutputResult.txt

In the OutputResult am not able to see the $Count value through script where in command promt am getting the count value as 1.

 
ps -ef |grep ELINKDEV |grep Purge |grep -v grep| wc -l

when you have more than a few commands to run, it is better to put it in a script ... scp the script to the remote server, run it via ssh and delete remote copy so you do not have to maintain more than one copy ...

scp -p /dir/script user@host:/tmp/script
ssh -n user@host "hostname; /tmp/script; rm /tmp/script" > /dir/log

Thanks again.But than the issue will be log file will generate on remote servers. Suppose i have to execute the same script on differnt 5 servers than log file will be generated 5 times and user have to look into 5 differnt files to know the results. My requirement is to execute the script from local server and get the results also in one single file at local server.Something like below

ssh -n user@host1 "some_test && echo ok || echo not_ok >> OutputResult.txt" 
ssh -n user@host2"some_test && echo ok || echo not_ok >> OutputResult.txt" 
ssh -n user@host3 "some_test && echo ok || echo not_ok >> OutputResult.txt"

But I do have some little bit of logic also in between.

create checkscript first, scp it over to the remote servers and run ... all output from all remote script runs will be in local /dir/log ...

checkscript <-- to run at remote servers

#! /bin/ksh
hostname
echo "=============="
CLECount=$(ps -ef |grep ELINKDEV |grep CLE |grep -v grep| wc -l)
PurgeDataCount=$(ps -ef |grep ELINKDEV |grep PurgeData |grep -v grep| wc -l)
if [[ "$CLECOUNT" = "3" && "$PurgeDataCount" = "1" ]]
then
echo "CLE Components................Running"
else
echo "CLE Components................Not Running"
fi
echo
exit 0

runscript <-- run locally on command line

#! /bin/ksh
LOG=/dir/log
SCRIPT=checkscript

for HOST in host1 host2 host3 host4
do
    scp -p $SCRIPT user@${HOST}:/tmp/${SCRIPT}
    ssh -n user@${HOST} "/tmp/${SCRIPT} && rm /tmp/${SCRIPT}"
done > $LOG 2>&1

exit 0

local /dir/log entries

host1
==============
CLE Components................Running

host2
==============
CLE Components................Running

host3
==============
CLE Components................Running

host4
==============
CLE Components................Running

Thanks Ice Age.

See I have different code snippets in my script file which i need to run at different servers

For eg. Below is my script file

 
# Run this code in Server 1
 
CLECount=$(ps -ef |grep ELINKDEV |grep CLE |grep -v grep| wc -l)
PurgeDataCount=$(ps -ef |grep ELINKDEV |grep PurgeData |grep -v grep| wc -l)
if [[ "$CLECOUNT" = "3" && "$PurgeDataCount" = "1" ]]
then
echo "CLE Components................Running"
else
echo "CLE Components................Not Running"
fi
echo
exit 0

 
# Run this on Server 2
Count=$(ps -ef |grep ELINKDEV |grep engine |grep -v grep| wc -l)
if [ "$Count" = "0" ];
then
            echo "ALL Components................Stopped">>OutputResult.txt
            else
            echo "Components................Running">>OutputResult.txt
fi
 
#Run this on server 3
 
Count=$(ps -ef |grep ELINKDEV |grep Serverengine |grep -v grep| wc -l)
if [ "$Count" = "0" ];
then
            echo "ALL Server Components................Stopped">>OutputResult.txt
            else
            echo "Server Components................Running">>OutputResult.txt
fi

As you suggested above It will run to the host 1, host 2. host3 all the commnds but i want to run specfic command to specific server.

I am really looking forward towards you as It seems am very close to get solution from you.

just create all those scripts separately -- remove all the output redirections as per earlier post -- and list them in a file with the hostnames (see sample below) ... then run the scp-ssh-log routine as suggested ...

hostscript.txt

host1:clescript
host2:compscript
host3:svrsrcipt

runscript

#! /bin/ksh
HSFILE=/dir/hostscript.txt
LOG=/dir/log

for HOST in $(awk -F":" '{print $1}' $HSFILE)
do
    SCRIPT=$(awk -F":" "\$2 ~ /$HOST/ {print \$2}" $HSFILE)
    scp -p $SCRIPT user@${HOST}:/tmp/${SCRIPT}
    ssh user@${HOST} "/tmp/${SCRIPT} && rm /tmp/${SCRIPT}"
done > ${LOG} 2>&1

exit 0

Thansk ICE Age,

Actually for maintaince purpose, we dont want to keep multiple scripts file to invoke for differnt servers
In HostScript.txt, I have to invoke single script file on multiple hosts which will be having different logic for differnt server.

Is it the way to pass the variable which contains the host name to the main script(assume host name) and there I will put logic to run only those parts of scripts for particular host name.