How to run a php script from windows .bat - php

This is what i want to do in a batch file:
write a file to ftp folder
run php script (http://mylocation.url/script.php)
download a file from ftp folder
FTP i have read before, it is possible to do it in a batch file.
But i don't found a way to execute my php script on my linux server.
Cronjob is not possible for this solution cause the uploaded file will be changed from
Scipt and after i need it downloaded again.
Any one has a solution?
Thanks for help
Phil

plink tool can be used to execute commands in Linux server from Windows through SSH. Below is the syntax
plink.exe root#10.0.0.1 -pw password "<ur script execution command here>"
Say if I want to run a shell script I would type in the command "sh myscript.sh". To achieve this the Linux server should have sshd running.
The command can be used along with other commands part of the batch file.

Related

PHP script fails when launched through crontab ("Directory nonexistent")

I have a PHP script creating a MySQL dump, working perfectly when launched from the browser. It calls mysqldump through the exec() function.
I scheduled this PHP script as root in the crontab (on an Ubuntu VPS). It fails with the following error message :
sh: 1: cannot create ./backups/db_20220522.sql: Directory nonexistent
I have set_include_path('/var/www/mysite.com/'); at the beginning of my PHP script. The script and the "backups" directory are in there.
What am I missing ?
Thanks a lot for your help
You need the specify the full path to your backup directory in your script .
Cron runs from a different directory.
eg
/home/username/domains/domain.com/public_html/backup

How to launch bat file on server with PHP (From Website)

I am hosting a website and minecraft server off of my windows computer, with IIS. They both work perfectly except for that I'd like a way to start the minecraft server from the website. I have a batch file I run to start it regularly so I thought perhaps I could launch that batch file. Some things I've tried:
exec("cmd.exe /C start server.bat");
shell_exec('start server.bat');
I've given IUSR and IIS_IUSRS all permissions on cmd and on the server folder. If it's easier I could also try just running the code within the batch file, from php too. Here is what's inside the server.bat:
cd C:\Users\username\Desktop\_MCServ
:begin
java -Xms3072m -Xmx3072m -Dlog4j.configurationFile=log4j2.xml -jar spigot-1.14.jar
timeout 5
echo resuming server...
goto begin
The loop is so if the server stops, it can automatically start itself again.
In order for this to work you need to run it using cmd and give it an exact path to the bat file. The following code should work for what you need.
system("cmd /c C:[path to file]");

Execute a bash file located on a different VPS

I have a php webpage located on Webserver1, which is from Host1.
I also have a bash script located in Gameserver1 which is from Host2.
Is there any way to send a command from Webserver1 to Gameserver1 to execute the bash file? The webpage and file are on different VPSs. Both are running Debian 7.
The script is literally one line, to execute a java command via a screen, so the server can start if a player notices it's down. The command's available already so it doesn't need to be a secure way of hiding what the command is.
There are 2 ways I can think of. either create a bash file in Webserver1 that connects through ssh and executes the bash script you need on Gameserver1. then run it through php with exec() command.
Or you can create a php file in Gameserver1 that uses exec() to execute the bash script you need on Gameserver1 and call it using file_get_contents() on Webserver1, which is not that secure since anyone can call that file and run your script.

Using PHP Exec() command to run WINRS / WINRM cmd

Platform Details: IIS7, PHP5, Windows Server 2008
Server Name: server1
I'm attempting to use php's exec() function to execute a .bat file which has the following command:
winrs -r:server2 "C:\custom_functions.bat"
However, when I execute that command, it does not work. When running the custom_functions.bat file directly on server2, it works fine therefore, the problem is not likely to be my code. I'm guessing its a permissions error.
When I execute exec("whoami"), it returns "nt authority\network service" as the user.
If I execute any basic windows commands through php exec() function such as exec("ipconfig") or exec("dir c:\"), they work fine. The problem comes when I'm trying to use WINRM to execute a command on a remote server. I have used php's system() function as well - with same results.
Please help!?
To run a command on a server, you need authorization on that server. When you run the command manually from the prompt, you have access through your user account.
When IIS runs the command, it runs as a build in user, that has no access to server2.
I do not advise to give the webserver process access to server2.

Php : running ssh from Windows to login to a Linux and run a script

Here's my goal :
I have a Windows XP PC with all the source code in it and a development database.
Let's call it "pc.dev.XP".
I have a destination computer that runs Linux.
Let's call it "pc.demo.Linux".
Here's what I've done on "pc.dev.XP" (just so you get the context) :
installed all cygwin stuff
created a valid rsa key and put it on the dest
backup computer so that ssh doesn't
ask for a password
rsync works pretty well this way
If i try to do this on "pc.dev.XP" via a command line :
cd \cygwin\bin
ssh Fred#pc.demo.Linux "cd /var/www && ls -al"
this works perfectly without asking a password
Now here's what I want to do on the "pc.dev.XP":
launch a php script that extract the dev. database into a sql file
zip this file
transfer it via ftp to the "pc.demo.Linux"
log to the "pc.demo.Linux" and execute "unzip then mysql -e "source unzipped file"
if I run on "pc.dev.XP" manually :
putty -load "myconf" -l Fred -pw XXX -m script.file.that.unzip.and.integrates.sql
this works perfectly.
Same for :
cd \cygwin\bin
ssh Fred#dest "cd /var/www && ls -al"
If I try to exec() in php (wamp installed on "pc.dev.XP") those scripts they hangs. I'm pretty sure this is because the user is "SYSTEM" and not "Fred", and putty or ssh ask for a password but maybe I'm wrong.
Anyway I'm looking for a way to automate those 4 tasks I've described and I'm stuck because exec() hangs. There's no problem with safe_exec_mode or safe_exec_dir directives, they're disabled on the development machine, thus exec() works pretty well if I try some basic stuff like exec("dir")
Any idea what I could do / check / correct ?
I'm not sure if this is what you need, but I typically use a construct like this to sync databases across machines:
php extractFromDb.php | ssh user#remote.com "mysql remoteDatabaseName"
This executes the PHP script locally, and pipes the SQL commands the script prints out through SSH straigt into the remote mysql process which executes them in the remote database.
If you need compression, you can either use SSH's -C switch, or integrate the use of your compression program of choice like this:
php extractFromDb.php | gzip -9 | ssh user#remote.com "gunzip | mysql remoteDatabaseName"
You want to do this from PHP running under apache, as in I go to http://myWebserver.com/crazyScript.php and all this happens? Or you just want to write your scripts in PHP and invoke them via cmd line?
If you want the first solution, try running your apache/iss under a different user that has credentials to perform all those tasks.
"if I run on the development PC manually this works perfectly.".
Why not do it like that? When you run that script, I assume you're connecting to the local SSH server on the dev machine. When you do this, you are using the credentials Fred, so everything works. When you run the PHP script, you are right that it is probably running as SYSTEM.
Try either changing the user that apache is running as or use php to connect to the local ssh thereby using alternate credentials.
Here's what I did :
a batch file that :
Calls a php file via "php.exe my_extract_then_compress_then_ftp.php"
Calls rsync to synchronize the source folder
Calls putty -l user -pw password -m file_with_ssh_commands_to_execute
It works like a charm.

Categories