Essentially, I would like to run a shell command from a Laravel app to my local client PC. The objective is to execute an exe file on my local Windows 7 machine located in the C drive. This functionality works when running Laravel in localhost, but it doesn't work after deploying it to a Plesk server.
Any suggestions?
Thanks.
Update: I understand that this can be done using ssh2 but it seems like quite risky in terms of security. So looking for alternative way.
My current code:
$exePath = 'C:\\ProgramData\\ADWinCT\\RsKey.exe';
$scriptPath = 'C:\\ProgramData\\ADWinCT\\script.exe';
$commandRsKey = "start /B \"\" \"$exePath\"";
exec($commandRsKey);
$commandScript = "start /B cmd /c \"$scriptPath\"";
exec($commandScript);
Direct execution of local executables through server-side code (like PHP in Laravel) is not feasible for remote clients due to security restrictions in browsers and operating systems. However, there are alternative approaches to achieve a similar outcome, focusing on client-side solutions and secure remote execution methods.
Your current approach works locally because the PHP
execfunction executes a command on the server's operating system. When deployed to a Plesk server, the command attempts to run on the server itself, not the client machine. For remote execution, technologies like SSH or PowerShell remoting could be considered but are outside the web application's typical use case and, as you noted, come with significant security implications.As noted in the comments, executing an
.exefile on a client's machine through a web application also poses significant security risks. But the functionality you require (interfacing with a weight scale machine) means a local solution due to the direct hardware interaction involved (assuming an application stored in a secure, predefined location).Executing client-side executables from a server-side Laravel application requires a shift in approach, focusing on client-side technologies or secure, authenticated desktop applications that interact with your server.
So a more viable approach would be to trigger the execution from the client side using technologies that the client has control over.
Develop a lightweight desktop application that listens for specific commands from your Laravel application (e.g., via websockets or polling an API endpoint) and then executes the local
.exefiles. That application would need to be installed on the client's machine.To invoke the WebSocket in the desktop application from a Laravel application, you would typically send messages to a WebSocket server that the desktop application is connected to.
The Laravel app would not directly communicate with the desktop application; instead, it would communicate via a WebSocket server that relays messages between the Laravel app and the desktop app.
{"action": "runExecutable", "path": "C:\\ProgramData\\ADWinCT\\RsKey.exe"}).Process.Start(@"C:\ProgramData\ADWinCT\RsKey.exe")in its code.RsKey.exein this example) is then run on the desktop, performing whatever action it is designed to do, in your case, interfacing with a weight scale machine.Regarding Websocket, Node.js with the
wslibrary (or any other WebSocket server implementation you want) is a possible option.In your Laravel application, you can use a PHP WebSocket client to send messages to the WebSocket server, for instance
sirn-se/websocket-php, which replaces the oldtextalk/websocket.You can use this service in your Laravel controller to send messages through the WebSocket:
Make sure to create a route for your controller method if you have not already:
That will allow your Laravel application to communicate with a WebSocket server, which in turn can communicate with any listening clients, such as your desktop application.
You would need to implement authentication for both the Laravel app and the desktop application when they connect to the WebSocket server. That would make sure only authorized applications can send and receive messages. And sanitize incoming messages to avoid executing unintended commands. Use
wss://(WebSocket Secure) for your WebSocket connections to make sure the data transferred is encrypted.The desktop application would also need to be securely authenticated to your server, and you should make sure all communication is encrypted (again, using WSS for websockets).
As a simplified example (in pseudocode):
That would be the basic structure for a console application that listens for commands over a WebSocket connection and executes a local executable file when a specific command is received: it enables a remote server (via secure web sockets) to request the execution of a local executable file indirectly by sending commands to the application running on the client machine.
(Plus, you would need error handling, security features, and more robust command parsing for production use).