Getting local PC IP ( for use in cron script ) - php

I created a script to search for, copy, compress and send files from a Cliet/Local PC to a Master PC.
The files need to be identified on the master PC. the format is
`UUID.IP.crc.gz`
So the PC running the script (Local), I need that PC's IP address.
I've tried using $_SERVER["REMOTE_ADDR"] and $_SERVER['SERVER_ADDR'], but none of those where defined.
Notice: Undefined index: SERVER_ADDR in C:\wamp\www\postEnginInstruction\properties.php on line 9
and the same for "REMOTE_ADDR"
What am I doing wrong?

If you use php with a CRON, there is no HTTP request nor server, you just use it as a script language. So you can't access $_SERVER value.
You have to use then a command like ipconfig (or ifconfig on linux) with the exec command in PHP.
Edit : look here also for easier solutions : How do I find my server's IP address in PHP(CLI)

One thing you can do to 'create' an HTTP request is schedule a curl-command to activate your PHP script:
curl http://your.server/path/to/your/scheduledScript.php
There is however one downside: your scheduled script would also be invokable from the outside. This is something you should manage. (Either through .htaccess Allow/Deny directives, or some form of generating and providing one-time tokens from CRON).
(One last sidenote: make sure you use the DNS name of your server, if you use localhost, the address is probably going to be 127.0.01, which is not very helpful) ;)

Related

Bottle web server - how to serve PHP file?

I am working on a webapp made by someone else which uses Bottle routing. I want to create a simple login page which requires some PHP. If I return the PHP page as a static_file, any HTML will be executed but PHP won't, for obvious reasons. How should I serve the PHP file so that it is dynamic?
Not working:
#route('/login')
def serve():
return static_file('login.php', root='.')
In order to server PHP files, you need to have PHP installed on the web server. Additionally, the webserver needs to be configured to detect PHP files and execute them.
Serving PHP files from Python is kinda useless and not recommended.
I'd recommend you to take the time to translate this script from PHP to Python.
I wanted to do the same thing yesterday, but the answers I got to my question made it clear it was either impossible or extremely difficult. I came up with writing a small python program to run the PHP built in server. NOTE: PHP needs to be able to run from the command line for this to work.
#Import the os package so that this code can run commands
import os
#Get the port that the user wants to host on
port = str(input("What port would you like to host on?"))
#Add wanted port to the command that hosts the php server
cmd = "php -S localhost:" + port
#Actually run the command to host php server
os.system(cmd)
#Now the PHP server will take over until you
#use ctrl + C to quit hosting
Just remember that the port needs to be 4 numbers. When you host this, you can return any file from the folder you ran this code in by simply typing it in the browser. Example:
localhost:8080/login.php
Returns login.php (if it is there) on the localhost port that you asked for.

Why would a file_get_contents() and a cURL POST have two different origin IP addresses?

I have a PHP script that runs from our server's non-publicly-accessible directory as a cron task. The script sends cURL POSTs to a publicly-accessible URL.
I set up the receiving URL with a .htaccess file that denies access from any IP address other than the ones I allowed. I ran a PHP script that emailed me the output of a file_get_contents("http://www.whatismyipaddress.com"); to get my server's origin IP address (it's a dedicated server so I don't expect it to change). That's the IP I allowed in the .htaccess file.
But when I ran the script through an SSH prompt, all of the POSTs returned 403 Forbidden errors. When I commented out the .htaccess protections, the POSTs succeeded.
Then I looked at my log files and it turned out the POSTs were coming from a different IP than what was reported by my file_get_contents(). It was easy enough to add that IP to the .htaccess file, which fixed the problem.
But I'm confused as to why there are two different origin IP addresses. Can anyone shed some light on the subject?
First of all, that's a weird way to find out your server's IP address. Tried these?
PHP how to get local IP of system
Second: Do you know if the server is behind a firewall/NAT or something else that redirects traffic? It could be that outgoing traffic from your server via the webserver is allowed, but traffic when called from the command line (different PHP process) goes another route through a proxy so that you end up with 2 different IP addresses in your destination server.
Edit: Clarified something
Edit2: Try this:
On your publicly accessible server, set up a script i.php with the following content:
<?php die($_SERVER['REMOTE_ADDR']); ?>
Then, on your private server that runs cron, make a script like that:
<?php echo file_get_contents("http://publicserver.com/i.php"); ?>
Run it from your browser. Then run it from the shell via SSH. Then, via SSH, execute the following command:
curl http://publicserver.com/i.php
Compare output. Is it all the same?

Changing ip address of apache server using php

I am trying to create a settings page(for the clients) where in they can view the current up address,change the ip address etc. I have a php file to view the ip address
<?php
$res=shell_exec("ifconfig");
echo $res;
?>
This code works just fine and displays the expected result.
However the code to change the ip address of the server is not working properly.
<?php
shell_exec("ifconfig eth0 192.168.163.136");
?>
After running this code when i check the ipaddress on the terminal using ipaddr i don't see any change in the ipaddress.
Pls point out where i am going wrong. I think its a problem of apache not being a super/root user. If that is the case i don't know how to make apache run as a root user.
Your PHP script doesn't have enough privileges to change the interface address. You may want to write a Shellscript, give it the right privileges (e.g., change its owner to root, and set the suid bit), then run it from your PHP script. Yet, I advise against doing such thing
IP address are configured in Network Layer of an Network Protocol, not in application layer where PHP runs. Simply, PHP does not have access to it and cannot changed them.
Just imagine the vulnerabilities it could create if this was possible.
Adding /srv/http before ifconfig worked. All it needed was the root owned location.

Nginx and PHP-cgi - can't file_get_contents of any website on the server

This one is best explained by code I think. From the web directory:
vi get.php
Add this php to get.php
<?
echo file_get_contents("http://IPOFTHESERVER/");
?>
IPOFTHESERVER is the IP of the server that nginx and PHP are running on.
php get.php
Returns the contents of the (default) website hosted at that I.P. BUT
http://IPOFTHESERVER/get.php
..returns a 504 Gateway Time-out. It's the same with curl. It's the same using the PHP exec command and GET. However, from the command line directly it all works fine.
I've replicated it on 2 nginx servers. For some reason nginx won't allow me to make an HTTP connection to the server its running on, via PHP (unless it's via the command line).
Anyone got any ideas why?
Thanks!
Check that your not running into worker depletion on the PHP side of things, this was the issue on my lab server setup which was configured to save RAM.
Basically I forgot that your using a single worker to process the main page been displayed to the end-user, then the get_file_contents() function is basically generating a separate HTTP request to the same web server, effectively requiring 2 workers for a single page load.
As the first page was using the last worker there was none avaliable for the get_file_contents function, therefore Nginx eventually replied with a 504 on the first page because there was no reply on the reverse proxy request.
Check if allow_url_fopen is set to true in your php.ini.

Working with PHP $_SERVER['SERVER_ADDR'] variable when used in command line script

It seems as if I cannot use the $_SERVER vars in a command line script in PHP as the return seems to be blank? If this is the case then does anyone have a suggestion for getting the IP of the server I am running the script on?
Thanks in advance.
SERVER_ADDR is defined by the webserver as CGI environment variable, and depends upon which interface it listens to. A server might have multiple network addresses.
You can fetch it from either ip addr or ifconfig, preferrably looking for the eth0 interface (most common).
preg_match("/inet (addr:)?(?!127)(\d+(\.\d+){3})/", `ip addr`, $m);
use grep command
http://www.linuxquestions.org/questions/linux-general-1/how-to-get-an-ip-address-from-command-line-522676/

Categories