LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 05-31-2012, 07:50 AM   #1
secondhandman
Member
 
Registered: May 2012
Posts: 60

Rep: Reputation: Disabled
php dynamic ip script


I want to create a script that will send an email every time the dynamic ip of my site changes. I have followed the following steps to install php mail options:

apt-get install php-pear
pear install mail
pear install Net_SMTP
pear install Auth_SASL
pear install mail_mime
apt-get install postfix

I have a script that reports the ip address when ever it's ran, but I only want it to send a message when the ip address changes. The code for reporting my public ip address is as follows:

<?php
$to = "me@example.com";
$subject = "Routers ip address";
$body = exec("curl -s http://whatsmyip.net/ |grep -i '<title>' | cut -d : -f$
if (mail($to, $subject, $body)) {
echo("<p>Message successfully sent!</p>");
} else {
echo("<p>Message delivery failed...</p>");
}
?>

I have tried a few thing to get this to work, but no luck. I would like this script to only send a message when the ip address changes if possible:

/* DOES NOT WORK */

<?php
$body = exec("curl -s http://whisismyip.net/ |grep -i '<title>' | cut -d : -f 2 | sed 's!</title>!!'");
$ip = "/home/user/Documents/currentip";
/*currenip is a file with the current ip address in it! */

if ($body != $ip) {
$to = "user@example.com"
$subject = "Public ip address";
$body = exec("curl -s http://whisismyip.net/ |grep -i '<title>' | cut -d : -f 2 | sed 's!</title>!!'");
} else {
echo("<p>Message delivery failed...</p>");
}
?>

This last script doesn't work!! I've tried! Please help, you're my only hope!
edit: also it doesn't send mail!

Last edited by secondhandman; 05-31-2012 at 08:08 PM.
 
Old 05-31-2012, 08:45 AM   #2
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,830

Rep: Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308
At least the sed script is incorrect: sed 's!</title>!!' would be better.
Do you have any error messages?
 
1 members found this post helpful.
Old 05-31-2012, 12:14 PM   #3
secondhandman
Member
 
Registered: May 2012
Posts: 60

Original Poster
Rep: Reputation: Disabled
No errors! It hangs for a short time and returns me to the command line.

That was typo! I'll edit the post.

Thanks for the comment.

Last edited by secondhandman; 05-31-2012 at 12:18 PM.
 
Old 05-31-2012, 01:25 PM   #4
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,830

Rep: Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308
$ip = "/home/user/Documents/currentip";
this line means the variable ip will be equal to the filename, not the content of the file. So you need to open the file and read the first line. You also need to check the case when this file was not available. Also, if ip changed you need to save it.
 
1 members found this post helpful.
Old 05-31-2012, 02:44 PM   #5
Babertje
Member
 
Registered: Jun 2009
Location: Haarlem, The Netherlands
Distribution: Archlinux
Posts: 125

Rep: Reputation: 20
Maybe a betterway is to use only php to fetch your IP
Code:
<?php
// Freegeoip.net csv service
// to me this returns: 86.93.xxx.yyy,NL,Netherlands,07,Noord-Holland,Haarlem,,52.3667,4.65,
$url = 'http://freegeoip.net/csv/';
$data = file_get_contents($url);
// only need first part
$ip_adress = split(",", $data);
echo $ip_adress[0];
?>
 
1 members found this post helpful.
Old 05-31-2012, 07:19 PM   #6
secondhandman
Member
 
Registered: May 2012
Posts: 60

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by pan64 View Post
$ip = "/home/user/Documents/currentip";
this line means the variable ip will be equal to the filename, not the content of the file. So you need to open the file and read the first line. You also need to check the case when this file was not available. Also, if ip changed you need to save it.
How would I go about doing this? I think I'm having trouble with the if statements?

I'll try out that code Babertje! Thanks.
 
Old 06-01-2012, 02:25 AM   #7
Babertje
Member
 
Registered: Jun 2009
Location: Haarlem, The Netherlands
Distribution: Archlinux
Posts: 125

Rep: Reputation: 20
To get the content of that file
Code:
$stored_ip file_get_contents('/home/user/Documents/currentip');
also take a look at the php reference http://www.php.net/manual/en/langref.php this is your most usefull place to look for any php related solutions I guess.
 
1 members found this post helpful.
Old 06-08-2012, 12:03 PM   #8
secondhandman
Member
 
Registered: May 2012
Posts: 60

Original Poster
Rep: Reputation: Disabled
I still can't get this to work Very new to php and linux. Thanks for your help!

Script still does nothing, just hangs!
 
Old 06-08-2012, 12:14 PM   #9
secondhandman
Member
 
Registered: May 2012
Posts: 60

Original Poster
Rep: Reputation: Disabled
can I call on php in a shell script? That might help me make it work!
 
Old 06-08-2012, 12:25 PM   #10
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,830

Rep: Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308
yes, you can: http://php.net/manual/en/features.commandline.php
 
1 members found this post helpful.
Old 06-08-2012, 01:40 PM   #11
Babertje
Member
 
Registered: Jun 2009
Location: Haarlem, The Netherlands
Distribution: Archlinux
Posts: 125

Rep: Reputation: 20
Quote:
Script still does nothing, just hangs!
Open a terminal and type:
Code:
tail -f /var/log/httpd/error_log
Run your script and if your php script doesn't work correctly you can get the concept of what goes wrong.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
PHP Warning: PHP Startup: Unable to load dynamic library '/usr/lib/php5/20090626/xsl malayo Debian 8 07-02-2013 04:57 AM
PHP Dynamic links? waelaltaqi Programming 2 06-27-2009 08:29 AM
LXer: Title: PHP/MySQL Classifieds Script AddAsset1.php Script Insertion LXer Syndicated Linux News 0 07-02-2006 06:21 PM
PHP - dynamic content jacksmash Programming 11 11-25-2003 02:43 PM
Php Dynamic Refresh Bheki Linux - General 0 05-16-2002 04:56 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 09:25 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration