1

so here is my problem: I have a pc that i use mainly through ssh and i recently created a telegram bot to quickly perform those commands used more frequently.

It works fine except for the shutdown command.

$connection = ssh2_connect('www.example.com', 22);
ssh2_auth_password($connection, 'user', 'mypass');
$stream = ssh2_exec($connection, 'netstat | grep ESTABLISHED || shutdown -P now');

I use this code since i need to verify nobody else (my brother) is connected to the pc before shutting it down, but it looks like that regardless the output of netstat | grep ESTABLISHED the code || shutdown -P now is always gettin executed.

I always used such a code through ssh to verify the connection and it has always work'd fine. how can i have || shutdown -P now part executed only if the output of netstat | grep ESTABLISHEDis empty?

1 Answers1

0

Apparently, ssh2_exec can't execute several commands joined by operators. In your case it looks like it just parses the command and executes the last one. Or it just executes all the command one by one. What you can do, is to split your commands.

How to get the result of the ssh2_exec command:

[get result from ssh2_exec

So when you get the result you can process it and run another ssh2_command for shutting down.

SergeyAn
  • 754
  • 6
  • 9
  • I figured out now to solve this. I created in the remote machine a bash file with an if structure and i call It from ssh. This worked fine for me – pietro montagna Jan 31 '20 at 09:17