Background SSH using here document


 
Thread Tools Search this Thread
Top Forums Programming Background SSH using here document
# 1  
Old 04-27-2012
Background SSH using here document

I'm trying to use Here documents to pass several commands to a remote server over SSH. However I'm having problems. The code is as follows:

Code:
nohup ssh $i_hostname exec /usr/bin/ksh -s << EOF >> $AS_STOPSAP_LOG &
echo $i_instname;
ps -ef | grep name | grep ${i_instname} |grep -v grep
 
proc=$(ps -ef | grep name | grep ${i_instname} |grep -v grep
| awk '{print $2}')
 
$(
for j in $STARTSRV
do
echo loop checking item $j
kill -9 $j;
echo "Killed $j"
done
)
EOF

The problem seems to start with the fact that the "proc" variable does not seem to set correctly. I thought you could include $() for sub-shells via EOF docs? Any ideas anyone?
# 2  
Old 04-27-2012
where is the i_instname variable set?
# 3  
Old 04-27-2012
it's set in the greater programme so it's OK. The sub-script within $() is the problem I believe. It seems to me that it's evaluated before the here doc is passed to the SSH --- I want it to be run on the target server ...
# 4  
Old 04-27-2012
Either scp and script and at or batch it using ssh on the remote machine, or put the entire command inside single tics (red):
Code:
nohup ssh $i_hostname ' exec /usr/bin/ksh -s << EOF >> $AS_STOPSAP_LOG &
echo $i_instname;
ps -ef | grep name | grep ${i_instname} |grep -v grep
 
proc=$(ps -ef | grep name | grep ${i_instname} |grep -v grep
| awk '{print $2}')
 
$(
for j in $STARTSRV
do
echo loop checking item $j
kill -9 $j;
echo "Killed $j"
done
)
EOF'

This User Gave Thanks to jim mcnamara For This Post:
# 5  
Old 04-27-2012
I found a way to alter it - have to have here doc (EOF) in speech marks ; "EOF"
so that it's all sent as plain text (nothing is exapanded etc.) so that any commands run on the remote host - not the local one.
then pass parameters to it for anything that has to be dynamically set locally and passed in.

See this:
Code:
nohup ssh $i_hostname exec /usr/bin/ksh -s ${i_instname} << "EOF" >> $AS_STOPSAP_LOG &
i_instname=${1}
ps -ef | grep name | grep ${i_instname} |grep -v grep
 
procs=$(ps -ef | grep name | grep ${i_instname} |grep -v grep | awk '{print $2}')
 
for j in $procs
 do
 kill -9 $j;
 echo "Killed $j"
done
EOF

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. 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

2. Shell Programming and Scripting

Executing a background script using ssh keys

Greetings, i've been working with a user-friendly menu on ksh to allow users execute scripts located on a remote server, so they wont have to login and manually launch those scripts every single time. This is a HP-UX box and currently on a /usr/bin/ksh shell. I've setup ssh keys on both... (1 Reply)
Discussion started by: nbriozzo
1 Replies

3. Shell Programming and Scripting

Ssh in the background

Hi, I am trying to execute an ssh command in my script. ssh abcd@server_name After this command it actually logs in to the server asking for password prompt and then actually logs in to the server. I want all this to be happening in the background and show noithing in the output of my... (8 Replies)
Discussion started by: vital_parsley
8 Replies

4. Shell Programming and Scripting

ssh uses here-document problem

I try to ssh uses bash script in here-document like this. ssh root@$SERVER <<EOF if ; then service httpd start fi exit EOF But got an error below. maybe the if command causes the problem. Thanks for help. (2 Replies)
Discussion started by: muffle
2 Replies

5. Programming

Foreground sesu SSH with here document

I'm trying to write a foreground (password prompt) ssh command, passing in a here doc - any idea where I'm going wrong? ssh <user>@<targetserver> /usr/seos/bin/sesu - <targuser> -c /usr/bin/ksh "param1" << EOF date echo ${1} EOF Tells me "Invalid option param1". However a simple date... (8 Replies)
Discussion started by: doonan_79
8 Replies

6. Programming

Background (nohup * &) SSH command block possible?

Hello, I am trying to find a way to send several sequential commands via SSH to a remote box in a single command. Thoughts so far: 1) Can I put them into a function and call the function within the ssh command? e.g. ssh <targetserver> $(functionx) No - then it calls the function in... (4 Replies)
Discussion started by: doonan_79
4 Replies

7. Shell Programming and Scripting

PID from background ssh

Hello. I was wondering if someone can help me out with something. To simplify my life, I have written a tiny script to open an ssh tunnel through another linux host so that I can access the esxi hosts on that network using the client. For this I have to tunnel ports 443, 902, and 903. Here is what... (1 Reply)
Discussion started by: numetheus
1 Replies

8. Shell Programming and Scripting

Using ssh to execute a remote script in the background

Help please!! I want to use ssh to execute a remote exe and while it's running I want to query for the process ID of the exe (2 different ssh commands) 1. sshpass -p "<passwd>" ssh -f -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no root@<ipaddress> nohup /tmp/mmds_asyn & 2.... (0 Replies)
Discussion started by: rvompoluTMW
0 Replies

9. Shell Programming and Scripting

Here document inside a here document?

Can we use a here document inside a here document? Something like this ssh user@remotehost << REMOTE sudo vserver vsernamename enter << VSERVER perform actions on vserver. VSERVER REMOTE (6 Replies)
Discussion started by: mnanavati
6 Replies

10. Shell Programming and Scripting

ssh and here document

Hi :) how can I use here doc to use ssh? I am facing a problem with the below script: #!/bin/bash ssh hosein@localhost << * 123456 * "123456" is my password Thanks (2 Replies)
Discussion started by: htabesh
2 Replies
Login or Register to Ask a Question