ssh_exec()
is refusing to execute a command in Windows.
Here is my code:
<?php
$connection = ssh2_connect('localhost', 22);
ssh2_auth_none($connection, 'root');
$stream = ssh2_exec($connection, 'C:\Program Files\CCleaner\CCleaner.exe',FALSE);
?>
It shows me the following warning: Unable to request a channel from remote host in
.
Firstly, it is unlikely that you are going to be able to connect to your Windows server using ssh2_connect
, because SSH is not a protocol typically used to connect to Windows, nor is it an available item in any Windows install.
Secondly, make sure that you have created the user 'root' on the Windows server, because it won't exist by default.
Alternatively If you are actually connecting to a Unix/Linux server FROM a Windows box, then the fact that you are trying to run a Windows command on it ('C:\Program Files\CCleaner\CCleaner.exe'), will fail. You can only execute commands on the remote server that are present, and any command that begins with "C:\" will obviously fail because Linux has an entirely different filesystem structure from Windows.
So from the code above it looks like you're probably trying to connect to a Linux server using the username 'root' with no password (which according to the PHP.net manual (ssh2_auth_none:
Attempt "none" authentication which usually will (and should) fail.
But as you have said you are trying to execute a command in Windows, and you are connecting to the server with host name 'localhost' - perhaps you actually are running this on Windows, and simply trying to use SSH to connect to the Windows server - which is pointless as the PHP code shown above will already be being executed on the Windows server, making the SSH connection completely redundant except to attempt to execute a command with elevated privileges (as 'root').
If you are trying to execute the command remotely using PHP as an administrator user, which is probably a bad idea, then you could consider using the function.exec.php
command in combination with the Windows runas command. Or you could just run your web server as an administrator, which is both very easy to do and very bad for security.
You could basically achieve what you are trying to do above, by installing an SSH server on Windows (What are some good SSH Servers for windows?) - after which you would have to log into the server using a user that exists, and use the password for that user (or create the user 'root' and use that).
Depending on your SSH server installation, running the command C:\Program Files\CCleaner\CCleaner.exe
could work in the way you described above.
There are more examples of how to go about this in Linux, different methods have different advantages and disadvantages.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With