Scripting a cron job to post data to a url via variable

L

lizardthefish

Guest
Cron Job to Post Server on Dynamic IP to Static IP Server via PHP

I edited this post to make it more of a guide for anyone that it might help.

You need these tools for this to work for you:
-- computer on dynamic IP with solid internet connection (this is the one that you need to log into while out of the office or away from home.)
-- a static IP server elswhere, where you have PHP running (this is where you will store the IP of the home computer so you can find it and log in as necessary.)

create a text file on your server that will hold your IP address. I created mine outside my web directory for privacy. but php can see it and write it outside the web directory just fine.

my text file:

/www/someconfigfiles/myIP.txt

====FILE====
123.123.123.123
====/FILE====

I have yet to modify the php file to allow me to easily retrieve the IP. I am going to password protect the IP to allow myself and authorized individuals access. This will restrict the number of people that even know where on the net my home computer is. adds yet another level to my security.

Here is the current file 'myIP.php' that I uploaded to the server:

'/www/myaccount/mysite/myIP.php'

====FILE====
<?
$IP_File = '/www/someconfigfiles/myIP.txt';

// open the file for writing
$fh = fopen($IP_File, 'a+') or die ($php_errormsg);

// clear out the file
rewind($fh) or die ($php_errormsg);
ftruncate($fh,0) or die ($php_errormsg);

// write IP and close file
if (-1 == (fwrite($fh,$ip_data))) { die ($php_errormsg); }
fflush($fh) or die ($php_errormsg);
fclose($fh) or die ($php_errormsg);

?>
====/FILE====


Then, as you see if you read below in this post, masaka___ helped me work out the script that will be run as a cron on the home computer. Here is that local script that was made executable to be able to be run manually as well as by the cron:

'/usr/bin/postIPToServer'

====FILE====
curl -s http://www.yourdomain.com/myIP.php?ip_data=`curl -s http://www.showmyip.com/simple/ | awk '{print $1}'`
====/FILE====


and lastly the cron entry I used to run it hourly, in fact, the whole **** crontab file, for your convenience:

'/private/etc/crontab'

====FILE====
# /etc/crontab
SHELL=/bin/sh
PATH=/etc:/bin:/sbin:/usr/bin:/usr/sbin
HOME=/var/log
#
#minute hour mday month wday who command
#
#*/5 * * * * root /usr/libexec/atrun
#
# Run daily/weekly/monthly jobs.
#The line I added to run the IP Post
0 * * * * root /usr/bin/postIPToServer
#Below is not related to the mac-forums tutorial
15 16 * * * root periodic daily
00 13 * * 6 root periodic weekly
30 18 1 * * root periodic monthly
====/FILE====

hope this helps someone.

Lizard


+++++++++++++++++++++++++++++++++++++++++++++

Here is my original post that sought help with this

+++++++++++++++++++++++++++++++++++++++++++++

I have a G4 desktop on a dynamic IP and want to set it to post its IP address to a remote server every hour so I can find the little guy while I'm out of the office.

Here is what I have worked up so far. I don't even know all the tools I have available so if you see that I am building a model car with 2x4's please let me know so I can reapproach the project.

That said...

I got this script from gatorparrots in these forums and have found it to work reliably (thanks gator):

curl -s http://www.showmyip.com/simple/ | awk '{print $1}'

It simply spits out the IP address, like so:

12.123.124.123

then I used curl for this line that will pass the IP to a PHP script that will write it to a text file for my retrieval later:

curl -s http://www.remotebox.com/myIP.php?ip_data=12.123.124.123

Since I want to make this a cron job that is run hourly I need a way to pass the IP string from the first curl command into a variable which is transmitted to the PHP script.

I am a bit stumped because I don't really know how to use curl like that. How do I get the IP out of that command so I can use it in the rest of my script?

If this piece of jerky ain't too tough for ya feel free to chew it up and spit something back at me. Thanks for the consideration.

Lizard
 
OP
M

masaka___

Guest
lizardthefish said:
curl -s http://www.showmyip.com/simple/ | awk '{print $1}'

curl -s http://www.remotebox.com/myIP.php?ip_data=12.123.124.123

I am a bit stumped because I don't really know how to use curl like that. How do I get the IP out of that command so I can use it in the rest of my script?

Lizard

Watch closely:

curl -s http://www.remotebox.com/myIP.php?ip_data=`curl -s http://www.showmyip.com/simple/ | awk '{print $1}'`

It's all about the back-tick ` located on the top-left corner of your keyboard. Surrounding a shell expression in back-ticks will execute the expression and return its output as a string. It's a very useful shell idiom.

That being said, there are easier ways to find your own IP address. For example, you could run the ifconfig command instead of using curl to ask a website to tell you your own IP address. (Silly Mac people. :eek:)
 
OP
L

lizardthefish

Guest
masaka___ said:
(Silly Mac people. :eek:)

WHOA! The OS'cial slur was uncalled for!

But hey, thanks for the help. I will use the back-tick tip in the future.

I just ran 'ifconfig' and it didn't pull my external IP anywhere it the output. I ran it w/o arguments and the info was all behind my firewall.

I'm using a wireless router that gets dynamic IP from ISP. Is 'ifconfig' still the way to go in your eyes?

Lizard
 
OP
M

masaka___

Guest
I'm just playin' :) :) :) :) :)

Anyway, you bring up a really good point. When you're behind a firewall, ifconfig is NOT the way to go, and using curl to hit that particularly informative URL would be a better alternative.
 

Shop Amazon


Shop for your Apple, Mac, iPhone and other computer products on Amazon.
We are a participant in the Amazon Services LLC Associates Program, an affiliate program designed to provide a means for us to earn fees by linking to Amazon and affiliated sites.
Top