AloneInTheDark
AloneInTheDark

Reputation: 938

Running multiple commands with parameters in ssh

I'm trying to run some commands from ssh. There are multiple variables in each remote server.

for db_serv in `olsnodes`; do
   for db_ins in `ssh $db_serv ps -ef | grep ora_pmon_ | grep -v grep | cut -d"_" -f3`; do
   SessionSayisi=`ssh $db_serv ps -ef | grep $db_ins | grep 'LOCAL=NO'|grep -vc ASM`
   echo "SessionSayisi= $SessionSayisi $db_ins"
   done
done

This works well but takes long time because the command is running ssh for each variable. I need to connect only one time for each server, then get all variable outputs, then ssh to another.

Here is what i tried but it didn't work:

 for db_serv in `olsnodes`; do
ssh $db_serv ps -ef | grep ora_pmon_ | grep -v grep | cut -d"_" -f3 << EOF
for db_ins in `ps -ef | grep ora_pmon_ | grep -v grep | cut -d"_" -f3`; do
echo "SessionSayisi= $SessionSayisi $db_ins"
done
EOF
done

Upvotes: 0

Views: 472

Answers (1)

Josh Jolly
Josh Jolly

Reputation: 11796

It looks like you only need to run one SSH command - if you capture the output in a variable, then you can run the rest of your commands on that variable instead of on the remote machine:

for db_serv in $(olsnodes); do
    procs=$(ssh "$db_serv" ps -ef)
    for db_ins in $(echo "$procs" | grep ora_pmon_ | grep -v grep | cut -d"_" -f3); do
        SessionSayisi=$(echo "$procs" | grep "$db_ins" | grep 'LOCAL=NO'| grep -vc ASM)
        echo "SessionSayisi= $SessionSayisi $db_ins"
    done
done

Upvotes: 1

Related Questions