PHP execute command on remote Windows computer with administrator rights - php

I am currently creating a computer management intranet site using a LAMP web server and PHP running on an Ubuntu server. To remotely shutdown domain connected machines, I usually run a shutdown command in command prompt on my machine, that tells the PCs to shutdown via Active Directory.
Is there a way to connect to a remote Windows machine from Apache on Ubuntu and run the command under an account with domain admin privileges? If possible, I would not like to have a password in plain text in the PHP script, so can this be achieved using public keys or something like that?

http://php.net/manual/en/function.exec.php
Will let you run command line programs that will help you automate this process, it could let you execute a batch file to do what you're looking for

Related

test windows rdp credentials use and password

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

PHP exec() , showing executed exe file in task manager but not displaying

i have used simple PHP script to execute exe file through php script, below is my PHP code
<? exec('"C:\Windows\notepad.exe"');?>
my problem is, when i check task manager after executing above code, notepad.exe is there but it is not diplaying
same way if i execute below mentioned code
<? exec('"C:\Program Files\WinSCP\WinSCP.exe"'); ?>
task manager shows WinSCP.exe, but winscp is not diplaying
can anyone tell me why applications are not showing ?
Whatever your web server is (Apache, Nginx, IIS...) it's clearly not configured to run with the credentials of your administrator user account. That's the sensible default. Background services are designed to run unattended.
You have several choice, from worse to better:
Kill security in your web server configuration. If web server runs as service, open Windows Service Manager and configure it to run with your account (don't forget to come back here if you change your password in the future). This way you allow any stranger in your computer, local network and/or internet to run programs in your name, in this case Notepad and WinSCP.
If the web server allows it, run it from a command-prompt in your own session. It's similar to #1 but will at least not run 24/7.
Any of the above, with a restricted account.
Don't use a web server. PHP can run from the command-line just fine, what means that you can even create a *.bat file and assign an icon to it.
As about your code, when it finally works your script will sleep until you close Notepad. This can be a bug or a feature depending on your needs.
Had the same problem with WAMP 2.4 & windows XP, the following helped:
Win+R, services.msc
edit wampapache and wampmysqld to allow affecting desktop.
Alternatively you can try:
Win+R, services.msc
edit wampapache and wampmysqld to log on as the same user.

PHP Web App to run Console SUDO Commands (Deciding how to do it)

My company have a cloud dedicated server, hosted in google cloud, running centos 7 with apache2, php5.5 and mariadb running.
The webserver is running a private application for business clients and companys. Every client has his own database and subdomain, so they, and their own clients can access his applicacion going to http://theirname.example.net/
I've created an interactive command line script for client creation. It creates user and secure password, ftp custom folder, create mysqldatabase and populate it with a sql file, create subdomains and other thing. Also i've made another version of this script with no user interaction, receiving parameters as the client name via command line arguments ( /path-to/script.sh usertocreate mysqluser mysql pass).
So, here is the deal, i want to create a web interface, password protected, just available for my company IP address, this interface should be able to run these SH scripts (not the interactive ones) with sudo permissions.
I was thinking on create a subserver in other port (like http://example.org:2501) using another instance of apache (or other webserver) which runs with a specified user with sudo permissions enables only in the neccesary folders.
Before doing anything, i've created a PHP script which runs commands to console, and tried to run SUDO commands with that. Allowing apache user to sudoers list (just to make it work during develop). I could run these scripts from my web app without SUDO permissions, but they where not working at 100% (since some commands require SUDO). When i try with sudo i receive a code 127 error responde (permission problem).
I stopped there and decided to investigate the best way to do this.
I have full control of the server machine. Apache2 and everything normal is running well. (mariadb, proftpd with passive mode active -100 ports added- )
SELINUX is DISABLED. Firewalld Running
SSH is available for use
I can install another webserver in another port to accomplish this. If a lot of HTTPD configurations should be changed to accomplish this, i will preffer to install another webserver
I can also install any 3rd party software.
I'm a PHP Developer with low experience in other programming languages, if it's necesary to invoke any other programming language to do this (maybe like Python) i would love some docummentation links
Access to this web application would be limited only to my company's static ip address and will be protected
Any thoughts/ideas ? Thanks in advance
PS: If someone want to edit my text and add some colours and format, edition will be aprovved
EDIT TLDR: I want to run another httpd in another with a webapi. This webapi should can run console commands as SUDO. Access to this webserver will be limited to my company's IP. I'm not sure if this is the best way to do it and i want opinions. Also, i'm not pretty sure if its possible to run sudo commands from php without any trouble.

PHP socket server

I'm very new to socket programming, but do lot of coding with php.
I have tested some socket server example codes and worked fine with localhost. I use CLI to run the server. But my concern is how do I run the socket server .php file at my hosting server? Do hosting providers normally give access to CLI to run the servers? How do I make sure my server is always running? If the hosting server is restarted, what happens to my server? In case, my server crashes (whatever reason), do I have to run it manually?
Can someone help?
If you are talking about a hosting server I expect you are talking about shared hosting. In that case it will be difficult to keep it stable if you even manage to run the service etc. I would suggests using at least a VPS for it. That way you can run it in the background, automatically start it at reboot but also install software to check the process and restart it if it failed.
For example: Testing whether the reboot startup works is impossible at shared hosting.
I don't know if your provider give you ssh access. Some provider do it but this are managed server or root server.
Then you can run your script over the CLI.
When you can run your server over CLI and when you have enough rights you can insert the script to the runlevel. And there is something that is called "shebang". With this you can give your script direct the php interpreter and run the script without the php command before.
php test.php or /usr/bin/php testScript.php
You can run direct run your script with test.php or name your script only testScript.
When you put your script to /usr/local/bin (for debian) you can run it everytime over the command like the php command.
Edit: I have forgotten something. For this solution you have to copy the /etc/init.d/skeleton to /etc/init.d/runPHPSocketServer for example and change the script values on top. Then you can insert it to the runlevel.
#: testScript or runPHPSocketServer start
When the script is under a executable directory you can insert it to your system runlevel.
#: update-rc.d runPHPSocketServer defaults
So you see there are some solution but for the most solutions you need ssh access.

Using PHP/Python to access data from a remote telnet server over SSH

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

Categories