0

I'm trying to execute a command on my Raspberry Pi via SSH and get the result of it in my PHP script on my Windows machine. Currently I can execute the command on my RasPi, but I do not get any results back into the PHP script.

The code I'm Using for this:

<?php

$cmd = "C:\\path_to_putty\\putty.exe -ssh pi@RasPiIP -pw raspberry -m C:\\path_to_test.txt\\test.txt";
$result = shell_exec($cmd);
echo $result;

?>

For sending commands to my RasPi the code works. I have tested multiple times by as example changing test.txt to sudo reboot and it worked as intended. I'm using PuTTY to send my command (test.txt is currently nfc-list which returns connected Scanners etc not important right here) to the RasPi.

What I want to achieve is that $result contains the returned data when my command is executed.

Is it even possible to do that? If yes how (any help appreciated). If no, are they maybe other ways to approach this?

Addressing the possible duplicate: I am using a Windows Machine and also I'm trying to get the result (of the one command) to reuse in my PHP script. In the other question, user is trying to save the full console log and save it to another file.

Martin Prikryl
  • 167,268
  • 50
  • 405
  • 846
Freddy789
  • 378
  • 2
  • 15
  • Probably want `c:/path/to/putty.exe` and maybe `c:/some/path/to/test.txt` – AbraCadaver Jun 07 '19 at 13:30
  • Thats how im doing it in my programm i just cut it so its easier to understand my code – Freddy789 Jun 07 '19 at 13:34
  • 1
    Possible duplicate of [Can we save the execution log when we run a command using PuTTY/Plink](https://stackoverflow.com/questions/27371900/can-we-save-the-execution-log-when-we-run-a-command-using-putty-plink) – Martin Prikryl Jun 07 '19 at 13:37
  • Well don't do that. You remove something that makes it the most obvious answer. – AbraCadaver Jun 07 '19 at 13:48
  • Added them thanks for letting me know – Freddy789 Jun 07 '19 at 13:55
  • Not that you would change course now, but you could use `Invoke-Command` from PowerShell. https://www.hanselman.com/blog/InstallingPowerShellCoreOnARaspberryPiPoweredByNETCore.aspx – lit Jun 07 '19 at 14:00

1 Answers1

2

First, do not use PuTTY. PuTTY is a GUI application intended for an interactive use. Use Plink, which is command-line/console equivalent of PuTTY intended for command automation. Being a console application, it has a standard output, which can be read in PHP (PuTTY as a GUI application does not have standard output).

With Plink, you can also specify the command on Plink command line, so you do not need to create the test.txt command file.

In any case, there's no way to make PuTTY or Plink separate an output of command only (at least not from a command-line).

But what you can do, is to print some header/trailer to distinguish the start and end of the command output, like:

plink.exe -ssh pi@RasPiIP -pw raspberry "echo start-of-command && command && echo end-of-command"

And then in PHP, you can look for the start-of-command and end-of-command to identify what part of Plink output is really the command output.


In any case, you better use a PHP SSH library to achieve what you want, rather then driving an external application. For example phpseclib. But that's a completely different question.

Martin Prikryl
  • 167,268
  • 50
  • 405
  • 846
  • First of all sorry for my late response. After trying as you said with Plink i have 2 questions 1. You said i wont need to use the test.txt file anymore but when using it with the actual command instead of the file it says plink: unable to open command file 2. How do i save the output when running the command in my PHP script i get an empty result. – Freddy789 Jun 11 '19 at 06:37
  • The `-m` should have been removed. I've corrected my answer. – Martin Prikryl Jun 11 '19 at 06:40
  • Thanks when running the command in an Console it works fine now but i still dont get the Output printed in the PHP script. – Freddy789 Jun 11 '19 at 06:54
  • What output? Do you mean that you get no output at all? Or that you do not get the output of your command? --- If you just execute `$cmd = "C:\path_to_putty\plink.exe` - Do you get the usage screen? – Martin Prikryl Jun 11 '19 at 07:02
  • After trying a couple times it seems that its not working as it should when trying as you said with just $cmd = "C:\path_to_putty\plink.exe i get : Plink: command-line connection utility Release 0.71 Usage: plink [options] [user@]host [command] ("host" can also be a PuTTY saved session name) Options: -V .... when however trying to as example reboot with the plink command nothing happens and i get an blank page – Freddy789 Jun 11 '19 at 07:13
  • It is working now i dont know what i did wrong earlier thank you for your help – Freddy789 Jun 11 '19 at 07:18