Finding default gateway in an openvpn environment in windows

1

I need to find the default gateway in a openvpn scenario where the route output looks like that:

IPv4 Route Table
===========================================================================
Active Routes:
Network Destination        Netmask          Gateway       Interface  Metric
          0.0.0.0          0.0.0.0       10.49.73.1      10.49.73.24     10
          0.0.0.0        128.0.0.0         10.8.0.1         10.8.0.2     30

So I googled around a bit and a found this script here:

@For /f "tokens=3" %%* in (
   'route.exe print ^|findstr "\<0.0.0.0\>"'
   ) Do @Set "DefaultGateway=%%*"

echo %DefaultGateway%

This works, but matches both lines in the route output.

But I need to find this line:

0.0.0.0          0.0.0.0       10.49.73.1      10.49.73.24     10

So I tried to modify the findstr parameter like this:

findstr "\<0.0.0.0\>.\<0.0.0.0\>"

in the expectation that '.' will match for the tab between the columns. But it doesn't. It will still set DefaultGateway to 10.8.0.1

I couldn't find a clue in MS documentation either. Maybe someone knows the right expression? Thanks a lot.

user118305

Posted 2012-10-05T08:55:28.033

Reputation:

Answers

0

Try this:

For /f "tokens=3" %%* in (
   'route print ^|findstr "\<0.0.0.0.*0.0.0.0\>"'
   ) Do @Set "DefaultGateway=%%*"

echo %DefaultGateway%

And you can check findstr /? for more informations.

m4573r

Posted 2012-10-05T08:55:28.033

Reputation: 5 051

Ah thats perfect :-) But I do not understand why. When I wrote "<0.0.0.0>.<0.0.0.0>" did it make findstr stop looking after the first '>' ? – None – 2012-10-05T09:16:10.270

First of all the . won't do alone, cause aparently it's not just a single tab in the middle. Then to be honest, I'm not really sure how the \< and \> work, it's not really part of the standard implementation of regexs as I know it... It's supposedly "beginning/end of word". So I just played around with it until it worked ;P – m4573r – 2012-10-05T09:19:09.703

Quite likely the \< and \> forms are required simply to escape the DOS redirection operators. – Karan – 2012-10-07T17:24:41.747