Thursday, October 19, 2023

install ssh2_connet to php

1. https://pecl.php.net/package/ssh2
    (select version match with sever)
2. php_ssh2.dll and php_ssh2.pdb files in the "ext" (e.g c:\xampp\php\ext) folder;
3. remove ; from this line extention:php_ssh2.dll in php.ini.

httpd-xampp.conf

Put the following in httpd-xampp.conf

LoadFile "C:/xampp81/php/libssh2.dll"


///////////// SAMPLE PHP

<?php

$connection = ssh2_connect('192.168.2.95', 22);

ssh2_auth_password($connection, 'pi', 'raspberry');


//$stream = ssh2_exec($connection, '/usr/local/bin/php -i');


//$cmd ='python -V';

//$cmd = 'ls /dev/tty*';


//stty -F /dev/ttyUSB0 9600 cs8 -cstopb -parenb

//chmod o+rw /dev/ttyUSB0

//ex1----send

//echo -ne '\033[2J' > /dev/ttyUSB0

//----receive

//cat -v < /dev/ttyUSB0


//ex2----send

//echo -e "\x7E\x03\xD0\xAF und normaler Text" > /dev/ttyUSB0

//AA104000600A3V+0001H+0001%2H0350V0400$A,080,170,0$=xxxQ1Z

//----receive

//cat /dev/ttyUSB0


//$cmds = array ('ls p*', 'ls /dev/tty*');

//$cmds = array ('stty -F /dev/ttyUSB0 9600 cs8 -cstopb -parenb', 'sudo chmod o+rw /dev/ttyUSB0','echo -e "\x32\x54\x47\x31\x32\x2d\x47\x35\x38\x32\x30\x5F\x33\x5F\x4C\x34\xD0\xAF" > /dev/ttyUSB0');

$cmds = array ('sudo stty -F /dev/ttyUSB0','ls -l /dev/ttyUSB0');

echo "READ<BR>";

$i=0;

foreach ($cmds as $cmd) {

$i+=1;

    $stream = ssh2_exec($connection, $cmd);

    stream_set_blocking($stream, true);

    $stream_out = ssh2_fetch_stream($stream, SSH2_STREAM_STDIO);

    echo stream_get_contents($stream_out);

    echo "<BR>".$i."<BR>";

}

//$cmds = array ('sudo stty -F /dev/ttyUSB0 9600 cs8 -cstopb -parenb', 'sudo chmod o+rw /dev/ttyUSB0','sudo echo -e "2TG12-G5820_3_L4\xD0\xAF\r" > /dev/ttyUSB0');

$cmds = array ('sudo stty -F /dev/ttyUSB0 19200 cs8 -cstopb -parenb', 'sudo chmod o+rw /dev/ttyUSB0','sudo echo -en "2TG12-G5820_3_L4\x0D\x0A\r" > /dev/ttyUSB0','sudo stty -A -F /dev/ttyUSB0 -echo');

echo "CMD<BR>";

$i=0;

foreach ($cmds as $cmd) {

$i+=1;

    $stream = ssh2_exec($connection, $cmd);

    stream_set_blocking($stream, true);

    $stream_out = ssh2_fetch_stream($stream, SSH2_STREAM_STDIO);

    echo stream_get_contents($stream_out);

    echo "<BR>".$i."<BR>";

}

echo "2read<BR>";

$cmds = array ('sudo stty -F /dev/ttyUSB0','ls -l /dev/ttyUSB0');

$i=0;

foreach ($cmds as $cmd) {

$i+=1;

    $stream = ssh2_exec($connection, $cmd);

    stream_set_blocking($stream, true);

    $stream_out = ssh2_fetch_stream($stream, SSH2_STREAM_STDIO);

    echo stream_get_contents($stream_out);

    echo "<BR>".$i."<BR>";

}

?>/