Funny characters returned from read function of Net_SSH2 library (phpseclib)

StackOverflow https://stackoverflow.com/questions/21338055

  •  02-10-2022
  •  | 
  •  

سؤال

When I use the Net_SSH2 library and the read/write functions like this:

$ssh = new Net_SSH2($strServerIPAddress);
if(!$ssh->login($strServerUsername, $strServerPassword))
die("error");
$ssh->write(" service httpd reload\n");
$strApacheRestartResult = $ssh->read("[root@$strServerName ~]#");
$ssh->disconnect();

I get funny characters in the read function results ($strApacheRestartResult) that I dont see when running the same thing via Putty, see below:

service httpd reload
Reloading httpd: [60G[[0;31mFAILED[0;39m]

[root@server1 ~]#

Why are these [60G[[0;31m and [0;39m] in the return data from the read function?

هل كانت مفيدة؟

المحلول

They're ANSI control codes. Their purpose is to control the formatting of the text and the color and what not. To decode them properly you'd need a terminal emulator. phpseclib has one called File_ANSI:

http://phpseclib.sourceforge.net/ssh/examples.html#top

Here's your code rewritten to use it:

$ssh = new Net_SSH2($strServerIPAddress);
$ansi = new File_ANSI();
if(!$ssh->login($strServerUsername, $strServerPassword))
die("error");
$ssh->write(" service httpd reload\n");
$ansi->appendString($ssh->read("[root@$strServerName ~]#"));
echo $ansi->getScreen();
$ssh->disconnect();
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top