phpseclib: SSH2: PTY Comparisons

Examples

Some commands require a PTY to work correctly with exec(). Examples follow:

passwd


exec():
echo $ssh->exec('passwd');

Stalls. If you use a callback function or setTimeout() you'll see that it's outputting (current) UNIX password: and waiting for input that can't ever come.

exec() with PTY:
$ssh->enablePTY(); 
$ssh->exec('passwd'); 
echo $ssh->read('password:'); 
$ssh->write("badpw\n"); 
$ssh->setTimeout(3); 
echo $ssh->read('password unchanged');

Runs as one might expect. The three second delay is implemented by Linux to protect against brute force attacks (per "man pam_unix").

read() / write():
echo $ssh->read('username@username:~$');
$ssh->write("passwd\n");
echo $ssh->read('password:'); 
$ssh->write("badpw\n"); 
$ssh->setTimeout(3); 
echo $ssh->read('password unchanged');

Pretty much the same output as exec() with PTY.


top


exec():
echo $ssh->exec('top');

Outputs TERM environment variable not set.

exec() with PTY:
$ssh->enablePTY();
$ssh->exec('top');
$ssh->setTimeout(5);
$ansi->appendString($ssh->read());
echo $ansi->getScreen();

See Interactive Shell: ANSI Codes for the output.

read() / write():
$ansi->appendString($ssh->read('username@username:~$'));
$ssh->write("top\n");
$ssh->setTimeout(5);
$ansi->appendString($ssh->read());
echo $ansi->getScreen();

Pretty much the same output as exec() with PTY.