Ben Holness
Ben Holness

Reputation: 2717

How can I pipe the output of a command to another server?

I would like to stream the output of a command from one server to a remote server (both linux servers, latest Ubuntu).

In an ideal world, I would like to stream it over https to a webserver that has a php script to receive the input and process it. For the sake of simplicity, let's say the php just outputs the stream to a file on the webserver. In reality it will parse it and put various things in a queue to be dealt with.

I would like it to look something like this (I know this is not valid of course!)

tail -f logfile.log | https://myserver.com/receiveLogfile.php

receiveLogFile.php would then look something like this:

<?php
$stream=fopen( "php://input", "r" );
$out=fopen ("/tmp/receivedLog.log", "a");

// Somehow send $stream to $out?

If it's not possible to send it directly to php, then is there a way to send it to a file on the remote server? Ideally without a password (in other words it would need something running on a given port on the remote server to receive the stream and write it to a local file. I'm not sure if something like that already exists or not).

If a password is required, then it would need to be able to be sent non-interactively. For reasons I can't go into here, a keyfile will not work well for my unique situation, so I am hoping to avoid that.

Upvotes: 0

Views: 414

Answers (1)

Aleksander Wons
Aleksander Wons

Reputation: 3967

SSH to the rescue. You can easily pipe things into a remote server over SSH. Let's say on server A you have a script like this:

$stream = fopen("php://stdin", "r");
while(!feof($stream)) {
    echo fgets($stream);
}
fclose($stream);

From server B you can call the script like this (I just tested it with vagrant):

echo "my output" | ssh [email protected] -p 2222 php test.php
my output

Upvotes: 1

Related Questions