13

How to redirect the output of a unix command from one server to another server.

I should be able to send the unix command's output from server-1. Then I should be able to receive the output in Server-2 and write it into a file.

1

3 Answers 3

26

General, you can always do:

<command> | ssh user@remote-server "cat > output.txt"

It saves output of <command> to output.txt file in remote server.

In your case, on Server-1:

echo "qwerty" | ssh user@Server-2 "cat > output.txt"

If two servers have no connectivity, but you can ssh to both servers, then from local machine, you can do:

ssh user@Server-1 "<command>" | ssh user@Server-2 "cat > output.txt"
3
  • I tried and I am getting timed out connection error.. I think there is no connectivity between two servers.. Is there any other way?? Commented Aug 21, 2014 at 7:11
  • 1
    @vinod: It's the connectivity problem from two server, you can not do this if two servers have no connectivity. Make sure you can ssh from Server-1 to Server-2. Can you ssh from local machine to both servers? Commented Aug 21, 2014 at 7:13
  • yes... I can ssh both the servers separately from local machine Commented Aug 21, 2014 at 7:31
6

You can run:

ssh remote_server "command" > file_on_local_host.txt

or use the output as an input for local command:

ssh remote_server "remote_command" | local_command

1

Since you can't connect directly from server 1 to server 2 you can use this, having your local machine in the middle:

ssh server1 command | ssh server2 "cat > output.txt"

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.