php / pecl-networking-ssh2 Goto Github PK
View Code? Open in Web Editor NEWBindings for the libssh2 library
Home Page: http://pecl.php.net/package/ssh2
License: Other
Bindings for the libssh2 library
Home Page: http://pecl.php.net/package/ssh2
License: Other
I notice 1.4 was created a few days ago in a90d5e9
But
Can you please clarify ?
I'm using https://pecl.php.net/package/ssh2 module in PHP. The password parameter is not working as intended.
This Works:
but if I put that code into a function and pass the password into the function ssh2_connect() authentication will fail. Password will only work if I manually supply it in the code. My use case is pulling the password from a database and passing it into a custom function parameter and then pass it to ssh2_connect().
I had the issue on windows PHP 7.4 using the 1.2 SSH2 module
I then upgraded to version PHP 8.1 and now using 1.3 SSH2 module on windows.
I get the same behavior on both versions.
#...
dump(php_uname('a'));
dump(phpversion('ssh2'));
dump(phpversion('xdebug'));
dump(php_uname('a'));
dump(phpversion());
dump(phpversion('ssh2'));
dump(phpversion('xdebug'));
$ssh_conn = ssh2_connect($my_hostname, 22222, ['hostkey' => 'ssh-rsa']);
dump($ssh_conn);
$auth_result = ssh2_auth_pubkey_file($ssh_conn, $my_username,
'~/.ssh/id_rsa.pub',
'~/.ssh/id_rsa');
dump($auth_result);
"Darwin myhostname.local 21.2.0 Darwin Kernel Version 21.2.0: Sun Nov 28 20:28:54 PST 2021; root:xnu-8019.61.5~1/RELEASE_X86_64 x86_64"
"8.1.6"
"1.3.1"
false
SSH2 Session resource @556
true
"Darwin myhostname.local 21.2.0 Darwin Kernel Version 21.2.0: Sun Nov 28 20:28:54 PST 2021; root:xnu-8019.61.5~1/RELEASE_X86_64 x86_64"
"8.1.6"
"1.3.1"
"3.1.4"
SSH2 Session resource @556
[1] 17604 segmentation fault myscript.php
------
Under PHP 8.0.13 all ok
Under PHP 8.1.0 I recived:
Warning: PHP Startup: ssh2: Unable to initialize module
Module compiled with module API=20200930
PHP compiled with module API=20210902
According to documentation, ssh2_connect currently supports only ssh-rsa and ssh-dss hostkey methods, but both of them are deprecated:
So, its unable to connect to server, which uses modern OpenSSH versions.
Library should be updated to support more modern hostkey methods.
After upgrading to Debian Bookworm
and PHP 8.2
it's no longer possible to connect to remote server via PHP and SSH2
The public/private key works because it's possible to connect via command line
ssh remote-server.com
code
$user = 'root';
$host = 'remote-server.com';
$private_key = '/root/.ssh/id_rsa';
$public_key = '/root/.ssh/id_rsa.pub';
if(!is_readable($private_key) || !is_readable($public_key)){
echo "RSA keys not found\n";
}
echo 'private key: '.file_get_contents($private_key)."\n";
echo 'public key: '.file_get_contents($public_key)."\n";
echo "host: $host\n";
echo "user: $user\n";
if(!$session = ssh2_connect($host)){
echo "Could not connect to '$host'\n";
}
if(!ssh2_auth_pubkey_file($session, $user, $public_key, $private_key)){
echo "Could not authenticate to '$host'\n";
}
output
# php test_ssh.php
private key: -----BEGIN OPENSSH PRIVATE KEY-----
b3BlbnNzaC1rZXktdjEAAAAABG5vbmUAAAAEbm9uZQAAAAAAAAABAAACFwAAAAdzc2gtcn
...
hoyaUZrhPmUPAAAAHHJvb3RAQVBJc2Nhbi1JbnRlbC1YZW9uLTY0Z2IBAgMEBQY=
-----END OPENSSH PRIVATE KEY-----
public key: ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAACAQC0geHl2dKCkd9UkU/2WtVBqo4NEhnprMzAV6hVhm7TAvK63GQN1xlAGlgagb7S5u1vET3BOAu+c8qPOkrvofhCKNF5a7v3uEh/u/oysd0Ckj6IA8spRdCXe3JbmXCHp85VadYjylwILC0drKetdMhXnHNnFHdvJDRIbVCqrO1j17kAJ9kf3cW/lsmqng9rPPGB8gozxcW6N5f3hNiSwDOo9ex0su37xflwxY2Hi4QYk1tdp7GO9YlaXlNS+s4Q47aU3dGsn5nMWlxUdT3yD2DJDdp7Bz24wLY3QFpKMbRqHC66YWIbe29Rr53ESUGukrNedZobanuW0f5wQ2xvFTB9BOXt0Bhf8OXVyNr1K79UEqaCSnpU3rGRA71lOxeRdZUN9EyHaji0rlq8kOCGBKgMwQ96fo2T1x8xikcWEiPVC+Bhd4pyOwq2G9Ihi3lAqV45zqm/5gx3Te7OtAAJIjb3aI7um+di6SL0DabHkkQH4ZzeeeikhV+ZmVUt8/guldpkZrPy7COqG043X27kk/meP/8mAnAS9mv7ircBvlW0IUHL7wwndJc0aGDwmrKaXk64qCS+80k+42BPuZAMk8dSrFJsu8vcYwuTVxn281T55t8N/JgGEkmNxEsP4VFM0DxQfgsktiPiBEPxe3S8P/ipPhx2Mn1JrohbIDahPDTyfQ== root@APIscan-Intel-Xeon-64gb
host: remote-server.com
user: root
PHP Warning: ssh2_auth_pubkey_file(): Authentication failed for root using public key: Username/PublicKey combination invalid in /root/test_ssh.php on line 21
Warning: ssh2_auth_pubkey_file(): Authentication failed for root using public key: Username/PublicKey combination invalid in /root/test_ssh.php on line 21
Could not authenticate to 'worker.dynaccount.com'
PHP
# php -v
PHP 8.2.15 (cli) (built: Jan 20 2024 14:14:18) (NTS)
Copyright (c) The PHP Group
Zend Engine v4.2.15, Copyright (c) Zend Technologies
with Zend OPcache v8.2.15, Copyright (c), by Zend Technologies
SSH client
# ssh -V
OpenSSH_9.2p1 Debian-2+deb12u2, OpenSSL 3.0.11 19 Sep 2023
SSH remote server
# ssh -V
OpenSSH_9.2p1 Debian-2+deb12u2, OpenSSL 3.0.11 19 Sep 2023
Generating public/private keys (the keys are created without passphrase)
ssh-keygen -t rsa -b 4096
The following code:
<?php
$ssh2 = ssh2_connect('example.com');
ssh2_auth_pubkey_file($ssh2, 'ubuntu', 'pubkey', 'privkey');
$sftp = ssh2_sftp($ssh2);
ssh2_sftp_unlink($sftp, 'myfile');
ssh2_scp_send($ssh2, 'myfile', 'myfile');
$myfile = ssh2_sftp_stat($sftp, 'myfile');
if ($myfile['size'] != filesize('myfile')) echo 'File size mismatch, local: ' . filesize('myfile') . ', remote: ' . $myfile['size'] . "\n";
Resulted in this output:
File size mismatch, local: 3938770, remote: 2310144
But I expected this output instead:
I should get no output as the file sizes on the local and remote servers should match. ssh2_scp_send() is not sending the whole file to the remote server.
PHP 8.1.2
No response
I'm using ssh2 extension. Unfortunately sometimes ssh2_sftp function waits forever because there is no way to specify timeout.
Please consider adding parameter timeout to ssh2_connect function (as in ftp_connect) or implement method ssh2_settimeout. I think it can be easily implemented because libssh supports libssh2_session_set_timeout().
% pecl install ssh2
pecl/ssh2 requires PHP (version >= 4.0.0, version <= 6.0.0), installed version is 7.4.20
No valid packages found
install failed
What about adding a new function to retrieve the exit code if a command executed with ssh2_exec()
?
I'm thinking about a new ssh2_get_exit_status()
function that could be used like this:
$connection = ssh2_connect($host);
ssh2_auth_password($connection, $username, $password);
$channel = ssh2_exec($connection, $command);
$exitCode = ssh2_get_exit_status($channel);
fclose($channel);
And internally, ssh2_exec()
could use libssh2_channel_get_exit_status()
.
on pecl site, I cant find ssh2 source for php8.2
when this will be available?
ssh2_exec
works fine with short commands but fails with PHP Warning: ssh2_exec(): Unable to request command execution on remote host
when trying to send long commands (41269 chars in my example). The magical limit seems to be 32KB.
Tested with v1.2 on Debian 11 with PHP 7.4.
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.