I am working on a game-server hosting panel and I've been tried this:
$ssh = ssh2_connect('xx.xx.xx.xx', 22);
if(ssh2_auth_password($ssh, 'user', 'userpass'))
{
echo 'Success';
}
else
{
die('Fail');
}
But its just doesn't show any message (Success/Fail).
How can I make this connection work? ><
you probably do not have the libssh2 extension installed. it is a pretty big pain to install it so i will recommend you use this tool:
https://github.com/phpseclib/libssh2-compatibility-layer
include it at top of file and all your libssh2 function calls should work even if libssh2 is not installed.
Related
I have been thrown into the world of PHP, and I'm having a bit of an issue connecting to my SQL server. I'm using the following code to try to connect. When it hits the SQLsrv_connect command, it just seems to stop processing. No error, it just stops loading.
function testConnection()
{
$returnable = "TEST";
echo 'Current PHP version: ' . phpversion();
$connectionInfo = array("Database"=>"MyDatabase", "UID"=>"MyLogin", "PWD"=>"MyPassword");
$conn = sqlsrv_connect("MyServer",$connectionInfo);
if ($conn) {
echo "Connection Established.<br />";
} else {
echo "Something went wrong while connecting to MSSQL.<br />";
}
return $returnable;
}
Any idea on what I might be missing? I tried some very old syntax for version 5, and I got the same issue. I am trying to connect to sql server 2008.
Thanks
First, check if you have installed the sql_srv extension for PHP.
Probably this extension is not installed/loaded by you php.ini file.
For Windows
Download proper driver from SQL Driver for MSSQL extract and copy into you php installation directory. Then edit php.ini file and add in extensions path your extension. (For 99% you should copy NTS sql_srv version) also don't forget add sql_srv_pdo extension.
For Ubuntu/Linux
You can try install sql_srv and pdo_sql_srv by pecl (if is installed)
pecl install sqlsrv pdo_sqlsrv.
I need to use a connection SSH and PHP, I have version PHP 5.6.8 ,
intall OpenSSL-Win32;
In php.ini :
extension=libssh2.dll ;librerias ssl2
extension=php_ssh2.dll
The libraries are in the folder php---->ext.
With the next program my result is:
Fatal error: Call to undefined function ssh2_connect() in
C:\xampp\htdocs\conexion\conexion_ssh.php on line 2
<?php
$connection = ssh2_connect('X.X.X.X', XX);
if (ssh2_auth_password($connection, 'root', 'XXXXXXXX')) {
echo "Authentication Successful!\n";
} else {
die('Authentication Failed...');
}
?>
Do you know the problem? I think is the configuration the system variable PATH but I don't Know the process.
Thanks for you help me!
Running on PHP 5.2.13 this works fine :
each line of output is shown..
include('includes/Net/SSH2.php');
$ssh = new Net_SSH2('127.0.0.1');
if (!$ssh->login('root', 'password')) {
exit('Login Failed');
}
function packet_handler($str)
{
echo $str;
}
$ssh->exec('ping 127.0.0.1', 'packet_handler');
Running the same code on PHP 5.3.17 returns nothing.
I'd have to use something like
echo $ssh->exec('timeout 5 ping 127.0.0.1');
And wait for it to time out.
Any idea why this isn't working on the new version ?
Thanks
Try use new NET_SSH2 version. I used composer and your code work well for me (I have PHP 5.6 on my local PC). My steps was:
1) I created new empty folder
2) Run composer:
composer require "phpseclib/phpseclib"
this will create vendor folder with NET_SSH2 version = 0.3.9
3) Then I changed your source code (for composer autoload):
require 'vendor/autoload.php';
$ssh = new Net_SSH2('127.0.0.1');
if (!$ssh->login('root', 'password')) {
exit('Login Failed');
}
function packet_handler($str) {
echo $str;
}
$ssh->exec('ping 127.0.0.1', 'packet_handler');
And all work well. I think you have the problem because you are using old library version. You can try use composer or just download new library version from GitHub.
I hope it will help.
Trying to upload a file to a different server using PHP. It connects with ssh_connect() successfully, but when I include ssh2_scp_send the page that is displayed is blank (as though there is a syntax error), and it certainly did not upload the file. Any idea why this may be? I'd prefer to use PHP, but if it doesn't work I suppose I could make a system call to CLI scp with private key files, but I'd prefer not to do that. Alternative suggestions are welcomed.
Info: debian w/apache
Installed: libssh2-1-dev libssh2-php php-pear php5-dev; pecl install -f ssh2
$ssh = ssh2_connect('localhost', 22);
ssh2_auth_password($ssh, 'user', 'password');
ssh2_scp_send($ssh, 'original', 'destination'); # does not work
ssh2_exec($ssh, 'exit'); #does not help
Any ideas?
Personally, I'd recommend using phpseclib, a pure PHP SFTP implementation:
<?php
include('Net/SFTP.php');
$sftp = new Net_SFTP('www.domain.tld');
if (!$sftp->login('username', 'password')) {
exit('Login Failed');
}
echo $sftp->pwd() . "\r\n";
$sftp->put('filename.ext', 'hello, world!');
print_r($sftp->nlist());
?>
Chief among libssh2's problems is the fact that it's not easy to install. The original authors have abandoned it. The source code on php.net requires modification for it to even compile and although you can install it with apt-get on Ubuntu right now what happens when it requires more than just two new lines to work? And what if the Linux box you're trying to get it working on isn't Ubuntu?
If you ever need to create a new server on which to host your website you don't want to be stuck spending hours on a bunch of esoteric dependencies.
What i need to do if next code gives me seg fault ?
$handle = opendir("ssh2.sftp://$sftp".'/usr/bin/');
$file = readdir($handle);
closedir($handle);
where $sftp is
$this->_connection = ssh2_connect($this->_server, 22);
if($this->_authType==ExportInterface::CONN_AUTH_KEY) {
ssh2_auth_pubkey_file($this->_connection,$this->_user,$this->_key,$this->_privateKey);
} else {
ssh2_auth_password($this->_connection,$this->_user,$this->_password);
}
$sftp = ssh2_sftp($this->_connection);
Connect work well. and segfault is when only i use readdir function.
I'm answering this old question due to high ranking on Google search. I was experiencing the same segmentation fault and the real solution wasn't here.
It turns out that since PHP 5.6, the behavior of the function ssh2_sftp changed and is now returning a resource that doesn't allow to be concatenated as a string to form filesystem paths.
So, once you have the return of the ssh2_sftp, you have first to pass it through intval in order to build the remote file path:
$sftp = ssh2_sftp($this->_connection);
$handle = opendir('ssh2.sftp://' . intval($sftp) . '/usr/bin/');
$file = readdir($handle);
closedir($handle);
You are welcome.
I had the same problem and I solved it upgrading the ssh2 pecl packaage to version 0.11.3
sudo pecl install ssh2 channel://pecl.php.net/ssh2-0.11.3
It worked for me after some problems.
Regards
A seg(mentation) fault is an internal error in php or the SSH/SFTP extension. You should file a bug against the extension in question.
Don't forget to include a short, reproducible example. Since this may be only reproducible with your specific combination of ssh client and server, you should reproduce the bug first in a brand-new VM and record every step you make, like this:
Install Ubuntu 11.04 amd64 in the VM
Install ssh with $ sudo apt-get install ssh
Configure ssh with ...
Install php with $ sudo apt-get install php5-cli
Copy the script [link to http://pastebin.com/ here] to the VM
Type php ssh-segfault.php and receive a segfault.
Not applicable to the OP's situation, but if you fail to call closedir explicitly on the handle created by opendir, you'll also get a segfault during PHP internal cleanup at script end.
I had a similar problem, though with some additional factors. I'll start with the solution: use absolute paths, rather than relative. And avoid the ~.
As for my findings. Take this script:
#!/usr/bin/php
<?php
$ssh = ssh2_connect('hostname.example.com');
$res = ssh2_auth_pubkey_file($ssh, 'user', '~/.ssh/various-keys/special-key_2015.pub', '~/.ssh/various-keys/special-key_2015');
unset($res); unset($ssh);
$ssh = ssh2_connect('hostname.example.com');
$res = ssh2_auth_pubkey_file($ssh, 'user', '~/.ssh/various-keys/special-key_2015.pub', '~/.ssh/various-keys/special-key_2015');
Results in an authentication error in the 2nd ssh2_auth_pubkey_file call. A bit strange to encounter, after two identical calls - but easy enough to analyze.
But having the rest of my application code around it would result in the segfault. Eventually I narrowed it down to that that including a file, that defines a function that uses the __FILE__ constant changes the error to turn into a segmentation fault.
Even a file as simple as this:
<?php
function nothing() {
error_log(__FILE__);
}
I hope this helps somebody else at some point...
One of the answers above suggests you need to cast the resource to an int intval($sftp) to avoid the segfault.
While I believe this used to be the case, this now appears to have inverted, at least on php 7.1.9 and casting to an int now causes the segfault, at least with file_put_contents:
$connection = ssh2_connect($host, $port);
$sftp = ssh2_sftp($connection);
$remoteFile = 'ssh2.sftp://' . intval($sftp) . '/var/file.text'
echo $remoteFile
file_put_contents($remoteFile, 'Content');
ssh2.sftp://2675/var/file.text
Segmentation Fault
Without a cast it works:
$connection = ssh2_connect($host, $port);
$sftp = ssh2_sftp($connection);
$remoteFile = 'ssh2.sftp://' . $sftp . '/var/file.text'
echo $remoteFile
file_put_contents($remoteFile, 'Content');
ssh2.sftp://Resource id #2675/var/file.text