user1356163
user1356163

Reputation: 407

running multiple commands through ssh and storing the outputs in different files

i've set up my public and private keys and have automated ssh login. I want to execute two commands say command1 and command2 in one login session and store them in files command1.txt and command2.txt on the local machine.

i'm using this code

ssh -i my_key user@ip 'command1 command2' and the two commands get executed in one login but i have no clue as to how to store them in 2 different files.

I want to do so because i dont want to repeatedly ssh into my remote host.

Upvotes: 3

Views: 15232

Answers (9)

kendall
kendall

Reputation: 23

You can do this. Assuming you can set up authentication from the remote machine back to the local machine, you can use ssh to pipe the output of the commands back. The trick is getting the backslashes right.

ssh remotehost command1 \| ssh localhost cat \\\> command1.txt \; command2 \| ssh localhost cat \\\> command2.txt 

Or if you aren't so into backslashes...

ssh remotehost 'command1 | ssh localhost cat \> command1.txt ; command2 | ssh localhost cat \> command2.txt' 

Upvotes: 1

Mihai Timar
Mihai Timar

Reputation: 1160

option 1. Tell your boss he's being silly. Unless, of course, he isn't and there is critical reason of needing it all in one session. For some reason such a case escapes my imagination.

option 2. why not tar?

ssh -i my_key user@ip 'command1 > out1; command2 > out2; tar cf - out*' | tar xf -

Upvotes: 1

MLSC
MLSC

Reputation: 5972

I think this is what you need: At first you need to install sshpass on your machine. then you can write your own script:

while read pass port user ip; do
sshpass -p$pass ssh -p $port $user@$ip <<ENDSSH1
    COMMAND 1  > file1
    .
    .
    .
    COMMAND n  > file2
ENDSSH1
done <<____HERE
    PASS    PORT    USER    IP
      .      .       .       .
      .      .       .       .
      .      .       .       .
    PASS    PORT    USER    IP    
____HERE

Upvotes: 0

user182030
user182030

Reputation: 1

I was able to, here's exactly what I did:

ssh root@your_host "netstat -an;hostname;uname -a"

This performs the commands in order and cat'd them onto my screen perfectly.

Make sure you start and finish with the quotation marks, else it'll run the first command remotely then run the remainder of the commands against your local machine.

I have an rsa key pair to my server, so if you want to avoid credential check then obviously you have to make that pair.

Upvotes: 0

prakash c kesavan
prakash c kesavan

Reputation: 1

How to run multiple command on remote server using single ssh conection.

[root@nismaster ~]# ssh 192.168.122.169 "uname -a;hostname" [email protected]'s password: Linux nisclient2 2.6.18-164.el5 #1 SMP Tue Aug 18 15:51:54 EDT 2009 i686 i686 i386 GNU/Linux nisclient2

    OR

[root@nismaster ~]# ssh 192.168.122.169 "uname -a && hostname" [email protected]'s password: Linux nisclient2 2.6.18-164.el5 #1 SMP Tue Aug 18 15:51:54 EDT 2009 i686 i686 i386 GNU/Linux nisclient2

Upvotes: -2

William Pursell
William Pursell

Reputation: 212218

It's possible, but probably more trouble than it's worth. If you can generate a unique string that is guaranteed not to be in the output of command1, you can do:

$ ssh remote 'cmd1; echo unique string; cmd2' |
  awk '/^unique string$/ { output="cmd2"; next } { print > output }' output=cmd1

This simply starts printing to the file cmd1, and then changes output to the file cmd2 when it sees the unique string. You'll probably want to handle stderr as well. That's left as an exercise for the reader.

Upvotes: 3

Rahul
Rahul

Reputation: 77866

NO, you will have to do it separately in separate command (multiple login) as already mentioned by @lanzz. To save the output in local, do like

ssh -i my_key user@ip "command1" > .\file_on_local_host.txt

In case, you want to run multiple command in a single login, then jot all your command in a script and then run that script through SSH, instead running multiple command.

Upvotes: 4

lanzz
lanzz

Reputation: 43168

Unless you can parse the actual outputs of the two commands and distinguish which is which, you can't. You will need two separate ssh sessions:

ssh -i my_key user@ip command1 > command1.txt
ssh -i my_key user@ip command2 > command2.txt

You could also redirect the outputs to files on the remote machine and then copy them to your local machine:

ssh -i my_key user@ip 'command1 > command1.txt; command2 > command2.txt'
scp -i my_key user@ip:'command*.txt' .

Upvotes: 5

Jakub Oboza
Jakub Oboza

Reputation: 5421

join them using && so you can have it like this

ssh -i my_key user@ip "command1 > command1.txt && command2 > command2.txt && command3 > command3.txt"

Hope this helps

Upvotes: 0

Related Questions