private async void Button_Click(object sender, RoutedEventArgs e) { string start = sip.Text; string end = lip.Text; byte[] sb = IPCon.GetBytes(start); byte[] eb = IPCon.GetBytes(end); flag = false; //for (byte a = sb[0]; a <= eb[0] && a <= 255; a++) // for (byte b = sb[1]; b <= eb[1] && b <= 255; b++) // for (byte c = sb[2]; c <= eb[2] && c <= 255; c++) for (byte i = sb[3]; i <= eb[3] && i <= 255; i++) { if (flag) { break; } PingReply reply = await Ping(IPCon.BytesToIP(sb[0], sb[1], sb[2], i)); string ip = reply.Address.ToString(); IPHostEntry host = Dns.GetHostEntry(IPCon.BytesToIP(sb[0], sb[1], sb[2], i)); IPStatus status = reply.Status; DataRow dr = dt.NewRow(); dr[0] = ip; dr[1] = host.HostName; dr[2] = status.ToString(); dt.Rows.Add(dr); } }
public static string GetGatewayAddress(string ip, string mask) { byte fb = (byte)(IPCon.FirstByte(ip) & IPCon.FirstByte(mask)); byte sb = (byte)(IPCon.SecondByte(ip) & IPCon.SecondByte(mask)); byte tb = (byte)(IPCon.ThirdByte(ip) & IPCon.ThirdByte(mask)); byte fob = (byte)((IPCon.FourthByte(ip) & IPCon.FourthByte(mask)) + 1); return(IPCon.BytesToIP(fb, sb, tb, fob)); }
public static string GetBroadcastAddress(string ip, string mask) { string nwa = GetNetworkAddress(ip, mask); byte fb = (byte)(~(IPCon.FirstByte(mask)) | (IPCon.FirstByte(nwa))); byte sb = (byte)(~(IPCon.SecondByte(mask)) | (IPCon.SecondByte(nwa))); byte tb = (byte)(~(IPCon.ThirdByte(mask)) | (IPCon.ThirdByte(nwa))); byte fob = (byte)(~(IPCon.FourthByte(mask)) | (IPCon.FourthByte(nwa))); return(IPCon.BytesToIP(fb, sb, tb, fob)); }
public MainWindow() { InitializeComponent(); DataColumn col1 = new DataColumn("IP", typeof(string)); DataColumn col2 = new DataColumn("Host", typeof(string)); DataColumn col3 = new DataColumn("Status", typeof(string)); dt.Columns.Add(col1); dt.Columns.Add(col2); dt.Columns.Add(col3); dg.ItemsSource = dt.DefaultView; mac.Text += GetMacAddress(); string host = Dns.GetHostName(); //try //{ string ips = new WebClient().DownloadString("https://api.ipify.org"); byte[] sb = IPCon.GetBytes(ips); IPAddress ip1 = new IPAddress(sb); exip.Text += ip1; //} //catch (WebException) { } IPAddress ip2 = Dns.GetHostEntry(host).AddressList[0]; mip.Text = GetSubnetMask(ip2).ToString(); locip.Text += ip2; List <string> ipl = GetIPs(); sip.Text = ipl[0]; lip.Text = ipl.Last(); IPAddress ip = new IPAddress(IPCon.GetBytes(ips)); int bits = 24; uint mask = ~(uint.MaxValue >> bits); byte[] ipBytes = ip.GetAddressBytes(); byte[] maskBytes = BitConverter.GetBytes(mask).Reverse().ToArray(); byte[] startIPBytes = new byte[ipBytes.Length]; byte[] endIPBytes = new byte[ipBytes.Length]; for (int i = 0; i < ipBytes.Length; i++) { startIPBytes[i] = (byte)(ipBytes[i] & maskBytes[i]); endIPBytes[i] = (byte)(ipBytes[i] | ~maskBytes[i]); } IPAddress startIP = new IPAddress(startIPBytes); IPAddress endIP = new IPAddress(endIPBytes); sip.Text = startIP.ToString(); lip.Text = endIP.ToString(); }
public static byte[] GetBytes(string ip) { byte[] bytes = { IPCon.FirstByte(ip), IPCon.SecondByte(ip), IPCon.ThirdByte(ip), IPCon.FourthByte(ip) }; return(bytes); }