Imports System.Net
Imports System.Management.ManagementClass
Imports System.Management

Public Class red
    Public Shared Function Ping(Servidor As String) As String
        Try
            Dim sRespuesta As String = ""
            Dim eco As New System.Net.NetworkInformation.Ping
            Dim res As System.Net.NetworkInformation.PingReply
            Dim ip As IPAddress

            Dim myIPAddresses() As IPAddress = Dns.GetHostAddresses(Servidor)

            For Each ip In myIPAddresses
                res = eco.Send(ip)
                If res.Status = NetworkInformation.IPStatus.Success Then
                    sRespuesta &= Servidor & ": Respuesta desde " & res.Address.ToString & vbNewLine
                Else
                    sRespuesta &= Servidor & ": Sin Respuesta desde " & res.Address.ToString & vbNewLine
                End If
            Next
            Return sRespuesta
        Catch ex As Exception
            Return ex.StackTrace
        End Try
    End Function

    Public Shared Sub SetIP(nicName As String, IpAddresses As String, SubnetMask As String, Gateway As String, DnsSearchOrder As String)
        Dim mc As New ManagementClass("Win32_NetworkAdapterConfiguration")
        Dim moc As ManagementObjectCollection = mc.GetInstances()

        For Each mo As ManagementObject In moc
            ' Make sure this is a IP enabled device. 
            ' Not something like memory card or VM Ware
            If DirectCast(mo("IPEnabled"), Boolean) Then
                If mo("Caption").Equals(nicName) Then

                    Dim newIP As ManagementBaseObject = mo.GetMethodParameters("EnableStatic")
                    Dim newGate As ManagementBaseObject = mo.GetMethodParameters("SetGateways")
                    Dim newDNS As ManagementBaseObject = mo.GetMethodParameters("SetDNSServerSearchOrder")

                    newGate("DefaultIPGateway") = New String() {Gateway}
                    newGate("GatewayCostMetric") = New Integer() {1}

                    newIP("IPAddress") = IpAddresses.Split(","c)
                    newIP("SubnetMask") = New String() {SubnetMask}

                    newDNS("DNSServerSearchOrder") = DnsSearchOrder.Split(","c)

                    Dim setIP__1 As ManagementBaseObject = mo.InvokeMethod("EnableStatic", newIP, Nothing)
                    Dim setGateways As ManagementBaseObject = mo.InvokeMethod("SetGateways", newGate, Nothing)
                    Dim setDNS As ManagementBaseObject = mo.InvokeMethod("SetDNSServerSearchOrder", newDNS, Nothing)

                    Exit For
                End If
            End If
        Next
    End Sub
End Class
