javascript - How to login to linux server using PHP code? - Stack Overflow

I want to make a GUI for automation testingtool, currently that was in CLI and we need to convert it

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?

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?

Share asked Apr 10, 2015 at 8:52 anishanish 971 gold badge6 silver badges12 bronze badges 1
  • 1 any 1 of u: Please use English! – jpaugh Commented Apr 16, 2015 at 21:55
Add a ment  | 

5 Answers 5

Reset to default 2

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

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

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

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

// authenticate by login / password

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

and then execute a mand :

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

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

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

stream_set_blocking($stream, true);

$stream_out = ssh2_fetch_stream($stream, SSH2_STREAM_STDIO);

// print the result in your local puter

echo stream_get_contents($stream_out);

The libssh2 extension is a PITA. Hard to install and hard to use. My remendation. 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');
?>

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.

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 mands and scripts from PHP. shell_exec

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

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

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744813887a4595209.html

相关推荐

  • javascript - How to login to linux server using PHP code? - Stack Overflow

    I want to make a GUI for automation testingtool, currently that was in CLI and we need to convert it

    2天前
    60

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信