SHARE
TWEET

Untitled

a guest Feb 4th, 2019 80 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Net;
  4. using System.Net.NetworkInformation;
  5. using System.Net.Sockets;
  6. using System.Threading;
  7. using UnityEngine;
  8. using Ping = System.Net.NetworkInformation.Ping;
  9.  
  10. public class MyPing : MonoBehaviour {
  11.   private List<string> ips = new List<string>();
  12.  
  13.     void Start() {
  14.       GetNetworks();
  15.       Ping_all();
  16.     }
  17.  
  18.   private void GetNetworks() {
  19.     foreach (var f in NetworkInterface.GetAllNetworkInterfaces()) {
  20.       if (f.OperationalStatus != OperationalStatus.Up) continue;
  21.       foreach (GatewayIPAddressInformation d in f.GetIPProperties().GatewayAddresses) {
  22.         Debug.Log($"IP - {d.Address}");
  23.         ips.Add(d.Address.ToString());
  24.       }
  25.     }
  26.   }
  27.  
  28.   private void Ping_all() {
  29.     var count = 0;
  30.     foreach (var ip in ips) {
  31.       var array = ip.Split('.');
  32.       if(array.Length < 4) continue;
  33.       for (var i = 1; i <= 254; i++) {
  34.         var ipAddress = array[0] + "." + array[1] + "." + array[2] + "." + i;          
  35.         Ping(ipAddress, 1000);
  36.         count++;
  37.       }
  38.     }
  39.     _replying = count;
  40.     Debug.Log("All request send!");
  41.   }
  42.  
  43.   private void Ping(string host, int timeout) {
  44.     //Debug.Log($"Ping to {host}");
  45.  
  46.     var buf = new byte[] {1, 2, 3, 4};
  47.     var options = new PingOptions(64, true);
  48.     var ping = new Ping();
  49.  
  50.     new Thread(delegate ()
  51.     {
  52.       try
  53.       {
  54.         ping.PingCompleted += PingCompleted;
  55.         ping.SendAsync(host, timeout, buf, options, host);
  56.       }
  57.       catch
  58.       {
  59.       }
  60.     }).Start();
  61.   }
  62.  
  63.   public int _replying;
  64.   private void PingCompleted(object sender, PingCompletedEventArgs e) {
  65.     var ip = (string) e.UserState;
  66.     if (e.Reply != null && e.Reply.Status == IPStatus.Success) {
  67.       var hostname = GetHostName(ip);
  68.       var macaddres = GetMacAddress(ip);
  69.       Debug.Log($"Success! HostName: {hostname}, ip: {ip}, macadres: {macaddres} ");
  70.     } else {
  71.       if(e.Reply != null && e.Reply.Status != IPStatus.TimedOut) Debug.LogWarning(e.Reply.Status.ToString());
  72.       //else Debug.LogWarning($"Null reply from {ip}");
  73.     }
  74.  
  75.     _replying--;
  76.     if (_replying == 0) Debug.Log("All reply!");
  77.   }
  78.  
  79.   private string GetHostName(string ipAddress) {
  80.     try {
  81.       var entry = Dns.GetHostEntry(ipAddress);
  82.       return entry.HostName;
  83.     } catch (SocketException e) {
  84.       Debug.LogWarning(e.Message.ToString());
  85.     }
  86.  
  87.     return null;
  88.   }
  89.  
  90.   private string GetMacAddress(string ipAddress) {
  91.     var process = new System.Diagnostics.Process {
  92.       StartInfo = {
  93.         FileName = "arp",
  94.         Arguments = "-a " + ipAddress,
  95.         UseShellExecute = false,
  96.         RedirectStandardOutput = true,
  97.         CreateNoWindow = true
  98.       }
  99.     };
  100.     process.Start();
  101.     var strOutput = process.StandardOutput.ReadToEnd();
  102.     var substrings = strOutput.Split('-');
  103.     if (substrings.Length < 8) return "OWN Machine";
  104.     var macAddress = $"{substrings[3].Substring(Math.Max(0, substrings[3].Length - 2))} - {substrings[4]} - {substrings[5]} - {substrings[6]} - {substrings[7]} - {substrings[8].Substring(0, 2)}";
  105.     return macAddress;
  106.   }
  107. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top