From: benjamin dot lugger at gmail dot com
Operating system: Linux 5.10.17-v7l+ #1403 SMP Mon
PHP version: 7.3.27
Package: *Regular Expressions
Bug Type: Bug
Bug description:None
Description:
------------
The script is used to access a router using SSH. This works fine.
It is also possible to issue commands on the router.
But it is not possible to get the information into the variable
$match[0].
I have installed the latest pecl release on a Pi as well on other
Ubuntos running PHP7. The problem seems to be related to php7. I did not
change the php.ini. The installation was simple doing the following
commands on a PI.
sudo apt-get -y install php-pecl-http
sudo apt-get -y install php-pear
sudo pecl channel-update pecl.php.net
sudo apt-get -y install tcl tcl-expect tcl-expect-dev php-dev tcl-dev
expect-dev
sudo pecl install expect
I also added the entry for the expect.so in the php.ini. Otherwise the
script would not work at all. As mentioned, the scrip can access the
router and issue commands. This can be seen when setting "ini_set
("expect.loguser", "On");". For whatever reason the script is not able
to store the regexpt :-) data in $match[0].
Test script:
---------------
<?php
$username = 'username;
$password = 'password';
$testip = 'ipaddress';
define('PASSWORD', 'PASSWORD'); define('ENABLE', 'ENABLE');
define('YESNO', 'YESNO'); define('CONFIRM', 'CONFIRM');
define('SHELL',
'SHELL');
ini_set ("expect.loguser", "Off");
ini_set ("expect.match_max", 10000000);
ini_set ("expect.timeout", 120);
$stream = expect_popen ("ssh -c aes256-cbc
-oKexAlgorithms=+diffie-hellman-group1-sha1 ".$username."@".$testip);
$command01 = '';
while (true) {
switch (expect_expectl ($stream, array (
array ("assword:", PASSWORD), // SSH is asking for password
array (">", ENABLE, EXP_EXACT), // ENABLE
array ("yes/no)?", YESNO), // SSH is asking whether to store the host
entry
array ("confirm", CONFIRM), // SSH is asking whether to store the host
entry
array (".*#", SHELL, EXP_REGEXP), // Command Prompt
), $match)) {
# Case Password
case PASSWORD:
fputs ($stream, $password."\n");
break;
# Case Password Yes/No
case YESNO:
fputs ($stream, "yes"."\n");
break;
# Case CLI Commands
case SHELL:
if ($command01 == '') {
fputs ($stream, "term len 0"."\n");
$command01 = 'done';
sleep(1);
break;
}
else {
$show = sendCommand($stream, "show clock");
print "OUTPUT:\n$show\n";
$show = sendCommand($stream, "logout");
break 2;
}
# Case
case EXP_TIMEOUT:
# Case
case EXP_EOF:
break 2;
default:
die ("Error has occurred!");
}
}
fclose ($stream);
function sendCommand($mystream, $mycommand)
{
fputs ($mystream, $mycommand."\n");
switch (expect_expectl ($mystream, array (array (".*#", SHELL,
EXP_REGEXP)), $match)) {
case SHELL:
return $match[0];
break;
}
}
?>
Expected result:
----------------
TESTSYSTEM1:/home # php ssh2.php
OUTPUT:
show clock
07:46:12.240 UTC Sat Apr 3 2021
ROUTER#
Actual result:
--------------
TESTSYSTEM2:/home $ php ssh2.php
ROUTER:
--
Edit bug report at https://bugs.php.net/bug.php?id=80930&edit=1
--
Fix committed: https://bugs.php.net/fix.php?id=80930&r=fixed
Fixed in release: https://bugs.php.net/fix.php?id=80930&r=alreadyfixed
Need backtrace: https://bugs.php.net/fix.php?id=80930&r=needtrace
Need Reproduce Script: https://bugs.php.net/fix.php?id=80930&r=needscript
Try newer version: https://bugs.php.net/fix.php?id=80930&r=oldversion
Not developer issue: https://bugs.php.net/fix.php?id=80930&r=support
Expected behavior: https://bugs.php.net/fix.php?id=80930&r=notwrong
Not enough info: https://bugs.php.net/fix.php?id=80930&r=notenoughinfo
Submitted twice: https://bugs.php.net/fix.php?id=80930&r=submittedtwice
register_globals: https://bugs.php.net/fix.php?id=80930&r=globals
PHP version support discontinued: https://bugs.php.net/fix.php?id=80930&r=phptooold
Daylight Savings: https://bugs.php.net/fix.php?id=80930&r=dst
IIS Stability: https://bugs.php.net/fix.php?id=80930&r=isapi
Install GNU Sed: https://bugs.php.net/fix.php?id=80930&r=gnused
Floating point limitations: https://bugs.php.net/fix.php?id=80930&r=float
No Zend Extensions: https://bugs.php.net/fix.php?id=80930&r=nozend
MySQL Configuration Error: https://bugs.php.net/fix.php?id=80930&r=mysqlcfg