Running command using SSH


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Running command using SSH
# 1  
Old 06-28-2013
Running command using SSH

Hi All,

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

Code:
 
 
#!/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?
# 2  
Old 06-28-2013
your issue is that the script is checking for the directories on the local server ...
Code:
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

This User Gave Thanks to Just Ice For This Post:
# 3  
Old 06-28-2013
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

Code:
 
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

# 4  
Old 06-28-2013
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 ...
Code:
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

This User Gave Thanks to Just Ice For This Post:
# 5  
Old 06-28-2013
Thanks again expert.

I have written like
Code:
 
 
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.

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

# 6  
Old 06-28-2013
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 ...
Code:
scp -p /dir/script user@host:/tmp/script
ssh -n user@host "hostname; /tmp/script; rm /tmp/script" > /dir/log

# 7  
Old 06-28-2013
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
Code:
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.
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