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.

vinod
  • 131

3 Answers3

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"
cuonglm
  • 153,898
  • I tried and I am getting timed out connection error.. I think there is no connectivity between two servers.. Is there any other way?? – vinod Aug 21 '14 at 07: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? – cuonglm Aug 21 '14 at 07:13
  • yes... I can ssh both the servers separately from local machine – vinod Aug 21 '14 at 07: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"
pqnet
  • 2,700