Results 1 to 8 of 8

Thread: [RESOLVED] Get mac address of gateway

  1. #1

    Thread Starter
    Fanatic Member Crash893's Avatar
    Join Date
    Dec 2005
    Posts
    930

    Resolved [RESOLVED] Get mac address of gateway

    I'm trying to figure out how to view the default gatways ip address and then get the mac address from that

    Any ideas on how to see what the computers defualt gateway is?

  2. #2
    PowerPoster 2.0 Negative0's Avatar
    Join Date
    Jun 2000
    Location
    Southeastern MI
    Posts
    4,367

    Re: Get mac address of gateway

    The gateway is defined on a per network card level, so this code will get you the Gateway for each of the NICs installed on a machine:

    Code:
            Dim sb As New StringBuilder
            sb.AppendLine()
            For Each n As NetworkInterface In NetworkInterface.GetAllNetworkInterfaces()
                sb.AppendLine(n.Description)
                sb.AppendLine("MAC:" & n.GetPhysicalAddress().ToString())
                For Each u As UnicastIPAddressInformation In n.GetIPProperties().UnicastAddresses
                    sb.AppendLine("IP:" & u.Address.ToString())
                Next
                For Each g As GatewayIPAddressInformation In n.GetIPProperties().GatewayAddresses
                    sb.AppendLine("Gateway:" & g.Address.ToString())
                Next
    
            Next
    
    
            MessageBox.Show(sb.ToString())

  3. #3
    Fanatic Member
    Join Date
    Mar 2008
    Posts
    519

    Re: Get mac address of gateway

    Well the default gateway varies for every user.
    For example, my default gateway is 192.168.1.1 (that's the router default gateway).

    Take a look at this example to get the default gateway:
    http://www.java2s.com/Tutorial/CShar...figuration.htm
    It's in C# but mentioned and VB.Net are very similar so there shouldn't be any problems porting the code.

    You might also want to take a look at this VB.Net example, just to get some ideas (since this code requires user defined information)
    http://www.xtremevbtalk.com/archive/.../t-251709.html

    Also this VB6 example:
    http://bytes.com/topic/visual-basic-...efault-gateway

    Happy coding

    *EDIT* Negative0: damn, you won this time... it took too long time for me to paste the links xD

  4. #4
    Powered By Medtronic dbasnett's Avatar
    Join Date
    Dec 2007
    Location
    Jefferson City, MO
    Posts
    9,897

    Re: Get mac address of gateway

    Quote Originally Posted by Negative0 View Post
    The gateway is defined on a per network card level, so this code will get you the Gateway for each of the NICs installed on a machine:

    Code:
            Dim sb As New StringBuilder
            sb.AppendLine()
            For Each n As NetworkInterface In NetworkInterface.GetAllNetworkInterfaces()
                sb.AppendLine(n.Description)
                sb.AppendLine("MAC:" & n.GetPhysicalAddress().ToString())
                For Each u As UnicastIPAddressInformation In n.GetIPProperties().UnicastAddresses
                    sb.AppendLine("IP:" & u.Address.ToString())
                Next
                For Each g As GatewayIPAddressInformation In n.GetIPProperties().GatewayAddresses
                    sb.AppendLine("Gateway:" & g.Address.ToString())
                Next
    
            Next
    
    
            MessageBox.Show(sb.ToString())
    That code looks familiar.
    My First Computer -- Documentation Link (RT?M) -- Using the Debugger -- Prime Number Sieve
    Counting Bits -- Subnet Calculator -- UI Guidelines -- >> SerialPort Answer <<

    "Those who use Application.DoEvents have no idea what it does and those who know what it does never use it." John Wein

  5. #5
    PowerPoster 2.0 Negative0's Avatar
    Join Date
    Jun 2000
    Location
    Southeastern MI
    Posts
    4,367

    Re: Get mac address of gateway

    Quote Originally Posted by dbasnett View Post
    That code looks familiar.
    Really? I wrote it from scratch. I googled to find out the classes to use, but the code is mine.

  6. #6
    Powered By Medtronic dbasnett's Avatar
    Join Date
    Dec 2007
    Location
    Jefferson City, MO
    Posts
    9,897

    Re: Get mac address of gateway

    Quote Originally Posted by Negative0 View Post
    Really? I wrote it from scratch. I googled to find out the classes to use, but the code is mine.
    I didn't mean anything, sorry.
    My First Computer -- Documentation Link (RT?M) -- Using the Debugger -- Prime Number Sieve
    Counting Bits -- Subnet Calculator -- UI Guidelines -- >> SerialPort Answer <<

    "Those who use Application.DoEvents have no idea what it does and those who know what it does never use it." John Wein

  7. #7

    Thread Starter
    Fanatic Member Crash893's Avatar
    Join Date
    Dec 2005
    Posts
    930

    Re: Get mac address of gateway

    Quote Originally Posted by Zeelia View Post
    Well the default gateway varies for every user.
    For example, my default gateway is 192.168.1.1 (that's the router default gateway).

    Take a look at this example to get the default gateway:
    http://www.java2s.com/Tutorial/CShar...figuration.htm
    It's in C# but mentioned and VB.Net are very similar so there shouldn't be any problems porting the code.

    You might also want to take a look at this VB.Net example, just to get some ideas (since this code requires user defined information)
    http://www.xtremevbtalk.com/archive/.../t-251709.html

    Also this VB6 example:
    http://bytes.com/topic/visual-basic-...efault-gateway

    Happy coding

    *EDIT* Negative0: damn, you won this time... it took too long time for me to paste the links xD
    I am working in c# so i was going to converet it anyway. There are alot more people in the vb rooms so its usually faster with more information if i just post here and then convert it over


    StringBuilder sb = new StringBuilder();
    sb.AppendLine();
    foreach (NetworkInterface n in NetworkInterface.GetAllNetworkInterfaces())
    {
    sb.AppendLine(n.Description);
    sb.AppendLine("MAC:" + n.GetPhysicalAddress().ToString());
    foreach (UnicastIPAddressInformation u in n.GetIPProperties().UnicastAddresses)
    {
    sb.AppendLine("IP:" + u.Address.ToString());
    }
    foreach (GatewayIPAddressInformation g in n.GetIPProperties().GatewayAddresses)
    {
    sb.AppendLine("Gateway:" + g.Address.ToString());
    }

    }


    MessageBox.Show(sb.ToString());

  8. #8

    Thread Starter
    Fanatic Member Crash893's Avatar
    Join Date
    Dec 2005
    Posts
    930

    Re: Get mac address of gateway

    Quote Originally Posted by Negative0 View Post
    The gateway is defined on a per network card level, so this code will get you the Gateway for each of the NICs installed on a machine:

    Code:
            Dim sb As New StringBuilder
            sb.AppendLine()
            For Each n As NetworkInterface In NetworkInterface.GetAllNetworkInterfaces()
                sb.AppendLine(n.Description)
                sb.AppendLine("MAC:" & n.GetPhysicalAddress().ToString())
                For Each u As UnicastIPAddressInformation In n.GetIPProperties().UnicastAddresses
                    sb.AppendLine("IP:" & u.Address.ToString())
                Next
                For Each g As GatewayIPAddressInformation In n.GetIPProperties().GatewayAddresses
                    sb.AppendLine("Gateway:" & g.Address.ToString())
                Next
    
            Next
    
    
            MessageBox.Show(sb.ToString())

    thanks this worked perfectly
    (see above comment i converted it to c#)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width