LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.

Notices


Reply
  Search this Thread
Old 11-02-2010, 06:02 AM   #1
genderbender
Member
 
Registered: Jan 2005
Location: US
Distribution: Centos, Ubuntu, Solaris, Redhat
Posts: 396

Rep: Reputation: 31
complex for loop executed remotely via ssh


I've got a fairly complex bit of code (a for loop within a for loop) which I'm trying to execute remotely, however because of the special characters this exits without making the connection.

Can anyone give me a hand and suggest how I'd go about sorting this?

Usually I'd do:

ssh root@ipaddress "for i in `cat /etc/hosts | awk '{print $2}'`; do ping $i; done;"

But given the complexity of my code this usual tactic doesn't work
 
Click here to see the post LQ members have rated as the most helpful post in this thread.
Old 11-02-2010, 06:31 AM   #2
neonsignal
Senior Member
 
Registered: Jan 2005
Location: Melbourne, Australia
Distribution: Debian Bookworm (Fluxbox WM)
Posts: 1,391
Blog Entries: 54

Rep: Reputation: 360Reputation: 360Reputation: 360Reputation: 360
Quote:
Originally Posted by genderbender View Post
ssh root@ipaddress "for i in `cat /etc/hosts | awk '{print $2}'`; do ping $i; done;"
Often you want single quotes around the whole thing, so that there is no parameter expansion before it gets passed to the remote machine by ssh, otherwise you would need to escape the $ characters.

Code:
ssh root@ipaddress 'for i in $(cat /etc/hosts | awk '\''{print $2}'\''); do ping $i; done'
When you need a single quote inside the statement, you have to exit the quotes, escape the single quote, and then reenter the quotes (hence the '\'' sequences). The $(...) is an alternative to the backquotes which can sometimes be more readable.

Incidentally, you will also want to limit the number of pings or it will never finish, and sometimes the /etc/hosts file has lines that might confuse this script.

Last edited by neonsignal; 11-02-2010 at 06:35 AM.
 
2 members found this post helpful.
Old 11-02-2010, 06:37 AM   #3
evo2
LQ Guru
 
Registered: Jan 2009
Location: Japan
Distribution: Mostly Debian and CentOS
Posts: 6,724

Rep: Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705
How about this?

Code:
ssh root@ipaddress 'for i in $( cat /etc/hosts | awk "{ print \$2 }" ) ; do ping $i ; done'
Evo2.

Last edited by evo2; 11-02-2010 at 06:38 AM. Reason: s/echo/ping
 
2 members found this post helpful.
Old 11-02-2010, 06:46 AM   #4
genderbender
Member
 
Registered: Jan 2005
Location: US
Distribution: Centos, Ubuntu, Solaris, Redhat
Posts: 396

Original Poster
Rep: Reputation: 31
My friend that works an absolute treat, much appreciated
 
  


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
[SOLVED] Xwindow's program will not run when executed on boot or when executed remotely richman1234 Programming 2 10-08-2010 01:32 PM
ssh record executed commands??? joangopan Linux - Newbie 4 05-13-2009 10:05 AM
can't login using ssh (via Putty) remotely blinton25 Linux - Software 11 05-27-2007 01:17 AM
Complex bash loop. hepburnenthorpe Linux - General 2 11-19-2006 09:42 AM
problems with X-apps when executed on remote host via ssh numerous Linux - Software 1 01-17-2005 07:08 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Software

All times are GMT -5. The time now is 05:48 PM.

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