0

i have a piece of code which asks the user for a command and then makes a ssh connection to the server and outputs the result. the problem is if the user inputs the command cd /etc he navigates to that directory but if he then enters the command pwd the output is /root which means it closed the first connection and it started a new connection.

<?php
$ip= $_SESSION['ip'];
$user=$_SESSION['user'];
$pass=$_SESSION['pass'];
$connection = ssh2_connect($ip);
ssh2_auth_password($connection,$user,$pass);

if(isset($_POST['but_submit'])){
$input=$_POST['input'];
$stream = ssh2_exec($connection,$input);
$err_stream = ssh2_fetch_stream($stream, SSH2_STREAM_STDERR);
stream_set_blocking($stream,true);
stream_set_blocking($err_stream, true);
$output= stream_get_contents($stream);
$result_err = stream_get_contents($err_stream);
echo "<fieldset><pre>$output$result_err</pre></fieldset>";
}else{echo 'Vendos nje komande';}
?>
plr108
  • 1,201
  • 11
  • 16
albert
  • 5
  • 4
  • You are right. What is your question though? My guess is that it is something like "how can I keep the connection open?" - if I am right: Well, what have you attempted so far to do that? – ArSeN Feb 27 '20 at 18:52
  • sory the question is how do i keep the connection open – albert Feb 27 '20 at 18:53

0 Answers0