Ethical
Ethical

Reputation: 1

Execute bash commands that take input from php

I have a php script that I use to execute bash commands.

That bash command does not take argument at start, i.e.

ssh root@ip

However, when this command is executed, later it requires input from user.

So how can I accomplish this with php?

Upvotes: 0

Views: 82

Answers (1)

Andreas Louv
Andreas Louv

Reputation: 47099

You can use popen which returns a file handle:

$handle = popen("ssh root@ip", "w");
fwrite($handle, "This is send as stdin\n");
// ...
pclose($handle);

Example:

<?php
  $handle = popen("cat - > a.txt", "w");
  fwrite($handle, "hello world\n");
  // ...
  pclose($handle);
?>

Will create a.txt with contains hello world.

Upvotes: 2

Related Questions