I am using Laravel framework with homestead. (Latest Version) and i am trying to use ssh2_connect. I have installed the extension
sudo apt install php-ssh2
and i have tested that its working
php -m |grep ssh2
i am seeing
ssh2
i am assuming that this is correct but when i try and make a connection i get the following error.
Call to undefined function App\Http\Controllers\ssh2_connect()
This is the code i am executing
$connection = ssh2_connect($request->sever_ip, 2222);
if (!$connection) die('Connection failed');
Would someone point me in the right direction?
Because you are in the namespace of the Controllers you might need to use:
$connection = \ssh2_connect($request->sever_ip, 2222);
if (!$connection) die('Connection failed');
Related
I am using WampServer Version 2.2, php5.4.3 , Apeache2.2.22 I could not use mssql_connect(), "mssql_connect() Fatal error: Call to undefined function mssql_connect()" I went through googling but still not found solution.
It is always better to use PDO which supports ms sqlserver and other databases, you can use the syntax
$dbh = new PDO('mysql:host=localhost;dbname=test', $user, $pass);
or you can refer the documentation https://secure.php.net/manual/en/pdo.connections.php
You don't have the MS SQL Drivers installed. You can check this with phpinfo();
On Linux you need mssql.so or sybase.so With debian its apt-get install php5-sybase
For windows take a look here
Drivers need to be configured for PHP to find the function
mssql_...
You could also look at PDO DB classes as they can connect to any DBS, you need the drivers installed tho.
i have several server in debian 6.0.8 with php version at 5.3.28-1~dotdeb.0.
I have made an apt-get install libssh2-php on each.
When u made an php -v, i have openssl and ssh2 in the list on each.
I have already restarted apache after that.
But when i run a php script with ssh2_connect() call, it doesnt' work on 2 servers but it works on the other server.
I have the error : "call to undefined function ssh2_connect()."
I don't understand, debian version and php version are the same.
Can you help me ?
Thanks.
In packaged distros like Debian and EL you have the "non-core" parts of PHP packaged separately. In Debian flavors you have to have the php5-ssh2 package installed.
sudo apt-get install php5-ssh2
I'd just use phpseclib, a pure PHP SSH implementation. eg.
<?php
include('Net/SSH2.php');
$ssh = new Net_SSH2('www.domain.tld');
if (!$ssh->login('username', 'password')) {
exit('Login Failed');
}
echo $ssh->exec('pwd');
echo $ssh->exec('ls -la');
?>
phpseclib has a number of a advantages over libssh2. You can also use phpseclib to emulate the libssh2 functions, if you're so inclined. eg.
https://github.com/phpseclib/libssh2-compatibility-layer
I'm trying to connect to mysql server using the following code:
$connection = mysql_connect("localhost", "username", "password");
if(!$connection)
{
die('Could not connect: '. mysql_error());
}
And I'm getting "Call to undefined function mysql_connect()". Why is that? isn't this function built in in php?
I'm using Ubuntu 12.04 + php 5.4.11 + php's built in http server.
First do not use mysql_* functions as those functions are deprecated
and second you need to install php5-mysql
sudo apt-get install php5-mysql
and then you can use mysqli and also PDO (needs to be installed too).
You must enable the extension.
Be aware that this API is deprecated.
What is the best way to connect via PHP on a Linux box to a Remote Microsoft SQL Server.
The PHP will only ever run on a Linux box.
I've been trawling for the simplest answer for a while now.
Php 5.6
Ubuntu
sudo apt-get install php5.6-sybase freetds-common libsybdb5
AWS / Centos / Redhat
sudo yum install php56-mssql
After that, you can connect to the MsSql database through PHP with something like this:
<?php
$server = 'localhost';
$user = 'someUser';
$pass = 'somePassword';
$database = 'theDatabaseName';
try {
$pdo = new \PDO(
sprintf(
"dblib:host=%s;dbname=%s",
$server,
$database
),
$user,
$pass
);
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
} catch (PDOException $e) {
echo "There was a problem connecting. " . $e->getMessage();
}
$query = "SELECT * FROM TestSchema.Employees";
$statement = $pdo->prepare($query);
$statement->execute();
$results = $statement->fetchAll(PDO::FETCH_ASSOC);
var_dump($results);
You can troubleshoot MsSQL with something like this on the command line:
tsql -H your.server.name -p 1433 -U yourusername -P yourpassword -D yourdatabasename
To install tsql you can run this:
Installation of SQL binaries for testing on any PHP version
sudo apt install freetds-bin
Php 7+
Microsoft has native drivers we can use. Full instructions are here (Redhat / Ubuntu / Others).
That link also contains required steps to install Ms SQL server on your dev machine (working on Ubuntu, doesn't work on AWS, but you can just spin up an RDS instance there).
It also contains basic instructions on how to create test tables and data in the database, and PHP connectivity code.
You can also install the components for a newer version of PHP like this:
sudo apt-get install php7.2-sybase freetds-common libsybdb5
you must to install mssql driver for php on linux.
this is a best tutorial for you.
Try: For Ubuntu
sudo apt-get install php5-sybase php5-odbc freetds-common
Edit freetds.conf then connect MSSQL server with this PHP.
After searching and trying out the many suggestions here and in other posts, and spending a lot of time, a colleague suggested trying ADO.
As we already had an ADO enabled PHP install, it literally took less than 10 minutes to get up and running.
If your serious about connecting from Linux PHP to MS-SQL, consider ADO.
It's interesting to note that every answer here proposes one solution to the problem rather than answering the question asked which was which is the "best" solution. Unfortunately the OP forgot to tell us what the criteria were for "best".
Nobody so far has mentioned odbc. While this entails both configuring the odbc driver for connecting to the database and enabling the php extension, this method provides the best isolation of the php code from the underlying database making it easier to port the code later.
The microsoft provided drivers for php should give good compatibility but only support v7 up of php.
You may prefer to stick with the functionality provided by your Linux distro to ensure you have a supportable product and/or automated patch management. An alternative to odbc here may be the freetds driver but you didn't tell us anything about which Linux distro this is.
Get the sqlsrv extension from microsoft for php http://msdn.microsoft.com/en-us/sqlserver/ff657782.aspx
Following is my script:
<?php
$connection = ssh2_connect('XX.XX.XX.XX', 22);
ssh2_auth_password($connection, 'root', '******');
$stream = ssh2_exec($connection, 'useradd -d /home/users/test -m testftp');
$stream = ssh2_exec($connection, 'passwd testftp');
$stream = ssh2_exec($connection, 'password');
$stream = ssh2_exec($connection, 'password');
?>
It showing the following error:
Fatal error: Call to undefined function ssh2_connect() in /home/chaosnz/public_html/fotosnap.net/test.php on line 2
How can I deal with this?
Thanks
I have installed the SSH2 PECL extension and its working fine thanks all for you help...
Honestly, I'd recommend using phpseclib, a pure PHP SSH2 implementation. Example:
<?php
include('Net/SSH2.php');
$ssh = new Net_SSH2('www.domain.tld');
if (!$ssh->login('username', 'password')) {
exit('Login Failed');
}
echo $ssh->exec('pwd');
echo $ssh->exec('ls -la');
?>
It's a ton more portable, easier to use and more feature packed too.
I have solved this on ubuntu 16.4 PHP 7.0.27-0+deb9u and nginx
sudo apt install php-ssh2
You need to install ssh2 lib
sudo apt-get install libssh2-php && sudo /etc/init.d/apache2 restart
that should be enough to get you on the road
If you are running a bomebrew on OSX, I used the following to install it:
brew install php56-ssh2
That worked for me. I pulled it from here. There should also be Ubuntu and OSX using mac port as well.
I am running CentOS 5.6 as my development environment and the following worked for me.
su -
pecl install ssh2
echo "extension=ssh2.so" > /etc/php.d/ssh2.ini
/etc/init.d/httpd restart
To expand on #neubert answer, if you are using Laravel 5 or similar, you can use phpseclib much simpler like this:
Run composer require phpseclib/phpseclib ~2.0
In your controller add
use phpseclib\Net\SSH2;
Then use it in a controller method like:
$host = config('ssh.host');
$username = config('ssh.username');
$password = config('ssh.password');
$command = 'php version';
$ssh = new SSH2($host);
if (!$ssh->login($username, $password)) {
$output ='Login Failed';
}
else{
$output = $ssh->exec($command);
}
For today pecl install ssh2 requires old versions of php (<=6.0). Here https://pecl.php.net/package/ssh2 you can see the latest beta versions of ssh2 supporting php7 and php8. So you should install one of them:
pecl install ssh2-1.3.1
* Do not forget to enable this ssh2.so extension in your php.ini file.
For WHM Panel
Menu > Server Configuration > Terminal:
yum install libssh2-devel -y
Menu > Software > Module Installers
PHP PECL Manage Click
ssh2 Install Now Click
Menu > Restart Services > HTTP Server (Apache)
Are you sure you wish to restart this service?
Yes
ssh2_connect() Work!
I know there are answers but I am just simplifying the answer here.
I have same issue and I fixed it with below solution in ubuntu 20:
sudo apt-get install libssh2-1 php7.1-ssh2 -y
you can change the php version as per your need like: php7.4-ssh2
Ref: https://blog.programster.org/ubuntu-16-04-install-php-ssh2-extension