2015-06-04 90 views
0

我在寫一個腳本來設置靜態IP給計算機。它讀取一個具有mac addr - ip addr對的文件。基於計算機的mac地址,它從文件中獲取其ip地址。我有問題設置。我從來沒有做過任何一種.net編程。我寫了一個適用於linux的bash腳本,但對於windows我沒有任何經驗。我在vb.net寫了這個程序。到目前爲止,程序可以從文件中獲取數據,現在我必須根據MAC地址和主機名來設置靜態IP。有幾個不同的職位1,2,但他們都在C#中,並有問題轉換爲VB.Net。如果有人能夠提供關於如何爲本地計算機上的特定NIC設置靜態IP地址的指針,那將會很棒。設置靜態IP地址VB.net

Imports System 
Imports System.Text.RegularExpressions 
Imports System.Net.NetworkInformation 
Imports System.IO 
Imports System.Management 


Module Module1 

Const FAILURE = 1 
Const SUCCESS = 0 
Dim phyAddr As String = getMAC() 


Sub Main() 

    Dim arguments(3) As String 
    Dim fileName As String = "" 


    If Environment.GetCommandLineArgs.Count = 3 Then 

     arguments = Environment.GetCommandLineArgs 
     fileName = arguments(2) 

    Else 

     Console.WriteLine("Wrong Syntax!") 
     help() 
     Console.Read() 
     close(FAILURE) 

    End If 

    If validName(fileName) Then 

     If fileExists(fileName) Then 

      'search file for ip 
      Dim confData As String = searchFile(phyAddr, fileName) 

      If Not String.IsNullOrEmpty(confData) Then 

       Dim netConf() As String = splitLine(confData) 

       Dim hostName As String = netConf(1) 
       Dim ipAddr As String = netConf(2) 
       Dim netMask As String = netConf(3) 
       Dim gateway As String = netConf(4) 
       Dim dns1 As String = netConf(5) 
       Dim dns2 As String = netConf(6) 




      Else 
       Console.WriteLine("Couldn't find MAC {0} in file {1}", phyAddr, fileName) 
       Console.Read() 
       close(FAILURE) 
      End If 


     Else 

      Console.WriteLine("File {0} doesn't exist", fileName) 
      Console.WriteLine("Please provide an absolute path to file") 
      Console.Read() 
      close(FAILURE) 
     End If 

    Else 
     Console.WriteLine("File name {0} not recognized", fileName) 
     Console.Read() 
     close(FAILURE) 
    End If 


End Sub 

Private Sub help() 
    Console.WriteLine("Please call program as: ") 
    Console.WriteLine("networkconfiguration -f datafile") 
End Sub 

Private Sub close(exitCode As Integer) 
    Environment.Exit(exitCode) 
End Sub 

Private Function validName(name As String) As Boolean 
    Static fileNameExpression As New Regex("^[\\:_a-zA-Z0-9.]+") 
    Return fileNameExpression.IsMatch(name) 
End Function 

Private Function fileExists(name As String) As Boolean 
    Return My.Computer.FileSystem.FileExists(name) 

End Function 

Private Function getMAC() As String 

    Dim nic As NetworkInterface 
    Dim result As String = String.Empty 

    For Each nic In NetworkInterface.GetAllNetworkInterfaces() 
     If nic.Name.Contains("Ethernet0") Then 
      result = nic.GetPhysicalAddress.ToString 
      Exit For 
     End If 
    Next 

    Return result 
End Function 


Private Function searchFile(keyword As String, fileName As String) As String 

    'store result 
    Dim result As String = String.Empty 

    'search for keyword in returned data 
    Using reader As New StreamReader(fileName) 
     While Not reader.EndOfStream 
      Dim line As String = reader.ReadLine 
      If line.Contains(keyword) Then 
       result = line 
       Exit While 
      End If 
     End While 
    End Using 

    Return result 

End Function 

Private Function splitLine(line As String) As String() 
    Dim separator As Char = ";" 
    Return line.Split(separator) 
End Function 


Private Function setupNetwork(ipAddr As String, netmask As String, gateway As String, dns1 As String, dns2 As String) As Boolean 

    Dim mc As New ManagementClass("Win32_NetworkAdapterConfiguration") 
    Dim moc As New ManagementObjectCollection 
    Dim mo As ManagementObject 

    moc = mc.GetInstances() 
    For Each mo In moc 
     'make sure this is ipenabled device 
     'not something like memory card or VMWare 

    Next 






End Function 

End Module 

回答

0

好的解決了它。我會在這裏發佈我的答案,這樣其他人可能會受益。

' set the network configuration of a computer 
Function setupNetwork(phyAddr As String, ipAddr As String, netmask As String, gateway As String, dns1 As String, dns2 As String) As Boolean 
    Dim result As Boolean = False 

    ' concatenate two dns addresses into one 
    Dim dnsSearchOrder As String = dns1 + "," + dns2 

    Dim objMC As ManagementClass = New ManagementClass("Win32_NetworkAdapterConfiguration") 
    Dim objMOC As ManagementObjectCollection = objMC.GetInstances() 

    For Each objMO As ManagementObject In objMOC 

     If (CBool(objMO("IPEnabled"))) Then 

      ' remove colons from mac address so that it could match the 
      ' provided mac address 
      Dim origMAC As String = objMO("MacAddress").ToString() 
      Dim pattern As String = ":" 
      Dim replacement As String = "" 
      Dim rgx As New Regex(pattern) 
      ' the mac address with colons removed from it 
      Dim repMAC As String = rgx.Replace(origMAC, replacement) 

      If (String.Equals(phyAddr, repMAC)) Then 
       Try 
        Dim objNewIP As ManagementBaseObject = Nothing 
        Dim objNewGate As ManagementBaseObject = Nothing 
        Dim objNewDNS As ManagementBaseObject = Nothing 
        Dim objSetIP As ManagementBaseObject = Nothing 

        objNewIP = objMO.GetMethodParameters("EnableStatic") 
        objNewGate = objMO.GetMethodParameters("SetGateways") 
        objNewDNS = objMO.GetMethodParameters("SetDNSServerSearchOrder") 

        'set defaultgateway 
        objNewGate("DefaultIPGateway") = New String() {gateway} 
        objNewGate("GatewayCostMetric") = New Integer() {1} 

        'set ipaddress and subnetmask 
        objNewIP("IPAddress") = New String() {ipAddr} 
        objNewIP("SubnetMask") = New String() {netmask} 
        objNewDNS("DNSServerSearchOrder") = dnsSearchOrder.Split(",") 

        objSetIP = objMO.InvokeMethod("EnableStatic", objNewIP, Nothing) 
        objSetIP = objMO.InvokeMethod("SetGateways", objNewGate, Nothing) 
        objSetIP = objMO.InvokeMethod("SetDNSServerSearchOrder", objNewDNS, Nothing) 

        result = True 
        Exit For 

       Catch ex As Exception 
        Console.WriteLine("Couldn't Set IP Address!") 
        Console.Read() 
        close(FAILURE) 

       End Try 

      End If 

     End If 
    Next 



    Return result 

End Function 

'set computers host name 
Private Function setHostname(hostname As String) As Boolean 
    Dim result As Boolean = False 

    Dim path As New ManagementPath 

    path.Server = System.Net.Dns.GetHostName 
    path.NamespacePath = "root\CIMV2" 
    path.RelativePath = "Win32_Computersystem.Name='" & path.Server & "'" 

    Dim objMO As New ManagementObject(path) 
    Dim params() As Object = {hostname} 
    objMO.InvokeMethod("Rename", params) 
    result = True 

    Return result 
End Function