Results 1 to 5 of 5
  1. #1
    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:



    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:


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


    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:


    curl -s`curl -s | awk '{print $1}'`

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


    # /etc/crontab
    #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

    hope this helps someone.



    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 | awk '{print $1}'

    It simply spits out the IP address, like so:

    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

    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.


  2. #2
    I get the funny feeling I'm about to answer my own question here.
    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.

    I will keep posting my findings.


  3. #3
    Quote Originally Posted by lizardthefish
    curl -s | awk '{print $1}'

    curl -s

    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?

    Watch closely:

    curl -s`curl -s | 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. )

  4. #4
    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?


  5. #5
    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.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Post your favourite song via URL
    By Sur3Mac in forum Schweb's Lounge
    Replies: 39
    Last Post: 02-06-2010, 04:18 PM
  2. How to run X from cron job
    By cborchert in forum macOS - Operating System
    Replies: 0
    Last Post: 02-04-2009, 01:38 PM
  3. CRON with an Apple Script
    By riganta in forum macOS - Operating System
    Replies: 1
    Last Post: 07-07-2008, 11:54 AM
  4. cron job question
    By nick.thorley in forum macOS - Operating System
    Replies: 1
    Last Post: 06-22-2007, 02:30 AM
  5. cron jobs
    By unplugged in forum Running Windows (or anything else) on your Mac
    Replies: 4
    Last Post: 07-24-2006, 06:52 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts