Running command using SSH


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Running command using SSH
# 8  
Old 06-28-2013
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
Code:
#! /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
Code:
#! /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
Code:
host1
==============
CLE Components................Running

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

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

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

# 9  
Old 06-28-2013
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

Code:
 
# 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.
# 10  
Old 06-28-2013
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
Code:
host1:clescript
host2:compscript
host3:svrsrcipt

runscript
Code:
#! /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

# 11  
Old 07-01-2013
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.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Issue with running a script via ssh

Hi, I'm trying to run a user defined shell script with options and arguments via ssh but getting error as ksh: Script.sh: not found. Here is what i'm running: ssh -t username@server 'cd /path/to/script; script.sh -t start here '-t' with script.sh, is an user defined option and 'start' is also... (3 Replies)
Discussion started by: xsam
3 Replies

2. UNIX for Dummies Questions & Answers

Script still running after ssh

I have the lines below on my script: script.ksh: case `hostname` in some_host) ssh server1A "/home/script.ksh $1 $2" ssh server1B "/home/script.ksh $1 $2" ssh server1C "/home/script.ksh $1 $2" ssh server1D "/home/script.ksh $1 $2" ssh... (1 Reply)
Discussion started by: erin00
1 Replies

3. Shell Programming and Scripting

Running test command in ssh

I tried to run the following commands in my shell script. This command works fine for me: ssh -n "${username}"@"${hostname}" "grep WARNING ${serverhome}/${serverlog} | wc -l" The result is: 1548 However when i try to run a similar one: ssh -n "${username}"@"${hostname}" "test `grep -q... (2 Replies)
Discussion started by: hys1117
2 Replies

4. UNIX for Dummies Questions & Answers

Sudo ssh with command running in background

I am trying to run a command. This is one of my attempts: for i in fileservera; do ssh -t $i 'sudo ls /';doneThis works, and I see the directories. However, what I want to do now is start a process on the remote server such as /usr/bin/connectproc -standalonesudo /usr/bin/connectproc... (1 Reply)
Discussion started by: newbie2010
1 Replies

5. Shell Programming and Scripting

ssh - running remote command not exiting

Hi, i have a shellscript, where in i need to connect to different server start these three jobs and exit from the server and start the same three jobs on local server. ssh user@remotehost 'bash -s' << EOF ${GETT_HOME}/bin/start1 & sleep 10 ${GETT_HOME}/bin/start2 & sleep 10... (1 Reply)
Discussion started by: swapnabhargav
1 Replies

6. Shell Programming and Scripting

Running script and commands through SSH

Hello All, I am trying to run some simulations through SSH from my mac on our university SOLARIS system. My problem is that whenever I want to execute a command I get an error which says "Invalid argument". Maybe I should explain more what I want to do and what I did. Firstly I installed a... (10 Replies)
Discussion started by: Apfik
10 Replies

7. Shell Programming and Scripting

ssh running on function

i have a problem regarding running an ssh command while inside "function" script structure #!/usr/bin/bash func1(){ script=$1 ssh user@server "$script" } cat script.txt | while read line do func1 $line done exit 0 the problem is when ssh ran,... (1 Reply)
Discussion started by: ryandegreat25
1 Replies

8. Shell Programming and Scripting

Cron job giving error while running SSH command

Hi All, The script which i am using to SSH to remote server is working fine when i run is using ./ but when cron runs it it gives error that "ssh: not found" please help!!! (3 Replies)
Discussion started by: visingha
3 Replies

9. Shell Programming and Scripting

is running this command via ssh possible? (formatting issues)

Here is the command I want to run: for pkg in `pkginfo | grep -i VRTS | awk '{print $2}'`; do showrev -p | grep $pkg; done | awk '{print $2 "\t" $7}' | uniq It returns the package info in a form such as: 113210-03 VRTSfspro 112392-06 VRTSvmman 113596-03 VRTSvmpro... (1 Reply)
Discussion started by: LordJezo
1 Replies

10. Cybersecurity

running NIS over ssh

Hi Guys, Is it possible to run NIS over ssh as I want to run NIS without the security risks that would normally follow? If you have got any suggestions please share them with me. Andy H (2 Replies)
Discussion started by: Andy Hibbins
2 Replies
Login or Register to Ask a Question