2

I know that it is quite easy to figure out a terminal's size parameters via the stty -a command. When using local CLI PHP scripts, there is no problem at all on grabbing that output via system() or so.

But I am trying the same thing via a php script started from an ssh command. Sadly, all that stty ever returns is:

stty: standard input: Invalid argument.

The calling code is:

exec('stty -a | head -n 1', $query);
echo $query[0];

So the question is: If I can output to the terminal and read input from it (e.g. can fread() from STDIN and fwrite() to STDOUT in PHP, shouldn't stty also have valid STDIN and STDOUT?

eFrane
  • 89
  • 1
  • 6
  • I should have mentioned that I am trying to avoid using something like ncurses just to get the screen size. – eFrane Apr 15 '12 at 16:24
  • Not 100% sure, but doing an exec for the terminal size may result in the incorrect size since it'll be the size allowable for the script, not of the terminal executing it. – ChrisK Apr 15 '12 at 16:28
  • Which would result in requesting the size of a terminal which isn't allocated since SSH itself doesn't allocate terminals for directly called commands. Hm. – eFrane Apr 15 '12 at 16:32
  • You could try exec('tput cols') and exec('tput lines'), which may give some information, again with the above disclaimer. – ChrisK Apr 15 '12 at 16:33
  • Alternatively it would be ncurses by the seems. – ChrisK Apr 15 '12 at 16:40
  • I guess it's ncurses, yep. The other approaches need $TERM to be defined which can not possibly be the case. Thanks though. – eFrane Apr 15 '12 at 16:44

1 Answers1

2

Use ssh -t:

% php ~/src/termtest.php
speed 9600 baud; 39 rows; 127 columns;
% ssh localhost php ~/src/termtest.php
stty: stdin isn't a terminal
% ssh -t localhost php ~/src/termtest.php
speed 9600 baud; 39 rows; 127 columns;Connection to localhost closed.

SSH does not pass in a fully functional terminal by default. Shells and ncurses seem to be able to get them somehow, but to launch something that needs one directly from SSH you need to set -t.

For the same reason you can e.g. launch tmux (or screen) by ssh'ing to a server and then typing tmux at the prompt or through ssh -t _server_ tmux but not through sh _server_ tmux.

matthiasr
  • 36
  • 1