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`any 1 of u`: Please use English! – jpaugh Apr 16 '15 at 21:55
5 Answers
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);
- 1
- 1
- 7,533
- 29
- 43
-
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 . – anish Apr 10 '15 at 12:13
-
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" – anish Apr 14 '15 at 06:08
-
I think you must first enable the "ssh2 module" in your php configuration. Follow theses instructions : http://stackoverflow.com/questions/15134421/php-install-ssh2-on-windows-machine – Nicolas Pennec Apr 14 '15 at 19:59
-
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? – anish Apr 15 '15 at 05:19
-
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 ? – Nicolas Pennec Apr 15 '15 at 15:35
-
No, that i haven't removed, in the php.ini file frm which line should i remove " ; " there are many extention present there. Please response – anish Apr 15 '15 at 16:19
-
edit your php.ini like that : before: `;extention:php_ssh2.dll` after: `extention:php_ssh2.dll` and restart apache – Nicolas Pennec Apr 15 '15 at 16:36
-
Sure I will try and let you know :) If it does't work i will disturb u again :P Tell me one more thing we need to register dll file also? from comand promt C:/---> Windows---->System32----> regsvr32 libssh2.dll like this? – anish Apr 15 '15 at 16:51
-
-
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');
?>
- 21
- 1
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.
- 1,392
- 2
- 13
- 27
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.
- 1
- 1
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
- 27
- 5