LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Networking (https://www.linuxquestions.org/questions/linux-networking-3/)
-   -   What's my DHCP assigned IP and where is it? (https://www.linuxquestions.org/questions/linux-networking-3/whats-my-dhcp-assigned-ip-and-where-is-it-141048/)

Buckyjunior 02-01-2004 12:16 PM

What's my DHCP assigned IP and where is it?
 
I would like to set up a cron job to tell me when my ISP changes my IP number.

If my Linux box keeps track of that number somewhere, I could grep it and send myself a message when it changes. All of my computers are behind a router/gateway. The router is controled via internal html pages. I supposed I could write a long script that starts up lynx, loads and saves the page with the IP number and then grep that.

If my Linux box keeps track of that number somewhere, it would be a lot easier.

Any ideas?
Bucky

XavierP 02-01-2004 12:20 PM

ifconfig is the command to find out that sort of thing. It works similarly to Windows ipconfig but is more detailed.

jtshaw 02-01-2004 12:21 PM

It would be easier to just wget the html page. Linux doesn't know the external IP of the router, all it cares about is it's IP on the local network and the local IP of the router. Some routers will talk SNMP and notify you when there IP has changed and what it has changed too.

Buckyjunior 02-01-2004 12:33 PM

Thanks for the prompt hint XavierP, but it doesn't _quite_ give the all the information I need.

What I get is the local loopback address (127.0.0.1) and my internal LAN address (192.168.0.110). I don't get my WAN IP.

Thanks for the try.
Bucky

Buckyjunior 02-01-2004 01:13 PM

jtshaw,
That's it. wget

A few tweaks to send user and password, and I've got access to the IP information I need.

There are _so_ many applications in my Linux box (e.g., wget) that I need to find out about. You knew the one I needed. Yesterday I printed out four pages, four columns each, at 8 pt. of apps in bin and sbin (from a2p . . . zprint). I guess I'll make it a project to do --help on each of them to make notes and see which might be of use to me in the future.

Thank jtshaw. I appreciate your help. If anyone is interested, after I follow through, I'll post my script and cron job. I don't imagine that I'm the only one who would like to keep track of their dynamically changing IP addresses.

Bucky

Quote:

Originally posted by jtshaw
It would be easier to just wget the html page. Linux doesn't know the external IP of the router, all it cares about is it's IP on the local network and the local IP of the router. Some routers will talk SNMP and notify you when there IP has changed and what it has changed too.

fur 02-01-2004 02:02 PM

I just read you post and thought this would be a nice thing to have. This is how I just did it..

First I installed sendEmail. This is a lightweight SMTP mail client .

http://caspian.dotconf.net/menu/Software/SendEmail/

Once it is downloaded unzip and untar the file. Then cd into the resulting directory, and copy 'sendEmail' to /usr/local/bin/ .

Then edit 'sendEmail' and change the "Global Variables" to suit your needs.


I then made this bash script.

Code:


#!/bin/bash
cd /home/user/myip
wget whatismyip.org
cat index.html | sendEmail -f you@domain.com -t destination@domain.com
rm index.html


Than just add it to cron.

Buckyjunior 02-01-2004 03:50 PM

fur,
You're fast! GREAT!

I really like the whatismyip.org. It outputs only one text line to grep. When I get things sorted out (i.e., I learn some more), I'll probably do it just a little different.

1) I'll use mail since it is already in the Linux system.
2) I'll compare the whatismyip file with a previously saved IP file
3) Only send myself a message if my IP has changed. (Not knowing sendEmail, you may already be doing this with the -f and -t options.)
4) If it has changed, and I've sent myself a message, then I copy the whatismyip file to the "previously saved IP" file so that I'm not sending myself multiple messages.

Quote:

Originally posted by fur
. . .
I then made this bash script.

Code:


#!/bin/bash
cd /home/user/myip
wget whatismyip.org
cat index.html | sendEmail -f you@domain.com -t destination@domain.com
rm index.html

. . .

Actually, some years ago, I wrote something like this to check for changes in (generally) static web pages. The idea was that when a scheduling page changed, I could automatically send interested users a "This page has changed." message. I can't find what I wrote now and it was never implimented because the computer admin people wouldn't give a graphics person access to crontab. So it goes.

Thanks fur, for the input. Good stuff.
Bucky

Buckyjunior 02-27-2004 01:29 PM

Getting my DHCP assigned IP address
 
I'm a little slow to follow up, but I think I have what I want. When my ISP changes my DHCP assigned IP address, I will find out about it within 24 hours and can make appropriate reference changes.

I'm guessing that this has all been done before, but as the Perl slogan goes "There's more than one way to do it." I thought that readers here might be in the same situation I am and might appreciate having this little script.

First, I created the subdirectory 'changes' in my home directory, and included the following perl script.
Code:

#!/usr/bin/perl -w
 # script written in perl to see if IP has changed
 # from linuxquestions.org discussion groups
 # written by Buckyjunior 2004/2/26
 
 # set up working directory on Linux box
 chdir "/home/me/changes" or die "cannot chdir to home/me/changes\n: $!";
 
 # uncomment the following line to create the file oldip.txt
 # system touch "oldip.txt";
 
 # get IP address from whatismyip.org as "newip.txt"
 system "wget --quiet --output-document newip.txt whatismyip.org";
 
 # open each file and assign the single line of content to variables
 open (OLD, ' while() {
        chomp($oldip = $_); #get the old file and assign variable
 }
 open (NEW, ' while() {
        chomp($newip = $_); #get the new file and assign variable
 }
 
 if ($oldip ne $newip) {
        # files are different, IP has changed, create newipmsg.txt
        system "cat mailmsg.txt oldip.txt RT.txt newip.txt > newipmsg.txt";
       
        # send mail home with the new IP
        system "mail -n -s 'IP Address Note' -c me\@localhost.localdomain bucky\@anotherISP.com < newipmsg.txt";
 
        # move the new file to become the old file
        system "mv newip.txt oldip.txt";
 
        # remove the "new" file
        system "rm newip.txt";
 }
 
 # no changes, so we don't need to do anything
 # drop out

I saved the file as chkIP. I also wrote a cute text file and named it mailmsg.txt so that I would have something to read in the message in addition to the IP numbers. I couldn't figure out how to insert a carriage return into the cat command, so I created another file RT.txt with a single carriage return.

For the chkIP job, this command and entry made the whole thing work.
Code:

crontab
 37 1 * * *    perl /home/me/changes/chkIP

This was followed by ^D. The crontab entry reads: at 37 minutes into the hour 1, all days of the month, all months of the year, all days of the week, have Perl run the file chkIP in the subdirectory named.

To test it, I made a false IP address for the oldip.txt file and waited overnight for the cron job to run. Works fine. My response looks something like this:

Subject: IP Address Note

Appended below is your old IP address followed by
your new IP address. Please make the appropriate
changes to your system configuration.

cron
123.456.78.90

234.56.789.120


Obviously, if you want to use this code, you'll have to change "me" to whomever you are on your system, make sure you are allowed to run cron jobs, etc.

Enjoy. Be well.
Bucky


All times are GMT -5. The time now is 08:06 AM.