LinuxQuestions.org
Help answer threads with 0 replies.
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-04-2013, 01:23 PM   #1
schneidz
LQ Guru
 
Registered: May 2005
Location: boston, usa
Distribution: fedora-35
Posts: 5,313

Rep: Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918
[bash] consolidate multiple ssh commands into 1


hi, i have a bash script that contains this:
Code:
...
f=`ssh hyper ls -1 /path/to/some/file | tail -n 1`
f=`ssh hyper grep $argv1 $f | cut -d \| -f 7 | grep hello-world | egrep -v "(l33t|h4x0rz)" | uniq`
fn=$(echo $f.gz | sed s/chun-li/chun-li\\/akuma/)
scp $USER@hyper:$fn .
...
seems kinda' inefficient to call ssh twice... i tired embedding them using `backticks`, "'quotes'", (parenthesis) but nothing seems to work. is this possible or should i just live with it ?

thanks,
schneidz
 
Old 04-04-2013, 02:00 PM   #2
mina86
Member
 
Registered: Aug 2008
Distribution: Debian
Posts: 517

Rep: Reputation: 229Reputation: 229Reputation: 229
Code:
f=`ssh hyper ls -1 /path/to/some/file | tail -n 1`
How does the effoct of the above line differ from:
Code:
f=/path/to/some/file
 
Old 04-04-2013, 02:46 PM   #3
schneidz
LQ Guru
 
Registered: May 2005
Location: boston, usa
Distribution: fedora-35
Posts: 5,313

Original Poster
Rep: Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918
Quote:
Originally Posted by mina86 View Post
Code:
f=`ssh hyper ls -1 /path/to/some/file | tail -n 1`
How does the effoct of the above line differ from:
Code:
f=/path/to/some/file
hi mina, becuz the last file in that directory will be date-stamped every friday (e.g.- log_2013-03-29.txt , last week the file would be log_2013-03-22.txt).

fyi, this is on an aix machine.
 
Old 04-04-2013, 06:37 PM   #4
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,007

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
Well my first impression is why do you use both `` and $()? I personally prefer the second but it does not seem to make it easy to follow if you change in and out of the two.

As for calling the ssh once, I believe the parenthesis option should work:
Code:
f=$(ssh hyper grep $argv1 $(ls -1 /path/to/some/file | tail -n 1) | cut -d \| -f 7 | grep hello-world | egrep -v "(l33t|h4x0rz)" | uniq)
It would appear that the several greps and cut seem a bit excessive. I know it is aix but I would have thought even its awk would do this a bit cleaner.
 
Old 04-05-2013, 12:09 AM   #5
konsolebox
Senior Member
 
Registered: Oct 2005
Distribution: Gentoo, Slackware, LFS
Posts: 2,248
Blog Entries: 8

Rep: Reputation: 235Reputation: 235Reputation: 235
Quote:
Originally Posted by grail View Post
Code:
$(ls -1 /path/to/some/file | tail -n 1)
I don't know why the OP has to but won't the command be not called remotely for that?

@schneidz

I think you could try sending a script or multiple commands to bash:
Code:
ssh hyper bash < script.sh
cat script.sh | ssh hyper bash
echo "grep \"$argv1\" \$(ls -1 /path/to/some/file | tail -n 1) | cut -d | -f 7 | grep hello-world | egrep -v \"(l33t|h4x0rz)\" | uniq" | ssh hyper bash
ssh hyper bash <<< "grep \"$argv1\" \$(ls -1 /path/to/some/file | tail -n 1) | cut -d | -f 7 | grep hello-world | egrep -v \"(l33t|h4x0rz)\" | uniq"
And place any of those statements in f=$(). Or if you could perhaps in fn=$() already. You should just note that everything sent to remote bash would be executed remotely as is.

Add: Sometimes you could also just use other shells. You just have to make your commands would work with them.

Last edited by konsolebox; 04-05-2013 at 12:14 AM.
 
1 members found this post helpful.
Old 04-05-2013, 12:36 AM   #6
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,007

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
Quote:
I don't know why the OP has to but won't the command be not called remotely for that?
I thought that was the point, that all commands would be executed remotely?
 
Old 04-05-2013, 03:31 AM   #7
konsolebox
Senior Member
 
Registered: Oct 2005
Distribution: Gentoo, Slackware, LFS
Posts: 2,248
Blog Entries: 8

Rep: Reputation: 235Reputation: 235Reputation: 235
Quote:
Originally Posted by grail View Post
I thought that was the point, that all commands would be executed remotely?
Yeah and what I meant was that the command won't be called remotely but on the shell that calls ssh.
 
Old 04-05-2013, 07:34 AM   #8
mina86
Member
 
Registered: Aug 2008
Distribution: Debian
Posts: 517

Rep: Reputation: 229Reputation: 229Reputation: 229
Quote:
Originally Posted by schneidz View Post
hi mina, becuz the last file in that directory will be date-stamped every friday (e.g.- log_2013-03-29.txt , last week the file would be log_2013-03-22.txt).
Ah, so /path/to/some/file is really /path/to/some/directory.

You might try this:
Code:
fn=$(cat '
f=$(ls -1 /path/to/some/file | tail -n 1)
f=$(grep "$1" "$f" | cut -d \| -f 7 | grep hello-world | egrep -v "(l33t|h4x0rz)" | uniq)
echo $(echo "$f.gz" | sed s/chun-li/chun-li\\/akuma/)
' | ssh hyper sh -s "$argv1")
Not tested, not sure if -s is POSIX compliant.

Last edited by mina86; 04-05-2013 at 07:36 AM.
 
Old 04-05-2013, 08:39 AM   #9
schneidz
LQ Guru
 
Registered: May 2005
Location: boston, usa
Distribution: fedora-35
Posts: 5,313

Original Poster
Rep: Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918
Quote:
Originally Posted by grail View Post
Well my first impression is why do you use both `` and $()? I personally prefer the second but it does not seem to make it easy to follow if you change in and out of the two.

As for calling the ssh once, I believe the parenthesis option should work:
Code:
f=$(ssh hyper grep $argv1 $(ls -1 /path/to/some/file | tail -n 1) | cut -d \| -f 7 | grep hello-world | egrep -v "(l33t|h4x0rz)" | uniq)
It would appear that the several greps and cut seem a bit excessive. I know it is aix but I would have thought even its awk would do this a bit cleaner.
i think with this i got path not found errors becuz it was looking for /path/to/some/file on the local machine (which doesnt exists).

i think konsolebox's idea of piping into ssh looks promising.

just had another clever idea; maybe running find with -mtime -7 (each friday) and -exec cut ... grep ... uniq. maybe i can even cat the contents of the scraped filename and redirect it to a file on the local machine rather than calling scp independently.
 
Old 04-05-2013, 09:44 AM   #10
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,007

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
Sorry konsole ... my bad ... i get you now
 
  


Reply



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
How to execute multiple commands via SSH in a script angrymachine Linux - General 4 10-12-2011 10:05 AM
can ssh execute multiple commands at the same time? Singist Linux - Networking 2 04-04-2006 09:14 AM
execute multiple ssh remote commands tom221 Linux - Newbie 2 01-28-2005 01:00 PM
executing multiple commands by ssh jpan Linux - General 1 10-22-2004 02:12 PM
remote ssh commands on multiple hosts evilchild Linux - Software 6 08-12-2004 10:48 PM

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

All times are GMT -5. The time now is 11:06 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