SID-500

Home » PowerShell » PowerShell: Testing the connectivity to the Default Gateway on localhost and Remote Hosts by reading the Routing Table

PowerShell: Testing the connectivity to the Default Gateway on localhost and Remote Hosts by reading the Routing Table

How to test the connection to the default gateway without knowing the IP-Address of that Gateway?

Introduction

Imagine you are logged on a Windows Computer Server01. You want to check if Server02 has a connection to it’s gateway. Sure, you could run Test-Connection with the Source Parameter and do a remote ping. But you don’t know the IP-Address of server02’s gateway. There are 2 possibilities: a) Connect to Server02 and run ipconfig to find out Server02’s gateway address or  b) Keep on reading my article  🙂

For checking the reachability of the localhost’s gateway, run the following One-Liner:

Test-Connection (Get-NetRoute -DestinationPrefix 0.0.0.0/0 | Select-Object -ExpandProperty Nexthop) -Quiet -Count 1

Unbenannt.PNG

True, everything is fine.

Explanation: The IP-Address of the Default Gateway can be found in the routing table. The route 0.0.0.0/0 is called Default Route or Gateway of Last Resort. And that route with it´s next hop address is very important for the next step.

Unbenannt.PNG

Testing the Gateway of Remote Hosts without knowing the IP-Address of the Gateway

One further thought: Why not use the -Source Parameter and combine it with our One-Liner? So I started playing … Is it possible to test the connection to server03’s gateway without knowing the IP-Address of server03’s gateway? Sounds crazy, but: Yes, it is.

I am logged on server dc01 (dc01 and server03 share the same domain) and do the following:

$Name=Read-Host "Computername?"
$Test=Invoke-Command -ComputerName $Name {Get-NetRoute -DestinationPrefix 0.0.0.0/0 | Select-Object -ExpandProperty Nexthop}
Test-Connection -Source $Name -Destination $Test -Count 1

unbenannt20.png

Nice. The IP-Address of server03’s gateway is 192.168.0.1. And yes, server 03 can reach it’s gateway. I don’t care about the IP-Address. 😉 The default route always gives me the IP-Address of the Default Gateway, regardless of the number of network cards and configured IP-Addresses.

For more about Test-Connection see my article The modern version of ping: Test-Connection.

Have fun playing with Test-Connection and Default Routes!


6 Comments

  1. Andreas says:

    Nice idea, but I don’t understand, why you need to include the source parameter for invoking the cmdlets on Server03. You’re already executing it on Server03, aren’t you?

    Like

    • pewa2303 says:

      Thank you for your kind words. I was logged on server dc01. See “I am logged on server dc01 (dc01 and server03 share the same domain) and do the following: ……..” Regards, P

      Like

      • Andreas says:

        OK, now I understand.
        I thought you would execute “Test-Connection” via Invoke-Command as well. You would even save a few keystrokes:

        $Name=Read-Host “Computername?”
        Invoke-Command -ComputerName $Name {$Test=Get-NetRoute -DestinationPrefix 0.0.0.0/0 | Select-Object -ExpandProperty Nexthop; Test-Connection -Destination $Test -Count 1}

        Liked by 1 person

      • pewa2303 says:

        Sometimes I decide to make it more user-friendly and readable. Thank’s for your input!

        Like

  2. […] Or find out the IP-Address of the default gateway by reading the routing table: PowerShell: Testing the connectivity to the Default Gateway on localhost and Remote Hosts by reading… […]

    Like

  3. […] PowerShell: Testing the connectivity to the Default Gateway on localhost and Remote Hosts by reading… […]

    Like

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Categories

WHOIS

My name is Patrick Grünauer (pewa2303). I am from Austria. On sid-500 I write about Windows, Cisco and IT-Security in English and German. Have fun while reading!

Patrick Gruenauer
Follow SID-500 on WordPress.com