Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer

Getting an IP address from DHCP

by Snuggle (Friar)
on Jul 30, 2002 at 20:31 UTC ( #186353=perlquestion: print w/replies, xml ) Need Help??

Snuggle has asked for the wisdom of the Perl Monks concerning the following question:

A question for your perusal...
I have a cable modem, and a linux box behind a broadband router. The ISP provides IPs through DHCP, and the IP that my network uses changes from time to time. I like to access my boxen through telnet from work a lot, and the problem is that the IP is always expiring and changing. So here is what I was wondering...

Is there a way for a box behind a firewall to tell what IP address the firewall is using, and more to the point, is there a perl module that anyone has experience using that can divine this info elegantly. My plan is to implement some cron jobbed perl to check the IP address, compare it to a last IP found, and email my office if the IP has changed.

Thanks for the opinions.

Anyway, no drug, not even alcohol, causes the fundamental ills of society. If we're looking for the source of our troubles, we shouldn't test people for drugs, we should test them for stupidity, ignorance, greed and love of power.

--P. J. O'Rourke

Replies are listed 'Best First'.
Re: Getting an IP address from DHCP (DynDNS)
by DamnDirtyApe (Curate) on Jul 30, 2002 at 20:39 UTC
Re: Getting an IP address from DHCP
by Kanji (Parson) on Jul 30, 2002 at 21:09 UTC
    Is there a way for a box behind a firewall to tell what IP address the firewall is using

    As DamnDirtyApe notes, a static hostname is a good way of solving this particular problem, but an alternative is something like...

    use LWP::Simple; if (get('') =~ /(\d+[.]\d+[.]\d+[.]\d+)/) { print $1; }

    Another solution (if you have control over the firewall), is to install SNMP and then query the firewall remotely using Net::SNMP (alt.) or Net::SNMP::Interfaces (alt.).


    Update: Fixed snippet.

Re: Getting an IP address from DHCP
by BrowserUk (Patriarch) on Jul 30, 2002 at 22:29 UTC

    DynDNS provide a really useful service as DirtyDamnApe suggests above, however, the service is predicated on some machine inside your home network being able to determine the external IP when it changes. If your router/firewall is a programable machine this is usually not a problem as they have a selection of user written (quality and facilities vary) update agents available for download from their website.

    What can be a problem is finding a way to determine the external IP from stand-alone firewall boxes like SonicWall etc.

    If your lucky, the box will have a html status screen--available from its internalip:port 80 (or 8080)--that will list the current external IP. Writing your own agent to extract this and update the DynDNS profile is reasonably simple to do.

    You may find, (as I did) that you need to download the latest firmware for your firewall box in order to get the latest version of the status screen that has the external IP displayed.

    Good luck.

Re: Getting an IP address from DHCP
by dws (Chancellor) on Jul 30, 2002 at 22:32 UTC
    Is there a way for a box behind a firewall to tell what IP address the firewall is using, and more to the point, is there a perl module that anyone has experience using that can divine this info elegantly.

    I have a similar setup. The way I deal with it is described in this post. Basically, it involves pinging a CGI on my external website periodically, and remembering the remote IP address (i.e., the one assigned to my router by my DSL provider).

Re: Getting an IP address from DHCP
by tachyon (Chancellor) on Jul 31, 2002 at 00:53 UTC

    Hmm is tracert too simple? The second IP will probably be your external ip which will be easy enough to parse out and do what you want with:

    C:\Documents and Settings\administrator>tracert Tracing route to [] over a maximum of 30 hops: 1 <10 ms <10 ms <10 ms SpeedTouch.lan [] 2 20 ms 10 ms 21 ms [217.32.6 8.201] 3 20 ms 10 ms 20 ms 4 10 ms 30 ms 20 ms 5 10 ms 10 ms 20 ms [62.7.250 +.129] 6 21 ms 10 ms 20 ms 7 10 ms 20 ms 30 ms ^C C:\Documents and Settings\administrator> # so something simple like.... $tracert = `tracert`; @ips = $tracert =~ /(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})/g; print "$_\n" for @ips; print "External IP gateway is $ips[1]\n"; # or whatever index is corre +ct




      Traceroute shouldn't work. The incoming interface should decrement the TTL, see that it became 0 and report back to the source. The external interface doesn't even see the packet. So, the first answer should be from the internal interface of your router. The second answer should then be from the router one hop away, ie. the ISP equipment. If it does work, the broadband router does strange things. However, some equipment might report back it's "main" IPaddress instead of said interface address, but don't count on it.

      And anyway, you should use the -m (maximum ttl) flag, or it's win32 equivalent, to not let the probes wander of to far.

Re: Getting an IP address from DHCP
by fsn (Friar) on Jul 30, 2002 at 21:49 UTC
    Hmm, you are not so precise in describing neither your setup or your problem. You don't give us enough data for us to help you with a precise answer, so I'll give you a broad one.

    As far as I understand your setup, the broadband router and the firewall is the same box, and this box is also the one that is changing it's outer IPaddress through DHCP all the time. Your problem is that you do not know how to see the IPadress on the router/firewall's outer interface from the Linux box. Am I correct so far? Also, the router/fw is essentially a black box that you can't install software on, at least not Linux software, correct?

    So, you need to access to router/firewall in some way to get that IPadress. I would guess you can either telnet into it or surf to it, or perhaps even use SNMP. SNMP is, at least for me, hard to work with, you probably need the MIB and stuff, so let's not bother. If you can surf to it, use LWP to fetch the data you need. If you can telnet to it, use Expect and catch the data.

    When you have the data you need, use any technique for distributing that data to you, either through a mail, or a call using LWP to dyndns et al. See also freshmeat, I think there are some dyndns update utilities already written.

Log In?

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://186353]
Approved by Kanji
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others studying the Monastery: (4)
As of 2023-02-01 19:31 GMT
Find Nodes?
    Voting Booth?
    I prefer not to run the latest version of Perl because:

    Results (12 votes). Check out past polls.