/// <summary> /// Set Network parameters /// </summary> static internal NetworkPropertiesResponse SetNetworkProperties( string IpAddresses, string hostName, string IPSubnet, string SubnetMask, string Gateway, string DNSPrimary, string DNSSecondary, string ipSource, int interfaceNumber = 1) { Console.WriteLine(ipSource); if (interfaceNumber > numberInterfaces) { Console.WriteLine( "Invalid Interface Number. Network Interface properties cannot be changed."); } NetworkPropertiesResponse response = new NetworkPropertiesResponse(); // Set default completion code to unknown. response.completionCode = 0; try { IpAddresses = IpAddresses.Trim(); SubnetMask = SubnetMask.Trim(); if (!string.IsNullOrEmpty(Gateway)) { Gateway = Gateway.Trim(); } Console.WriteLine(IpAddresses); Console.WriteLine(SubnetMask); if (!(checkIpFormat(IpAddresses) && checkIpFormat(SubnetMask) && checkIpFormat(DNSPrimary) && checkIpFormat(DNSSecondary))) { Console.WriteLine(@"Invalid Input Network Parameters. Network interface properties cannot be changed."); } ManagementClass mc = new ManagementClass("Win32_NetworkAdapterConfiguration"); ManagementObjectCollection moc = mc.GetInstances(); int index = 1; foreach (ManagementObject mo in moc) { Console.WriteLine("index: " + index); if ((bool)mo["IPEnabled"]) { if (index == interfaceNumber) { try { Console.WriteLine("index" + index); Console.WriteLine(ipSource); ManagementBaseObject newGateWay = mo.GetMethodParameters("SetGateways"); ManagementBaseObject newDNS = mo.GetMethodParameters("SetDNSServerSearchOrder"); if (string.Equals(ipSource, "DHCP", StringComparison.OrdinalIgnoreCase)) { // Set DNS newDNS["DNSServerSearchOrder"] = null; ManagementBaseObject enableDHCP = mo.InvokeMethod("EnableDHCP", null, null); } else if (string.Equals(ipSource, "STATIC", StringComparison.OrdinalIgnoreCase)) { Console.WriteLine("Enabling STATIC IP ..."); try { mo.InvokeMethod("EnableStatic", new object[] {new string[] { IpAddresses }, new string[] { SubnetMask } }); // Set DNS string[] DNSServerSearchOrder = new string[] { DNSPrimary, DNSSecondary }; newDNS["DNSSeverSearchOrder"] = DNSServerSearchOrder; newDNS["DNSHostName"] = hostName; } catch (Exception ex) { Console.WriteLine("Exception in EnableStatic: " + ex.Message); } } if (!string.IsNullOrEmpty(Gateway)) { // Set Gateway newGateWay["DefaultIPGateway"] = new string[] { Gateway }; newGateWay["GatewayCostMetric"] = new int[] { 1 }; ManagementBaseObject setGateways = mo.InvokeMethod("SetGateways", newGateWay, null); } ManagementBaseObject setDNS = mo.InvokeMethod("SetDNSServerSearchOrder", newDNS, null); } catch (Exception e) { Console.WriteLine("Error: " + e.Message); } } index++; } } } catch (Exception ex) { response.completionCode = 0; Console.WriteLine("exception encountered: " + ex.Message); } response.completionCode = 1; return response; }
/// <summary> /// Set Network parameters /// </summary> internal static NetworkPropertiesResponse SetNetworkProperties( string ipSource, string IpAddress, string SubnetMask, string DNSPrimary, string DNSSecondary, string Gateway, uint interfaceNumber) { NetworkPropertiesResponse response = new NetworkPropertiesResponse(); // Set default completion code to unknown. response.completionCode = Contracts.CompletionCode.Unknown; if (interfaceNumber >= numberInterfaces) { Console.WriteLine("Invalid Network Interface number. Network Interface properties cannot be changed."); response.completionCode = Contracts.CompletionCode.Failure; return response; } try { ManagementClass mc = new ManagementClass("Win32_NetworkAdapterConfiguration"); ManagementObjectCollection moc = mc.GetInstances(); foreach (ManagementObject mo in moc) { // Physical network adapters have MAC addresses. Other Network Adapters will have null MAC addresses. if (!string.IsNullOrEmpty((string)mo["MACAddress"])) { int logicalIndex = -1; uint index = (uint)mo["Index"]; if (index >= 0) { logicalIndex = (int)index; } else { // Failed to verify Index Console.WriteLine(@"Could not obtain network controller interface Index."); response.completionCode = Contracts.CompletionCode.Failure; return response; } // Configure specified network interface if ((int)interfaceNumber == Contracts.SharedFunc.NetworkCtrlPhysicalIndex(logicalIndex)) { // Check parameters before doing any configuration // Trim white spaces from IP addresses and check format bool ipCheck = true; if (!string.IsNullOrEmpty(DNSPrimary)) { DNSPrimary = DNSPrimary.Trim(); ipCheck = ipCheck & checkIpFormat(DNSPrimary); // Secondary DNS is only configured if primary DNS is specified if (!string.IsNullOrEmpty(DNSSecondary)) { DNSSecondary = DNSSecondary.Trim(); ipCheck = ipCheck & checkIpFormat(DNSSecondary); } } if (!ipCheck) { Console.WriteLine(@"Invalid DNS Server Parameters. Network interface properties cannot be changed."); response.completionCode = Contracts.CompletionCode.Failure; return response; } // Configure network properties for DHCP or static IP if (string.Equals(ipSource, "DHCP", StringComparison.OrdinalIgnoreCase)) { Console.WriteLine("Configuring DHCP ..."); ManagementBaseObject enableDHCP = mo.InvokeMethod("EnableDHCP", null, null); response.completionCode = checkWin32_CmdReturnVal((uint)enableDHCP["returnValue"], "EnableDHCP"); } else if (string.Equals(ipSource, "STATIC", StringComparison.OrdinalIgnoreCase)) { // Trim white spaces from IP addresses and check format bool staticIpCheck = true; IpAddress = IpAddress.Trim(); SubnetMask = SubnetMask.Trim(); staticIpCheck = staticIpCheck & checkIpFormat(IpAddress); staticIpCheck = staticIpCheck & checkIpFormat(SubnetMask); if (!string.IsNullOrEmpty(Gateway)) { Gateway = Gateway.Trim(); staticIpCheck = staticIpCheck & checkIpFormat(Gateway); } if (!staticIpCheck) { Console.WriteLine(@"Invalid Input Network Parameters. Network interface properties cannot be changed."); response.completionCode = Contracts.CompletionCode.Failure; return response; } Console.WriteLine("Configuring static IP address ..."); object enableStatic = mo.InvokeMethod("EnableStatic", new object[] {new string[] { IpAddress }, new string[] { SubnetMask } }); response.completionCode = checkWin32_CmdReturnVal((uint)enableStatic, "EnableStatic"); // Get the SetGateways method parameters ManagementBaseObject newGateWay = mo.GetMethodParameters("SetGateways"); newGateWay["DefaultIPGateway"] = null; if (!string.IsNullOrEmpty(Gateway)) { // Set Gateway to user specified value newGateWay["DefaultIPGateway"] = new string[] { Gateway }; newGateWay["GatewayCostMetric"] = new int[] { 1 }; } if (response.completionCode == Contracts.CompletionCode.Success) { ManagementBaseObject setGateways = mo.InvokeMethod("SetGateways", newGateWay, null); response.completionCode = checkWin32_CmdReturnVal((uint)setGateways["returnValue"], "SetGateways"); } } else { // Not DHCP and not static IP Console.WriteLine(@"Invalid IP Source type."); response.completionCode = Contracts.CompletionCode.Failure; return response; } if (response.completionCode == Contracts.CompletionCode.Success) { // Retrieve Set DNS Server method ManagementBaseObject newDNS = mo.GetMethodParameters("SetDNSServerSearchOrder"); newDNS["DNSServerSearchOrder"] = null; // Set DNS Server Search Order if specified if (!string.IsNullOrEmpty(DNSPrimary)) { string[] DNSServerSearchOrder; // Secondary DNS is only configured if primary DNS is specified if (!string.IsNullOrEmpty(DNSSecondary)) { DNSServerSearchOrder = new string[] { DNSPrimary, DNSSecondary }; } else { DNSServerSearchOrder = new string[] { DNSPrimary }; } newDNS["DNSServerSearchOrder"] = DNSServerSearchOrder; } // Set DNS Server Search order ManagementBaseObject setDNS = mo.InvokeMethod("SetDNSServerSearchOrder", newDNS, null); response.completionCode = checkWin32_CmdReturnVal((uint)setDNS["returnValue"], "SetDNSServerSearchOrder"); } } } } } catch (Exception ex) { response.completionCode = Contracts.CompletionCode.Failure; Console.WriteLine(WcsCliConstants.commandFailure + " Exception encountered: " + ex.Message); } return response; }
/// <summary> /// Get Network parameters /// </summary> static internal NetworkPropertiesResponse GetNetworkProperties() { string[] ipAddresses = null; string[] subnets = null; string[] gateways = null; string dnsHostName = null; string dhcpServer = null; string dnsDomain = null; string macAddress = null; bool dhcpEnabled = true; string primary = null; string secondary = null; string[] DNSServerAddress = null; NetworkPropertiesResponse response = new NetworkPropertiesResponse(); response.NetworkPropertyCollection = new List<NetworkProperty>(); // Create management class object using the Win32_NetworkAdapterConfiguration // class to retrieve different attributes of the network adapters ManagementClass mc = new ManagementClass("Win32_NetworkAdapterConfiguration"); // Create ManagementObjectCollection to retrieve the attributes ManagementObjectCollection moc = mc.GetInstances(); // Set default completion code to unknown. response.completionCode = 0; try { foreach (ManagementObject mo in moc) { // If interface has IP enabled if ((bool)mo["ipEnabled"]) { ipAddresses = (string[])mo["IPAddress"]; subnets = (string[])mo["IPSubnet"]; gateways = (string[])mo["DefaultIPGateway"]; dnsHostName = (string)mo["DNSHostName"]; dhcpServer = (string)mo["DHCPServer"]; dnsDomain = (string)mo["DNSDomain"]; macAddress = (string)mo["MACAddress"]; dhcpEnabled = (bool)mo["DHCPEnabled"]; primary = (string)mo["WINSPrimaryServer"]; secondary = (string)mo["WINSSecondaryServer"]; DNSServerAddress = (string[])mo["DNSServerSearchOrder"]; int index = 0; foreach (string ip in ipAddresses) { if (NetworkInterfaces.checkIpFormat(ip)) { NetworkProperty cr = new NetworkProperty(); cr.ipAddress = ipAddresses.ToArray()[index]; if (subnets != null) { cr.subnetMask = subnets.ToArray()[index]; } if (gateways != null) { cr.gatewayAddress = gateways.ToArray()[index]; } if (DNSServerAddress != null) { cr.dnsAddress = DNSServerAddress.ToArray()[index]; } cr.dhcpServer = dhcpServer; cr.dnsDomain = dnsDomain; cr.dnsHostName = dnsHostName; cr.macAddress = macAddress; cr.dhcpEnabled = dhcpEnabled; cr.primary = primary; cr.secondary = secondary; cr.completionCode = 1; response.NetworkPropertyCollection.Add(cr); } index++; } } else // all other interfaces (with ip not enables) { macAddress = (string)mo["MACAddress"]; // Populating interfaces only with valid mac addresses - ignoring loopback and other virtual interfaces if (macAddress != null) { NetworkProperty cr = new NetworkProperty(); cr.macAddress = macAddress; cr.completionCode = 1; response.NetworkPropertyCollection.Add(cr); } } } } catch (Exception ex) { response.completionCode = 0; Console.WriteLine("Get NIC properties failed with message " + ex.Message); return response; } response.completionCode = 1; return response; }
/// <summary> /// Get Network parameters /// </summary> internal static NetworkPropertiesResponse GetNetworkProperties() { NetworkPropertiesResponse response = new NetworkPropertiesResponse(); response.NetworkPropertyCollection = new List<NetworkProperty>(); // Create management class object using the Win32_NetworkAdapterConfiguration // class to retrieve different attributes of the network adapters ManagementClass mc = new ManagementClass("Win32_NetworkAdapterConfiguration"); // Create ManagementObjectCollection to retrieve the attributes ManagementObjectCollection moc = mc.GetInstances(); // Set default completion code to unknown. response.completionCode = Contracts.CompletionCode.Unknown; try { foreach (ManagementObject mo in moc) { // Physical network adapters have MAC addresses. Other Network Adapters will have null MAC addresses. if (!string.IsNullOrEmpty((string)mo["MACAddress"])) { NetworkProperty cr = new NetworkProperty(); cr.index = (uint)mo["Index"]; cr.description = (string)mo["Description"]; cr.serviceName = (string)mo["ServiceName"]; cr.macAddress = (string)mo["MACAddress"]; cr.ipEnabled = (bool)mo["IPEnabled"]; cr.ipAddress = (string[])mo["IPAddress"]; cr.subnetMask = (string[])mo["IPSubnet"]; cr.gatewayAddress = (string[])mo["DefaultIPGateway"]; cr.dhcpEnabled = (bool)mo["DHCPEnabled"]; cr.dhcpServer = (string)mo["DHCPServer"]; cr.dnsHostName = (string)mo["DNSHostName"]; cr.dnsDomain = (string)mo["DNSDomain"]; cr.dnsSearchOrder = (string[])mo["DNSServerSearchOrder"]; cr.winsPrimary = (string)mo["WINSPrimaryServer"]; cr.winsSecondary = (string)mo["WINSSecondaryServer"]; cr.completionCode = Contracts.CompletionCode.Success; response.NetworkPropertyCollection.Add(cr); } } } catch (Exception ex) { response.completionCode = Contracts.CompletionCode.Failure; Console.WriteLine("Get NIC properties failed with message " + ex.Message); return response; } response.completionCode = Contracts.CompletionCode.Success; return response; }