LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 04-22-2011, 11:54 PM   #1
Monika32011
LQ Newbie
 
Registered: Apr 2011
Posts: 7

Rep: Reputation: 0
Question multiple command in ssh not working for me.


Hi

I have prepared a script which will login to each server and search for a keyword.
I want output on same machine from where m running script.

When i try to run command on any machine.. It works well.


w=$(grep -irH "keyword.com" /home/*/public_html/*);if [ $? -eq 0 ];then echo -e "\n $HOSTNAME";echo $(grep -irH "keyword.com" /home/*/public_html/* | cut -d: -f1 | uniq | awk '{for (i=1;i<=NF;i++) printf "%s \n",$i}');fi

but its not working in script with ssh
its gives below error:

script.sh: line 5: syntax error near unexpected token `('

Script is:

#!/bin/ksh
echo -e "Script to check and list if keyword is being used by any server \n" > /tmp/test.txt
for f in `cat /root/sync_script/serverlist1.txt`
do
ssh $f.exam5.com 'w=$(grep -irH "keyword.com" /home/*/public_html/*);if [ $? -eq 0 ];then echo -e "\n $HOSTNAME";echo $(grep -irH "keyword.com" /home/*/public_html/* | cut -d: -f1 | uniq | awk '{for (i=1;i<=NF;i++) printf "%s \n",$i}');fi' >> /tmp/test.txt
done




Please respond ASAP.

Thanks & Regards,
Monika

Last edited by Monika32011; 04-23-2011 at 04:11 AM. Reason: security
 
Old 04-23-2011, 12:02 AM   #2
xeleema
Member
 
Registered: Aug 2005
Location: D.i.t.h.o, Texas
Distribution: Slackware 13.x, rhel3/5, Solaris 8-10(sparc), HP-UX 11.x (pa-risc)
Posts: 988
Blog Entries: 4

Rep: Reputation: 254Reputation: 254Reputation: 254
Greetingz!
Try putting a space between your "$(" and "grep -irH"
 
Old 04-23-2011, 02:05 AM   #3
Monika32011
LQ Newbie
 
Registered: Apr 2011
Posts: 7

Original Poster
Rep: Reputation: 0
Tried, But still no luck :-(
 
Old 04-23-2011, 04:48 AM   #4
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405
Hi,

Try changing ' to " for the ssh command. I.e: ssh $f.exam5.com "........." instead of ssh $f.exam5.com '.........'

Hope this helps.

Don't demand an immediate answer, we are all volunteers here at LQ.....

Last edited by druuna; 04-23-2011 at 04:51 AM.
 
Old 04-23-2011, 06:13 AM   #5
Monika32011
LQ Newbie
 
Registered: Apr 2011
Posts: 7

Original Poster
Rep: Reputation: 0
Have already tried that, it will execute script on same server but wont do ssh :-(
Have also tried putting back tick (`) instead of (') and without anything as well.. gives same result, executes on same server.

I think it is getting confused between 2 pair of quotes ('), but is required at both places for ssh and awk.
It gives lot of error if I remove quotes from awk command.

Please advise.
 
Old 04-23-2011, 06:32 AM   #6
markush
Senior Member
 
Registered: Apr 2007
Location: Germany
Distribution: Slackware
Posts: 3,979

Rep: Reputation: Disabled
Hello Monika,

unfortunately I cannot help whith your question. But I would recommend to use the "report" button (at one of your posts) and ask a Moderator to move this thread to the "Programming" part of LQ http://www.linuxquestions.org/questions/programming-9/. I think you will find more help there.

Markus
 
Old 04-23-2011, 07:25 AM   #7
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405
Hi,

Looking at the script I do wonder if this isn't too complicated for what you have in mind.

I do believe you want to check, on different hosts, if a certain string is present in multiple files and if this is the case you want to print the host name and the file name(s).

If my above assumption is correct, try the following:
Code:
#!/bin/ksh

echo -e "Script to check and list if keyword is being used by any server \n" > /tmp/test.txt

for HOST in `cat /root/sync_script/serverlist1.txt`
do
  ssh $HOST.exam5.com ' OUTPUT=$(grep -irH "keyword.com" /home/*/public_html/*) ; if [ $? -eq 0 ] ; then echo -e "\n $HOSTNAME" ; echo $OUTPUT | cut -d: -f1 ; fi' >> /tmp/test.txt
done
This is based on assumptions, I don't know what the generated input and output is in your case and which info you want to see. This also means the above solution is untested.

Anyway, hope this helps.
 
Old 04-23-2011, 07:44 AM   #8
fbianconi
Member
 
Registered: Apr 2008
Location: argentina
Distribution: Arch
Posts: 86

Rep: Reputation: 22
you sohuld escape the inner pair of single quotes, like this:

#!/bin/ksh
echo -e "Script to check and list if keyword is being used by any server \n" > /tmp/test.txt
for f in `cat /root/sync_script/serverlist1.txt`
do
ssh $f.exam5.com 'w=$(grep -irH "keyword.com" /home/*/public_html/*);if [ $? -eq 0 ];then echo -e "\n $HOSTNAME";echo $(grep -irH "keyword.com" /home/*/public_html/* | cut -d: -f1 | uniq | awk \'{for (i=1;i<=NF;i++) printf "%s \n",$i}\');fi' >> /tmp/test.txt
done

Last edited by fbianconi; 04-23-2011 at 07:45 AM. Reason: emphasis
 
Old 04-23-2011, 01:47 PM   #9
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
Moved: This thread is more suitable in Programming and has been moved accordingly to help your thread/question get the exposure it deserves. As per kind request.
 
Old 04-26-2011, 08:05 AM   #10
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
Most likely the problem is - as already noticed - the opening single quote is actually closed by the opening single quote of the awk command. To prevent this behaviour you need to let the shell close it, then pass a literal single quote embedded in double quotes. Do the same for the closing single quote of the awk command and the trick is done:
Code:
ssh $f.exam5.com 'w=$(grep -irH "keyword.com" /home/*/public_html/*);if [ $? -eq 0 ];then echo -e "\n $HOSTNAME";echo $(grep -irH "keyword.com" /home/*/public_html/* | cut -d: -f1 | uniq | awk '"'"'{for (i=1;i<=NF;i++) printf "%s \n",$i}'"'"');fi' >> /tmp/test.txt
Toward the end of the line see the two sequences
Code:
'"'"'
that mean close the single quote, pass a literal single quote embedded in double quotes, re-open the single quotes. Hope this helps.
 
  


Reply

Tags
ssh remote



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
SSH not working after ssh-keygen due to lack of entropy grob115 Linux - Security 8 08-28-2010 11:33 AM
Chroot SSH problem: ssh working, not SFTP & SCP. NaCo Linux - Security 3 02-01-2009 02:23 AM
LXer: ssh on multiple servers Using cluster ssh LXer Syndicated Linux News 0 01-11-2008 03:40 PM
command not working in SSH wincrk Linux - Networking 2 04-26-2006 10:09 PM
Working with SSH and multiple private keys IgD Linux - Security 2 09-08-2003 10:07 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 06:15 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration