public void ManualConfig() { Console.WriteLine("Preforming network Manual configuration"); NetworkDevice nic = NetworkDevice.GetDeviceByName("eth0"); //get network device by name IPConfig.Enable(nic, new Address(192, 168, 1, 70), //IP new Address(255, 255, 255, 252), //SUBNET new Address(192, 168, 2, 1) //GATEWAY ); //enable IPv4 configuration Console.WriteLine("Manual config complete"); }
/// <summary> /// CommandIPConfig /// </summary> /// <param name="arguments">Arguments</param> public override ReturnInfo Execute(List <string> arguments) { var xClient = new DHCPClient(); if (arguments[0] == "/release") { xClient.SendReleasePacket(); } else if (arguments[0] == "/ask") { if (xClient.SendDiscoverPacket() != -1) { Console.WriteLine("Configuration applied! Your local IPv4 Address is " + NetworkConfig.CurrentConfig.Value.IPAddress.ToString() + "."); } else { return(new ReturnInfo(this, ReturnCode.ERROR, "DHCP Discover failed. Can't apply dynamic IPv4 address.")); } } else if (arguments[0] == "/listnic") { foreach (var device in NetworkDevice.Devices) { switch (device.CardType) { case CardType.Ethernet: Console.WriteLine("Ethernet Card - " + device.NameID + " - " + device.Name + " (" + device.MACAddress + ")"); break; case CardType.Wireless: Console.WriteLine("Wireless Card - " + device.NameID + " - " + device.Name + " (" + device.MACAddress + ")"); break; } } } else if (arguments[0] == "/set") { if ((arguments.Count == 3) || (arguments.Count == 4)) // ipconfig /set eth0 192.168.1.2/24 {gw} { string[] adrnetwork = arguments[2].Split('/'); Address ip = Address.Parse(adrnetwork[0]); NetworkDevice nic = NetworkDevice.GetDeviceByName(arguments[1]); Address gw = null; if (arguments.Count == 4) { gw = Address.Parse(arguments[3]); } int cidr; try { cidr = int.Parse(adrnetwork[1]); } catch (Exception ex) { return(new ReturnInfo(this, ReturnCode.ERROR, ex.Message)); } Address subnet = Address.CIDRToAddress(cidr); if (nic == null) { return(new ReturnInfo(this, ReturnCode.ERROR, "Couldn't find network device: " + arguments[1])); } if (ip != null && subnet != null && gw != null) { IPConfig.Enable(nic, ip, subnet, gw); Console.WriteLine("Config OK!"); } else if (ip != null && subnet != null) { IPConfig.Enable(nic, ip, subnet, ip); Console.WriteLine("Config OK!"); } else { return(new ReturnInfo(this, ReturnCode.ERROR, "Can't parse IP addresses (make sure they are well formated).")); } } else { return(new ReturnInfo(this, ReturnCode.ERROR, "Usage : ipconfig /set {device} {IPv4/CIDR} {Gateway|null}")); } } else if (arguments[0] == "/nameserver") { if (arguments[1] == "-add") { DNSConfig.Add(Address.Parse(arguments[2])); Console.WriteLine(arguments[2] + " has been added to nameservers."); } else if (arguments[1] == "-rem") { DNSConfig.Remove(Address.Parse(arguments[2])); Console.WriteLine(arguments[2] + " has been removed from nameservers list."); } else { return(new ReturnInfo(this, ReturnCode.ERROR, "Usage : ipconfig /nameserver {add|remove} {IP}")); } } else { return(new ReturnInfo(this, ReturnCode.ERROR, "Wrong usage, please type: ipconfig /help")); } return(new ReturnInfo(this, ReturnCode.OK)); }
/// <summary> /// CommandIPConfig /// </summary> /// <param name="arguments">Arguments</param> public override ReturnInfo Execute(List <string> arguments) { if (arguments[0] == "/release") { DHCPClient.SendReleasePacket(); } else if (arguments[0] == "/ask") { DHCPClient.SendDiscoverPacket(); } else if (arguments[0] == "/listnic") { foreach (var device in NetworkDevice.Devices) { switch (device.CardType) { case CardType.Ethernet: Console.WriteLine("Ethernet Card - " + device.NameID + " - " + device.Name + " (" + device.MACAddress + ")"); break; case CardType.Wireless: Console.WriteLine("Wireless Card - " + device.NameID + " - " + device.Name + " (" + device.MACAddress + ")"); break; } } } else if (arguments[0] == "/set") { if (arguments.Count < 5) { return(new ReturnInfo(this, ReturnCode.ERROR, "Usage : ipconfig /set {device} {IPv4} {Subnet} {Gateway}")); } else { Address ip = Address.Parse(arguments[2]); Address subnet = Address.Parse(arguments[3]); Address gw = Address.Parse(arguments[4]); NetworkDevice nic = NetworkDevice.GetDeviceByName(arguments[1]); if (nic == null) { return(new ReturnInfo(this, ReturnCode.ERROR, "Couldn't find network device: " + arguments[1])); } if (ip != null && subnet != null && gw != null) { NetworkInit.Enable(nic, ip, subnet, gw, gw); Console.WriteLine("Config OK!"); } else { return(new ReturnInfo(this, ReturnCode.ERROR, "Can't parse IP addresses (make sure they are well formated).")); } /*if (NetworkInterfaces.Interface(arguments[1]) != "null") * { * Utils.Settings settings = new Utils.Settings(@"0:\System\" + NetworkInterfaces.Interface(arguments[1]) + ".conf"); * NetworkStack.RemoveAllConfigIP(); * ApplyIP(arguments.ToArray(), settings); //TODO Fix that (arguments) * settings.Push(); * } * else * { * Console.WriteLine("This interface doesn't exists."); * } */ } } else { return(new ReturnInfo(this, ReturnCode.ERROR, "Wrong usage, please type: ipconfig /help")); } return(new ReturnInfo(this, ReturnCode.OK)); }