I'm looking for a solution to SSH through an existing SSH connection and execute a command with PHP. I've had this working with a simple command. Example:
$result = shell_exec("ssh user#server1 ssh user#server2 ls");
This works when run through CLI but not CGI. I've worked a bit with PHPSecLib but cannot figure out how to open the second connection, once I have a connection to server1.
Anyone have any advice?
When running through CGI, your PHP script probably runs as the web server user, and that user doesn't have access to your SSH keys. Either give the web server user your SSH keys or arrange for the PHP script to run as your user e.g. using suPHP.
And make sure to secure your application so your keys don't get stolen :)
What about $ssh->exec('ssh user#server2 ls')?
Related
I have developed a web app which shows information in real time from certain actions carried out by different users. For this I use websockets, built in PHP, in a local environment (WAMP) and works fine, but I need this to also work on an external server (web hosting service), which I only have access to through the CPanel and FTP.
Locally I make the websocket work executing next code line through Windows' CMD:
C:\wamp64\bin\php\php7.2.10\php.exe -q C:\wamp64\www\myapp\websocket_daemon.php
My question is, how can I achieve the same result in CPanel, or maybe there is another way?
It is not likely for a shared hosting environment (i.e. Apache with VirtualHost config, PHP, MySQL, and a CPanel interface) to support your websocket application.
For websocket to work, you need to either:
have a port dedicated to websocket in-bound connections; or
have a HTTP/HTTPS server that knows when to upgrade a connection and proxy pass to your websocket application.
To run your own websocket service, you should think about using Virtual Private Server services such as Amazon EC2, DigitalOcean VPS.
For that purpose you will need to have CLI (Command-Line Interface) access to the (Linux) server involved. Assuming that you have such access, running the WS service would look something like
./websocket_daemon.php
The small script assumes that you are in the appropriate folder. However, you need to resolve a few things before you get there:
Step 1: SSH support on your machine
You will need to ensure that your OS supports SSH. Your OS appears to be Windows, so you will need to either install Putty of Git Bash. Read about these technologies
Step 2 Generate an SSH key
In your CPanel, you will need to generate SSH keys:
Click on Manage SSH Keys
Click on Generate a New Key
Use the settings you prefer in order to generate a key, don't worry, you can remove the SSH keys at any time and recreate them if you realize that you prefer a different way to generate them
Read more here: https://docs.cpanel.net/cpanel/security/ssh-access/
SSH keys are composite keys, that is, it consists of a private and a public key. You can share your public key with anyone, but never ever send your private key to anyone. It should be on your computer and possibly saved to backups. Read more about SSH keys here: https://sectigo.com/resource-library/what-is-an-ssh-key
Step 3: Ensure that your computer uses the SSH keys you have generated for CPanel
You will need to tell your OS where the SSH key-pair is located, luckily this is not a new problem, see an exhausting discussion about this topic here: https://serverfault.com/questions/194567/how-do-i-tell-git-for-windows-where-to-find-my-private-rsa-key
Step 4: Test your SSH
Run the following command into your CLI that supports SSH:
ssh <username>#path
If there is no error, then you have successfully tested SSH and you are almost ready to proceed further
Step 5: Upload your Websocket script
You can do this via FTP, as you already know, but you can also do it via SCP. scp would not only use your newly created SSH connection and having fun with it, but it's also secure. Syntax:
scp file.txt remote_username#10.10.0.2:/remote/directory
Step 6: SSH to the server
See Step #4.
Step 7: Navigate to your file's location
Step 8: Ensure that you have the rights to run it
See more here: https://alligator.io/workflow/command-line-basics-file-permissions/
Step 9:: Execute the file
Run
./websocket_daemon.php
If this succeeded, then the job is basically done, you will need some script to run it upon startup and to manage it, but this is not strictly related to the question.
https://oracle-base.com/articles/linux/linux-scripts-running-in-the-background
https://smallbusiness.chron.com/run-command-startup-linux-27796.html
However, if the issue is not resolved yet, read further
Step 10: Ensuring WS support on server
You will need to set up your own WS support. Since you have managed to do so locally on your Windows, hopefully your know-how will work on the remote Linux as well. If not, read more here:
PHP Websocket server in Linux hosting
I want to make an checker to check RDP IP with user and password
I've tried to install guacamole server I install it on ubuntu and it works good and connects well but I can't make any check using php or any coding language with it because its system is so complicated
so if any one have any idea about how to check RDP IP with user and password using any coding please let me know
You can use command line RDP clients (or this one) for linux if you environment is a Linux (or MacOS), or run RDP client from command line if you using Windows.
guacamole is not good idea for automation. You need to run web-side javascript to connect, so Selenium or Chrome headless will be only possible solution as I know.
EDIT 1: How to run remote command
Set SSH passwordless connection to freerdp server
Run freerdp command on remote server via SSH connection
$process = new Process(['ssh', 'user#serverB' 'freerdp ...']);
$process->run();
In other way you can create standalone PHP service on serverB which trying to run local freerdp command with requested IP (or user?) and returning result
I am looking into a way to reboot a remote dedicated server using php. While doing research on Google I read about Python shell client. Is there is something similar in php?
Can I use php to reboot a remote server using shell commands? and what is needed to do so?
I assume that you want a web based shell implemented in php.
In that case you can look at this http://ajaxshell.sourceforge.net/
and this http://sourceforge.net/projects/ajaxshell/
Using these scripts you can run arbitrary shell commands.
But remember 2 things.
1. password protect the web shell app
2. The shell will run commands as the apache user. If you want to reboot the server, apache may need root privileges. Running apache as root is usually considered as not a good practice.
You probably can, by using exec(), passthru() etc. Is it a good idea, I'm not sure : you'll have to give reboot privileges (I don't know exactly, but it's almost root...) to your php user.
Just to be sure, what you want to do is to call a web page that will reboot your server, or it's more complicated that that?
There is a shell equivilent for PHP. I recently published a project that allows PHP to obtain and interact with a real Bash shell, you can even easily get a shell with root. Get it here: https://github.com/merlinthemagic/MTS
After downloading you would simply use the following code:
//if PHP is running on the server you want to reboot:
$shell = \MTS\Factories::getDevices()->getLocalHost()->getShell('bash', true);
//if PHP needs to SSH to the server first
$shell = \MTS\Factories::getDevices()->getRemoteHost('ip_address')->getShellBySsh('username', 'password');
$strCmd = "reboot";
$return1 = $shell->exeCmd($strCmd);
I have a CentOS 5.5 server running a local telnet daemon (which is only accessible from localhost) which prints out a list of active users on our accounting system when sent the correct commands through telnet. I need to make this information available on our intranet website which uses PHP.
I've written a Python script using the telnetlib modules to run locally on the CentOS server which simply captures the output of the telnet command and prints them to stdout. I've setup key based ssh between the web server and the CentOS server to run the python script remotely from the web server. I can execute the script successfully from the web server using ssh and it prints the list of users to stdout on the webserver. I was hoping to be able to execute this SSH command and capture the results into a variable to display on the website.
However, I can't get exec(), shell_exec(), passthru() or any other PHP exec function to display the data. I'm well aware that I may be approaching this from the totally wrong angle! What would be the best way to capture the output from ssh?
Why don't you redirect your stdout to a file. Then use your php website framework to read the file and display the results
I am trying to implement a web console using the PHPterm . By the PHPterm most of the commands can run in the console.But can't login to a server by using the SSH command.
It will display an error that host key verification failed.Is there any other web console supporting the SSH command using PHP and ajax.
As far as I can imagine, PHPterm is e mere Unix shell emulator. It just gives you the possibility to execute shell commands from the browser, providing you the look and feel of a normal shell.
If you're receiving a "host key verification failed" error, I guess the problem resides in the SSH connection rather than in PHPterm itself.
Did you try to perform the SSH connection without using PHP term, but a simple terminal?
Is your public in the right place?
Is your public key contained in the authorized key?
The verbose mode for SSH could help you in identifying the reason of the problem.