Changing ip address of apache server using php - 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.

Related

shell_exec in PHP

I have PHP code (setup.php) that execute bash-script, this is my code :
$hasil = shell_exec("./setup-srv.sh testajah mydomain.com");
echo "<pre>".$hasil."</pre>";
successfull when executed from commadline (setup-srv.sh), but failed when executed from browser (setup.php)
these 2 files at /var/www/html/
setup-srv.sh has several jobs :
register subdomain to DNS server
create and populate data to mysql database
copy master folder and it's contents to new folder
execute certbot to new subdomain
I chmod 2 files with 777, but still failed.
I need help, thank you at advance.
I'm assuming you're on a newer PHP than 5.3, or you have safe mode off.
This is likely to be an issue with the differences between your environment and the web server's environment.
Things to try:
Unlikely to be the problem, but ..
$hasil = shell_exec("/var/www/html/setup-srv.sh testajah mydomain.com");
SU to the webserver user and try to run the script, see what happens.
Make sure SELinux isn't interfering with the webserver's ability to execute external programs.

Getting local PC IP ( for use in cron script )

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) ;)

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?

I keep getting 'Host key verification failed.' when calling `ssh` from PHP on Mac OS X

I have set up a password-less login from my laptop (OS X) to my dev server (Linux) - this works fine (using ~/.ssh/id_rsa private key, and having put the contents of ~/.ssh/id_rsa.pub into the servers ~/.ssh/authorized_keys file). So far, so good.
However, I am trying to connect to the server from within a PHP script. The Apache server supposedly runs as the _www user, and I have added the necessary hosts line to ~_www/.ssh/known_hosts without solving the problem.
I am feeling that there is something basic and fundamental about how Apache on OSX works that I am missing - any suggestions on what I might be doing wrong?
Thanks!
EDIT: I have confirmed that _www is indeed the currently logged in user, and the home directory is indeed /Library/WebServer. I added the known host to /Library/WebServer/.ssh/known_hosts, and I also forgot to mention that the private key file has been made readable by the _www user, and when I call the script I call ssh -i /path/to/private.key.
I figured it out - the .ssh directory in /Library/WebServer was owned by root:wheel (like the other files and directories in that directory, thus hiding the fact in plain view...). I changed ownership to _www:wheel, and it works like it should now! Thanks, and sorry for wasting your time.

Mapped Network Drives

I have mapped a network drive to a computer in my home network. Now I am trying to access it via PHP - I did this quick test:
echo opendir('Z:\\');
This gives me:
Warning: opendir(Z:\) [function.opendir]: failed to open dir: No error in C:\wamp\www\webs\tester-function.php on line 3
What have I done wrong here?
I don't want my users typing in the UNC path so is there a way to get the UNC path for them and maybe that will work when I try to access it? This is possible in Microsoft languages but I am not sure how to get PHP to do this - maybe using a cmd.exe command?
Please note, the mapped drive does exist as I can see it and I can access it. It also does not appear to be a permissions problem as I am assuming it would of complained about this IF it could access that drive...right?
Thanks all for any help
The User Contributed Notes to opendir() have this:
I was trying to access network drives using this opendir function. I read so many posts saying that it was almost impossible to access a network drive and finally, I found the answer; there are 2 steps to be followed to access a network drive with PHP either on the same machine or another machine.
in this case, the user rights seemed indeed to be the problem, which can be different from your rights depending on what user PHP / the web server are running on.
Open the Services MMC by going to Start -> Run (or by using the search box, if using Windows 7) and typing Services.msc and pressing enter.
Find the Apache service. Having never used WAMP, I have no idea what it's been named. Hopefully it starts with the word "Apache" though, and thus shouldn't be too hard to find.
Right-click on the service and select Properties.
Switch to the Log On tab.
Select the radio button next to "This account:" and enter in the credentials for the user account you want to run Apache under. If it's a network/domain account, use the "DOMAIN\user" syntax (or search for the account using the Browse button).
Select OK and restart the Apache service.
Verify that your changes worked by looking for the "httpd.exe" process in Task Manager and checking to see which user name the process is running under.
reference http://board.phpbuilder.com/board/showthread.php?t=10371870&page=2
simple, use: "\\\\server\\dir\\subfolder" as the path. make sure you can access the "server".
works for me.
$arquivo = "\\\\server\\dir\\subfolder\\file.txt";
echo is_file($arquivo); //print 1.

Categories