![]() |
[HELP] Creating a script to record IP of a website
Hello all, this is my first post here at LinuxQuestions and to start it off I have a question that may be simple. I will provide as much information that I can to help explain what it is I am trying to accomplish.
Information: My server is running CentOS latest version I am booting to GRSEC My problem is that my phone's IP address changes every 10-30 minutes, and my server is IP whitelisted. So in order for me to login to my server I need to somehow record my Phones IP address into the "/etc/hosts.allow file". Getting the IP of my phone is simple, for this I set up an account at dyndns.org. My phone successfully updates it's ip to the site whenever it changes. For this example I will use "linuxquestions.dyndns.org" In order to get my phones IP I will run a "nslookup" on linuxquestions.dyndns.org every 10 minutes. For this I will set a cron job to run every 10 minutes. Code:
0,10,30,45 * * * * /scripts/dyndnslinuxquestionsMy nslookup command looks as follows: Code:
root@LinuxQuestions [/]# nslookup linuxquestions.dyndns.orgSo lets say my hosts.allow file looks like such: Code:
# hosts.allow This file describes the names of the hosts which areIf someone could step me through the process and explain to me what each part of the script does instead of just handing it to me that would be awesome. I am here to learn not just copy and paste. Any help would be greatly appreciated! -Reneg4d3 |
Code:
#!/bin/bashI might also consider using iptables instead of hosts.allow, but I suspect that's just personal preference more than any real security advantage. Other opinions might differ. |
Were you looking for something like this:
Code:
#!/bin/sh |
If you (your script) knows the old IP address then this one liner should work
Code:
sed -i "s/${old_IP//./\\.}/$(dig +short linuxquestions.org)/" hostsTest without the -i option! Here it is demonstrated at the command line Code:
c@CW8:/tmp$ cat hosts |
Quote:
Quote:
Just from reading a little bit about sed and looking at your examples I came up with this: Code:
NewIP=$(nslookup linuxquestions.dyndns.org | grep "dyndns.org" -A 1 | grep "Address:" | sed -e "s/[^0-9]*\([0-9.]*\).*/\1/")Also, I see your looking up "User 1" IP. I believe I would need to change that to "My Phone" Over all this is pretty much EXACTLY what I was wanting and I learned quite a bit. I may extend this script to be more active and do more but for the time being I believe this will work. Thanks! Quote:
-- I think my overall script would be something like: Code:
#!/bin/shCode:
root@LinuxQuestions [/]# sh /scripts/android_nslookupCode:
# My Phone |
You might also like to think about checking to see if the IP has changed before you actually do the substitution.
|
Quote:
Code:
c@CW8:~$ dig +short linuxquestions.orgsed you know about. Its substitute command (s/<regular expression>/<replacement string>/) searches for a regular expression and replaces it with <replacement string>. In a regular expression, "." means any single character. Thus if you use an IP address as a regular expression, 192.168.1.1 will match 192.168.1.1 but it will also match 192a168Bm1z1. The special meaning of "." in a regular expression may be removed by "escaping" it with a backslash "\" thus regular expression 192\.168\.1\.1 will match only 192.168.1.1. sed's -i option makes sed change a file "in place" -- it modifies the input file. It is a very useful technique to try commands and parts of commands at the command line. You could have tried Code:
dig +short linuxquestions.org |
Quote:
|
Using safer code is recommended. Also, if you write to file, you are changing it's time stamp even when there is no change.
This code could and probably will be used by others also, maybe for larger number of IP's, so I would add write suppression just in case. Also, I would ADD BACKUP of hosts.allow, maybe even with time stamp in name (it does not have to be in /etc folder) so if you brake the file your system is not rendered totally useless while you re-create it by hand. If you create backup file, but without a stamp in filename, next run will overwrite the backup. Adding smart code to view (less) backuped files from script, and option to revert to last good one would be my choice. Write once, saves you until the end of time. :-) |
Quote:
the principle applies regardless of the application, why do more work than is necessary and risk something going wrong? if you test first you can avoid the possibility of replacing the value with a dud one. What if, for instance, the lookup fails and gives an unexpected result? |
Quote:
|
All learnt from bitter experience! :)
|
| All times are GMT -5. The time now is 10:40 AM. |