Grabbing variables and comparing

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

111.111.111.111 #host1.dyndns.com
222.222.222.222 #host2.dyndns.com

I need a script that will run this command which gets the IPs of my computers:

/usr/bin/dig +short host1.dyndns.com | /usr/bin/tail -n 1
/usr/bin/dig +short host2.dyndns.com | /usr/bin/tail -n 1

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 :slight_smile:

Thanks in advance.

perl -pe 'use Socket; /(\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

FILE is where you have the old addresses. You must change that in order for this to work.

redoubtable,

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
Tsunami comparing # cat ips 
206.190.60.37 #altavista.com
64.233.167.99 #google.com
Tsunami comparing # 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);' ips
206.190.60.37 #altavista.com
64.233.187.99 #google.com
Tsunami comparing # 

An approach with awk:

awk -F"#" 'BEGIN{
"/usr/bin/dig +short host1.dyndns.com | /usr/bin/tail -n 1" |getline ip["host1.dyndns.com"]
"/usr/bin/dig +short host2.dyndns.com | /usr/bin/tail -n 1" |getline ip["host2.dyndns.com"]}
$2 in ip{$0=ip[$2]" #"$2}
{print}' file > new_file

Regards

redoubtable, you are the man. It works perfectly :slight_smile:

Franklin52. Thank you as well! I appreciate all the help I've received on this forum :slight_smile:

I forgot to ask about this in my initial post and I apologize for that.

I need the following command to be executed ONLY if changes have been made to "FILE"

/etc/init.d/service restart

If no changes have been made then the above command should not be executed.

Thanks!!!!

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.