How to get router's IP Address programmatically?
MikeMike4
Enthusiast - Level 1

While on my computer, behind the router Verizon router, I need to know my the router's IP address, so I can ssh into my box from remote hosts.

When I subscribed to cable, and had a Linksys router as my main gateway at 192.168.1.1, I could log into it programmatically (using Perl LWP) and parse out the WAN IP address.

But with the ridiculous Verizon setup, their login screen is jam-packed with silly Javascript that changes the number of characters on the screen as you type, etc (all of which have been covered in these forums), and I cannot figure out what the heck it is doing.  Logging into the router manually works fine.  But I need to be able to do so programmatically.

(Or is there some other way to obtain my router's WAN IP address from my local system?)

Any suggestions?

Thanks,

-Mike

0 Likes
1 Solution

Correct answers
Re: How to get router's IP Address programmatically?
pamcphilly
Enthusiast - Level 3

Couldn't you just setup a dynamic dns name from dyndns.org (or one of the other providers) and set that up in the router's dynamic dns setup.  That way you could always use a FQDN instead of an ip address.  The router will also keey dyndns.org updated with you curring WAN ipaddress.

If you want to do something progamatically you could access a website like http://www.whatsmyip.org/ and parse its response.

View solution in original post

Re: How to get router's IP Address programmatically?
pamcphilly
Enthusiast - Level 3

Couldn't you just setup a dynamic dns name from dyndns.org (or one of the other providers) and set that up in the router's dynamic dns setup.  That way you could always use a FQDN instead of an ip address.  The router will also keey dyndns.org updated with you curring WAN ipaddress.

If you want to do something progamatically you could access a website like http://www.whatsmyip.org/ and parse its response.

Re: How to get router's IP Address programmatically?
MikeMike4
Enthusiast - Level 1

HA!  Such simple solutions, thanks.

I've been so focused (with tunnelvision) on the way I'd been doing it before that I never considered these possibilities.  Thanks for the kick in the butt.  I needed it.

-Mike

0 Likes
Re: How to get router's IP Address programmatically?
Anti-Phish1
Master - Level 1

@MikeMike wrote:

When I subscribed to cable, and had a Linksys router as my main gateway at 192.168.1.1, I could log into it programmatically (using Perl LWP) and parse out the WAN IP address. 


You can also telnet into the router and use the command line interface.

Telnet 192.168.2.1

User: admin

Password: *******

Wireless Broadband Router> rg_conf_print /dev/ixp0/dyn
(dyn
  (ip(192.168.1.4))
)

Returned 0
Wireless Broadband Router>

* 192.168.2.1 is the LAN address of my test router

   192.168.1.4 is the WAN address of my test router.

Re: How to get router's IP Address programmatically?
lasagna
Community Leader
Community Leader

The following will also work on a Windows box:

nslookup -type=a myip.opendns.com resolver1.opendns.com

Re: How to get router's IP Address programmatically?
FSC2
Contributor - Level 1

login to the router it tells you the ip addresses connected to the device.

0 Likes
Re: How to get router's IP Address programmatically?
Anti-Phish1
Master - Level 1

@FSC wrote:

login to the router it tells you the ip addresses connected to the device.


Did you read the OP's post?

He wants to determine the WAN IP address programmatically.

0 Likes
Re: How to get router's IP Address programmatically?
FSC2
Contributor - Level 1

Dint someone answer the question to that...above...

I thought I wanted to give some extra info.

In Java:

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package ip;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URL;

/**
 *
 * @author tntadmin
 */
public class Main {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
         BufferedReader buffer = null;
        try {
                URL url = new URL("http://whatismyip.com/automation/n09230945.asp");
                InputStreamReader in = new InputStreamReader(url.openStream());
                buffer = new BufferedReader(in);

                String line = buffer.readLine();
                System.out.println(line);
        } catch (IOException e) {
                e.printStackTrace();
        } finally {
                try {
                        if (buffer != null) {
                                buffer.close();
                        }
                } catch (IOException e) {
                        e.printStackTrace();
                }
        }
    }

}
Re: How to get router's IP Address programmatically?
lucastheisen
Newbie

Thats great information, but on my router I had to enable telnet first through: Advanced->Local Administration

Given that its telnet, I had to pair it with Expect, and since the original post asks about perl, here is what I did:

#!/usr/bin/perl

use strict;
use warnings;

use Expect;

my $username = '';
my $password = '';
my $command = 'conf print /dev/eth1/dyn/ip/';

my $exp = Expect->new();
$exp->raw_pty( 1 );
$exp->log_stdout( 0 );
$exp->spawn( 'telnet 192.168.1.1' );
$exp->expect( 10, 'Username' );
$exp->send( "$username\n" );
$exp->expect( 10, 'Password' );
$exp->send( "$password\n" );
$exp->expect( 10, 'Wireless Broadband Router>' );
$exp->send( "$command\n" );
my ($matchedPatternPosition, $error, $successfullyMatchingString, $beforeMatch, $afterMatch) = $exp->expect( 10, '-re', '\(ip\(.*?\)\)' );
$exp->expect( 10, 'Wireless Broadband Router>' );
$exp->send( "exit\n" );
$exp->hard_close();

my $ip;
if ( $successfullyMatchingString =~ /\(ip\((.*?)\)\)/ ) {
    $ip = $1;
}

print( "ip is $ip\n" );
print( "DONE\n" );
0 Likes
Re: How to get router's IP Address programmatically?
viafax999
Community Leader
Community Leader

@MikeMike wrote:

HA!  Such simple solutions, thanks.

I've been so focused (with tunnelvision) on the way I'd been doing it before that I never considered these possibilities.  Thanks for the kick in the butt.  I needed it.

-Mike


Be aware that dyndns now requires that users with free accounts need to logon to the account every 30 days else the account will be deleted.

Not that onerous but a PIA

I just set up a schedules task to kick up the logon screen every couple of weeks.

0 Likes