Possible to connect to Remote Desktop with PHP? - php

I've got a few servers that I'd like to connect to every so often to run a program. However, to make it easier, I'd like to make a PHP script connect to each of them via remote desktop and run each of them. Is this possible? If so, where should I start? An example would be lovely.

In theory you could implement a PHP script that could communicate with a server in the RDP protocol, but I certainly wouldn't want to try to build a script that can do anything meaningful, as RDP is built to expose a remote machine's user interface to the connected client and PHP is built first and foremost to manipulate text.
If you want a PHP script to remotely control another computer, then SSH is a far more sensible option, as PHP could easily connect to the remote cmputer's CLI and issue commands to the server via shell commands, which are textual and therefore easily generated with PHP.
On the other hand, interfacing with the remote computer with RDP would be extremely difficult. Just think about clicking on an icon to get a directory listing for a start. You'd first have to determine where the mouse pointer is, whether the icon you want to click is visible and if so where its bounding box is relative to the mouse pointer. You'd then have to issue commands to move the mouse pointer to within the bounding box, then check that the mouse pointer is in the right place (a local or other remote user might be moving the mouse around) and then issue a pair of clicks with a short delay between them.
That's going to be a lot harder than issuing "cd C:\Program Files\" followed by "dir", for example.

No PHP cannot use RDP to connect to the server. They can use SSH or FTP or a socket to a listening port.
What you are asking is kinda crazy!!! =)
Edit:
Since you insist on doing it here is what you need:
[MS-RDPBCGR]: Remote Desktop Protocol: Basic Connectivity and Graphics Remoting Specification
[MS-RDPCR2]: Remote Desktop Protocol: Composited Remoting V2 Specification
[MS-RDPEA]: Remote Desktop Protocol: Audio Output Virtual Channel Extension
[MS-RDPEAI]: Remote Desktop Protocol: Audio Input Redirection Virtual Channel Extension
[MS-RDPECLIP]: Remote Desktop Protocol: Clipboard Virtual Channel Extension
[MS-RDPEDC]: Remote Desktop Protocol: Desktop Composition Virtual Channel Extension
[MS-RDPEDYC]: Remote Desktop Protocol: Dynamic Channel Virtual Channel Extension
[MS-RDPEFS]: Remote Desktop Protocol: File System Virtual Channel Extension
[MS-RDPEGDI]: Remote Desktop Protocol: Graphics Device Interface (GDI) Acceleration Extensions
[MS-RDPELE]: Remote Desktop Protocol: Licensing Extension
[MS-RDPEMC]: Remote Desktop Protocol: Multiparty Virtual Channel Extension
[MS-RDPEPC]: Remote Desktop Protocol: Print Virtual Channel Extension
[MS-RDPEPNP]: Remote Desktop Protocol: Plug and Play Devices Virtual Channel Extension
[MS-RDPEPS]: Remote Desktop Protocol: Session Selection Extension
[MS-RDPERP]: Remote Desktop Protocol: Remote Programs Virtual Channel Extension
[MS-RDPESC]: Remote Desktop Protocol: Smart Card Virtual Channel Extension
[MS-RDPESP]: Remote Desktop Protocol: Serial and Parallel Port Virtual Channel Extension
[MS-RDPEUSB]: Remote Desktop Protocol: USB Devices Virtual Channel Extension
[MS-RDPEV]: Remote Desktop Protocol: Video Redirection Virtual Channel Extension
[MS-RDPEXPS]: Remote Desktop Protocol: XML Paper Specification (XPS) Print Virtual Channel Extension
[MS-RDPNSC]: Remote Desktop Protocol: NSCodec Extension
[MS-RDPRFX]: Remote Desktop Protocol: RemoteFX Codec Extension
You can find all those here: http://msdn.microsoft.com/en-us/library/cc216513(v=PROT.10).aspx
With this documentation you can determine the feasibilty of your project.

I am using
rdesktop -u username -p password 192.168.1.131 -g 1024x640
in linux,
where 192.168.1.131 is the destination ip
and 1024x640 is the window size.
Also you can use
rdesktop -u username -p password 192.168.1.131 -f
for full screen.
Don't forget to replace the username and password with your details.

I think it be best if the servers were running a service that you could connect to. For example they could be running a web service (e.g. php if that's your desired language). You could then connect to that web address through port 80 which could run the applications. Of course you have security concerns, but you should be able to configure them so that they only accept requests from certain IPs. Alternatively you could make the script password-protected

The reason ppl are not recommending RDP to do this is because RDP has no API to do even remotely what you actually want.
The easiest way to do this while somehow staying within your requirements, is to install one of the software linked below on the host machines. Then you would need an API to connect:
This: http://www.codeproject.com/KB/system/remoteexec.aspx. In PHP all you would need is something as simple as exec("rexec 123.21.61.12 notepad.exe "\fileserver1\test file.txt") (you can modify it to accept a key for a bit of security)
TeamViewer (free): http://www.teamviewer.com/en/products/remotecontrol.aspx Then you would use TeamViewer WebConnect to connect to via PHP
OpenVPN (open source) Not sure if there is an API that comes with it but I wouldn't be surprised
Log me in (same as above)
Install WAMP on the hosts and then have php run the commands on them. So it would be something like php to php where ur client would call something like curl(127.0.0.1/?key=loooong&command=blah) and the server would run exec("passed command")

vmware Esxi uses such a ui page but not with PERL application, After reading the answers it seems a real challenge. I'm working on such a thing for my web based hosts access. If I get something done, I'll be happy to share with you.
https://labs.vmware.com/flings/esxi-embedded-host-client

Honestly, you're best bet would be C#. This probably isn't what you want to hear, but it's extremely trivial to make an rdp client in that language.
If you're allowed to create a user that has access to the logs. I'd make something that would run when the user is connected. The program would upload the logs to a unix server that has FTP access.
Then even if you can't write a one click solution in C# you could at least just login manually wait for the upload to be done then log out.

Why not running a perl script from a cronjob or execute the programs with the backquotes?

If all you want to do is to connect to your many servers and run the same program, you could do a one-time upload of a php execute script to all your servers that should exec() a command from input data (most likely from $_POST or $_GET). You then write a local script, which will send the command string to run your program to all the exec scripts on all your servers, so essentially typing the command to run whatever program you need will execute the command on every server.
Obviously you'll need to require authentication for your exec script so nobody can abuse your servers. Of cause, this whole thing wouldn't be suitable if the program you're running requires much interaction (you did say you wanted to run a program, which implies you want to execute it and leave it to do it's own thing)

Why not use FTP for what you need?
then generate and execute files with simple commands microsoft.

I think it would be better to set up daemon/service on server to which you want connect and communicate with it via TCP. In this case, you are free to implement any protocol you want.

Related

Access files in remote computer with php

I need to create an application using php which is hosted in a server and need to communicate with a csv file located in clients local machine. Is there any way we could do this ? How can I connect to a remote csv file ? Is this possible ?
Server machine accessing a csv file directly from a client machine is not a good idea. It's a security threat indeed. Consider you are navigating some website and it's server is able to access your computer's file system!!!
There are various alternatives to achieve this, some of these might be:
Make the user upload csv files to server in order to make it
available to the server application
If the client and server are in the same network, then share the
folder on client machine to make it accessible from the server
etc... I would have preferred the first option as mentioned above.
As #AnthonyB mentioned in comment under your question, server can't directly call client, and that is true. Server is called "server" as it serves requests from the client.
To be able to give away files to remote requests, your client needs its own server application, like Apache HTTPD for example.
In case if you need continuously request client's server to collect files with your PHP server, what you are looking for called "worker". One of AWS tools called Elastic Beanstalk offers possibility to choose a server or a worker application during start up wizard for PHP. It is pretty straight forward and easy to use.
Please note, that your client must have dedicated IP address or use Dynamic DNS approach by pushing its IP to a DB (or directly to a server) where worker will take it from.
If you don't need dedicated worker, you can configure CRON JOB to send requests to clients server applications.
IMHO, all that scenario worth it only if you are building corporate grade application. In most cases (and if you do REALLY need to collect files from clients) you have to install Apache + PHP server on the client side and make this guys to wait for request from YOUR remote php server. Without it, you can not get files from clients computers via browser without user input interactions. At least legally :)

Run PHP code on local MacOS Jekyll site, port 4000

I've build a Jekyll website on my localhost (MacOS Mavericks). The website is served at http://localhost:4000/website/ and everything regarding Jekyll is running just fine.
However, I now want to have a contact form in PHP that allows me to receive emails. I placed a contact.php file in the website/ folder and have the form POST to that file. On my remote web server, this is working perfectly. However, on the localhost, the PHP isn't parsed, and plain text is displayed on contact.php. However, PHP is parsed perfectly on localhost/contact.php.
How do I get my localhost (Apache? PHP?) to process PHP files on my local Mac http://localhost:4000/ (without breaking my Jekyll website that listens on the same :4000 port)?
You can't use the same port. The port determines the application endpoint that will handle the request on the IP address. The Jekyll server (WEBrick library) uses port 4000 as a default.
The typical way to handle this problem, is to use a "web service" to add dynamic functionality. For instance, the jekyll docs suggest using something like FormKeep, or SimpleForm.
What you're asking is to setup a "web service" yourself. To do this it would need to be on another port or another IP address. The "service" will simply act as an endpoint to accept and process your form post. In this case you could setup a webserver using Apache/PHP on a different port than Jekyll -- such as the standard port 80 -- then write a PHP script (e.g., webform.php) that in combination with the static form is setup to respond and process your form.
Note: It is possible to configure both Jekyll and Apache to respond to requests on port 4000. However, both applications (aka servers) can't be running at the same time. The ip:port combination determines which application an internet request is sent to.
I realize the post is old but this may help someone...
The answer by Mike Stewart is excellent and describes what needs to be done to accomplish the goal.
To add to that answer, here are the specifics of how I do this type of development on a Mac.
Configure CORS in Apache
Run the Jekyll site on default port 4000
Run MAMP stack on default port 8888
Code goes in MAMP's htdocs folder (htdocs/your_project)
PHP resides in a separete "php" or other folder inside the "your_project" folder
Jekyll watches the "your_project" folder and compiles to _site as normal
The CORS issues you'll experience can be resolved locally during development several ways. Here is a good resource for enabling CORS on Apache: http://enable-cors.org/server_apache.html
Once you have CORS configured you'll be able to make Ajax calls to the PHP on port 8888.
I'm running the PHP built-in web server alongside the Jekyll server. I opened a second Terminal window and navigated to the _site folder. The command is php -S localhost:8000 (or whatever port you want to use that is not 4000).
Note that I'm using viewing localhost:8000 in the browser, but having the Jekyll server running simultaneously is nice because Jekyll keeps the build updated as I make changes to the source code (refresh required).

Load a page server-side and deliver to web browser

I'm working on a raspberry pi project where i've set up a media centre with an apache web-interface over my LAN. I've recently installed the deluge-web browser GUI for managing torrents which runs over a different port.
I'm forwarding port 80 so that I can access the media center interface when i'm away from home for management, but would like to get it set up so i can access the deluge-web ui without having to forward its port, so that i can implement a stricter security control using mysql or php with apache.
So what i'm trying to do is set up a frame which is loading the deluge-web ui on the server-side, visible and accessible by the browser.
I'm running apache 2.2.22, php 5.4.39, raspian (debian 7.8).
Is that it all possible?
No. A frame is a client side technology. It can't do anything about permissions or creating network paths to internal servers.
If you want to make a service accessible with added authentication, then you'll need to look at using something like an SSH tunnel or HTTP proxy instead.
If I understand right, what youre looking for is curl. Check Curl Documentation on PHP Site. Basically, it can download any url (page/file/whatever) and then you can use it in your php script, possibly dumping it into an iframe or div.

Execute local application via PHP from server

My company has an internal Linux server running PHP with an application that displays client specific information, such as SSH connection info, etc. Is there a way to have the server invoke a command on a local machine via PHP? For example, the server displays a link with SSH connection information, such as c:\putty.exe -ssh someone#192.168.1.100 -pw MyPassword`. The user - on a windows machine - clicks the link and Putty fires up with all the connection information.
I believe Flash's fscommand has the capabilities I'm looking for, although I would much prefer to use PHP, if at all possible.
PHP is a server side technology and as such does not have any control over the client. You'll have to use a client side tech like flash or if it's IE only, ActiveX
Only way to things like this is mount your resource on server machine. To do that you have to have a server that will be visible for server and able to host your files.

To use cURL FTP does both servers need the PHP cURL library installed?

I'm trying to wrap my brain about how to do this. We need to provide some files within a directory from our servers to our clients' servers via a PHP/Web interface using FTP. I've looked at the FTP capabilities built in to PHP and some custom classes, but someone suggested cURL might be a better option. We will have the FTP login credentials in our database for the application to access. With that information can we use cURL FTP capabilities to do the transfers, knowing our server has libcurl installed, but the clients servers may not? Do both servers have to have it for the FTP function to work?
Or am I completely going about this the wrong way, and have misunderstood how to use cURL and should be looking into an FTP PHP class?
libCURL is a library; it acts as the client.
Your clients need to be running a FTP server but do not need libCURL.
Just to make it super clear, there are 2 computers involved:
Your server, the one that's supposed to provide files to the client using the FTP protocol. That server does not need to have a web server (or PHP) running. The only thing it needs is an FTP server. It also needs to have permissions configured in such a way that there is an account that can access the files through FTP.
Your client's server, the one that's supposed to retrieve files from your server using the FTP protocol. That server needs to have PHP installed, with libCurl. The software on that server needs to access your server using the FTP protocol, providing the user credentials that you configured on your box.
Hope that helps.
It sounds like what you want to do is have the client connect to your PHP script & then push a button to start an FTP transfer that sends a file from your FTP server to their FTP server. If this is the case, then all you need is cURL on your server.

Categories