linux - PHP ssh2_shell execute command and get response -


i connecting ssh server , using ssh2_shell create interactive bash shell.

the problem have how send command shell , response command accurately.

this doing:

$stream = ssh2_connect(); $shell = ssh2_shell($stream, 'bash');  fwrite($shell, "whoami\n"); fwrite(stdout, fread($shell, 2048) . php_eol); 

the problem above code output stdout (for commandline view) shows last login server etc.. aka welcome message. have wait second or 2 , read shell again output of command sent.

now can solve doing sleep(1) after command send hacky. if command takes longer second, not results. if command takes less second, waste time.

i tried stream_set_blocking($shell, true) before writing command , calling fread problem still persists unless sleep after command.

basically, how can use ssh2_shell, send command , response command regardless of how long take, without timing out script requesting content of blocked stream after shell has returned contents?

what describe intended behavior of ssh2_shell - interactive. means need work timeouts.

if want command or script executed on remote host , receive output need non-interactive execution - ssh2_exec.


Comments