I have two computers with dynamic IP addresses and am using dyndns so that they are identifiable as the same computer even if their IPs change (we'll call them host1.dyndns.com and host2.dyndns.com). I also have a remote server which I would like to store my computers' IP addresses on. There is a file on my remote server which has the IPs that the two hostnames point to. So the file would look like this
Then compare the result (which is an IP address) to the file that I have described above. If the IP is different from the old one then remove that line and replace it with the new IP. I thought I could do this myself but grabbing the old IP and storing it in a variable and then comparing it to the new IP was beyond me. I don't even know if that is how you should do it. sed/awk is preferable but any language will do
Thanks. I need to run the script on the remote server and find the IP addresses of my home computers with dynamic IPs. I'm not sure if that is what the script is doing b/c it looks very complex and I don't really understand everything. In any event, ran the script and I got this error:
Bad arg length for Socket::inet_ntoa, length is 0, should be 4 at -e line 1, <> line 1.
Thanks again and thanks in advance if anyone else takes a crack at this.
That's because your host's file has an empty line. "next unless" will solve that. Keep in mind that the host's file has to have always something like ip #hostname for this to work.
perl -pe 'use Socket; next unless /(\d+.\d+.\d+.\d+)\s#(.*?)\s*$/; $dig=inet_ntoa(inet_aton($2)); s/$1/$dig/ if ($dig ne $1);' FILE > FILE.new && mv FILE.new FILE
If you keep a dummy file whose modification date indicates when the service was restarted, you can figure out whether FILE or the dummy file is newer. If FILE is newer then it was modified after the service was last restarted. But it requires the discipline that you update the timestamp of the dummy file each time you restart the service.
perl -pe 'use Socket; next unless /(\d+.\d+.\d+.\d+)\s#(.*?)\s*$/; $dig=inet_ntoa(inet_aton($2)); if ($dig ne $1) { s/$1/$dig/; $changed=1; } END { system "sleep 60 && /etc/init.d/service restart &" if $changed }' FILE > FILE.new && mv FILE.new FILE
that will execute /etc/init.d/service restart 60 seconds after an entry in FILE has been changed. The 60 seconds are needed and the '&' in the end is most important because this "script" needs to complete moving FILE.new to FILE before /etc/init.d/service can read the updated FILE. Otherwise your service would read the old entry instead of the newly created one.