2

I want to make a GUI for automation testing tool, currently that was in CLI and we need to convert it ig GUI, now the problem i am facing is that I have selected HTML,CSS,JS for front end and PHP for backend, so can any 1 of u suggest me how to login to the server, which is having some IP? or simply how to login to linux server using PHP code?

1
  • 1
    any 1 of u: Please use English! Commented Apr 16, 2015 at 21:55

5 Answers 5

2

You can open a connection via SSH with the "Secure Shell2" PHP module.

https://php.net/manual/en/book.ssh2.php

// open a SSH connection (hostname = IP or network name of the remote computer)

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

// authenticate by login / password

ssh2_auth_password($connection, 'username', 'password');

and then execute a command :

// execute a shell command on the remote computer (eg. 'php -v' to know the PHP version)

$stream = ssh2_exec($connection, 'php -v');

// read the result from the remote computer to your local computer

stream_set_blocking($stream, true);

$stream_out = ssh2_fetch_stream($stream, SSH2_STREAM_STDIO);

// print the result in your local computer

echo stream_get_contents($stream_out);

Sign up to request clarification or add additional context in comments.

10 Comments

Hi NicoPennec, Thank you for positive response , can you please description of each line. I want to know the functionality of each line. I will be very thankful if you can do this .
Its not connecting Nic, see for hostname i am providing IP address, username and password also i am giving , see now currently i am using dreamweaver for this and i have a local server XAMPP, but i need to connect to the remote server. I am running this conde then in browser what should i get? currently i am getting this error "Fatal error: Call to undefined function ssh2_connect() in C:\xampp\htdocs\automation\test.php on line 2"
I think you must first enable the "ssh2 module" in your php configuration. Follow theses instructions : stackoverflow.com/questions/15134421/…
I think i need to have some predefined libraries to run ssh with php i have installed them too, and as mentioned libssh2.dll to keep in Windows-> System32 but the problem now is that how to register the .dll file. I am trying this using this too regsvr32 libssh2.dll in comand promt but its not working, i am working on 32 bit. what do do next?
Have you copy php_ssh2.dll and php_ssh2.pdb to php/ext folder ? Then, have you remove ';' from this line extention:php_ssh2.dll in php.ini file ?
|
2

The libssh2 extension is a PITA. Hard to install and hard to use. My recommendation. Use phpseclib. Example:

<?php
include('Net/SSH2.php');

$ssh = new Net_SSH2('hostname', 22);
if (!$ssh->login('username', 'password')) {
    exit('Login Failed');
}

echo $ssh->exec('php -v');
?>

Comments

0

With PHP you can't log into a Server as you only run a php-script but you're not acctually a user that does stuff. That said there is a user set up for php-scripts. Everytime you send a request to get a php-file that user runs the script and sends beck the result.

If you want to output data from your server you have to have these data accessible via php. Than you can open those PHP-files as part of a website.

Comments

0

You don't login to the server from PHP, however if your web server is running on the same server you are trying to control, you can easily execute shell commands and scripts from PHP. shell_exec

Be careful of the the permission you are granting your commands and scripts.

Comments

0

How to execute "service bind9 restart" use phpseclib .. the code is:

<?php include('Net/SSH2.php'); 
 $ssh = new Net_SSH2('xxxx');
 if (!$ssh->login('xx', 'xxx')) {
 exit('Login Failed');
 }
 echo $ssh->exec('service bind9 restart');
?>

Output : bind9: unrecognized service

thanks

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.