New To Mac-Forums?

Welcome to our community! Join the discussion today by registering your FREE account. If you have any problems with the registration process, please contact us!

Get your questions answered by community gurus Advice and insight from world-class Apple enthusiasts Exclusive access to members-only contests, giveaways and deals

Join today!

 
Start a Discussion
 

Mac-Forums Brief

Subscribe to Mac-Forums Brief to receive special offers from Mac-Forums partners and sponsors

Join the conversation RSS
OS X - Development and Darwin Discussion and questions about development for Mac OS X.

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


Post Reply New Thread Subscribe

 
Thread Tools
lizardthefish
Guest
 
Posts: n/a

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 damn 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
QUOTE Thanks
lizardthefish
Guest
 
Posts: n/a

I was in 'macOSXhints' forum pages and found this. I think it will get me going on the right track. Tell me if I am wrong but with a few modifications here I will be able to hack this around to work nicely for my uses.

http://www.macosxhints.com/article.p...uery=scripting

I will keep posting my findings.

Lizard
QUOTE Thanks
masaka___
Guest
 
Posts: n/a

Quote:
Originally Posted by lizardthefish
curl -s http://www.showmyip.com/simple/ | awk '{print $1}'

curl -s http://www.remotebox.com/myIP.php?ip...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. )
QUOTE Thanks
lizardthefish
Guest
 
Posts: n/a

Quote:
Originally Posted by masaka___
(Silly Mac people. )
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
QUOTE Thanks
masaka___
Guest
 
Posts: n/a

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.
QUOTE Thanks

Post Reply New Thread Subscribe


« creating os9 installer | File type code from the command line »
Thread Tools

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
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 On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off
Forum Jump

All times are GMT -4. The time now is 08:00 PM.

Powered by vBulletin
Copyright ©2000 - 2014, Jelsoft Enterprises Ltd.
X

Welcome to Mac-Forums.com

Create your username to jump into the discussion!

New members like you have made this community the ultimate source for your Mac since 2003!


(4 digit year)

Already a member?