0

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?

1 Answer 1

2

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.

Sign up to request clarification or add additional context in comments.

5 Comments

ssh root@ip , after executing this command it will next ask for the password for the remove server, why will i need a file handle for that ? Sorry if i don't understand your answer. @andlrc
@Ethical So that you can send an answer, to the process. popen stands for process open, and returns the process's standard input to $handle.
Thank you for your response it some how helped. But the command I am working with requires 2-3 user inputs, for that popen is not working, any other alternatives or suggestions? @andlrc
@Ethical Can't you just call fwrite three times or write a multiline string: fwrite($handle, "a\nb\nc\n")
Yes i actually called it three times and tried with \n as well, but it still don't work. It gives the input only to one of the intermediate steps. @andlrc

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.