Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Net;
- using System.Net.NetworkInformation;
- using System.Net.Sockets;
- using System.Threading;
- using UnityEngine;
- using Ping = System.Net.NetworkInformation.Ping;
- public class MyPing : MonoBehaviour {
- private List<string> ips = new List<string>();
- void Start() {
- GetNetworks();
- Ping_all();
- }
- private void GetNetworks() {
- foreach (var f in NetworkInterface.GetAllNetworkInterfaces()) {
- if (f.OperationalStatus != OperationalStatus.Up) continue;
- foreach (GatewayIPAddressInformation d in f.GetIPProperties().GatewayAddresses) {
- Debug.Log($"IP - {d.Address}");
- ips.Add(d.Address.ToString());
- }
- }
- }
- private void Ping_all() {
- var count = 0;
- foreach (var ip in ips) {
- var array = ip.Split('.');
- if(array.Length < 4) continue;
- for (var i = 1; i <= 254; i++) {
- var ipAddress = array[0] + "." + array[1] + "." + array[2] + "." + i;
- Ping(ipAddress, 1000);
- count++;
- }
- }
- _replying = count;
- Debug.Log("All request send!");
- }
- private void Ping(string host, int timeout) {
- //Debug.Log($"Ping to {host}");
- var buf = new byte[] {1, 2, 3, 4};
- var options = new PingOptions(64, true);
- var ping = new Ping();
- new Thread(delegate ()
- {
- try
- {
- ping.PingCompleted += PingCompleted;
- ping.SendAsync(host, timeout, buf, options, host);
- }
- catch
- {
- }
- }).Start();
- }
- public int _replying;
- private void PingCompleted(object sender, PingCompletedEventArgs e) {
- var ip = (string) e.UserState;
- if (e.Reply != null && e.Reply.Status == IPStatus.Success) {
- var hostname = GetHostName(ip);
- var macaddres = GetMacAddress(ip);
- Debug.Log($"Success! HostName: {hostname}, ip: {ip}, macadres: {macaddres} ");
- } else {
- if(e.Reply != null && e.Reply.Status != IPStatus.TimedOut) Debug.LogWarning(e.Reply.Status.ToString());
- //else Debug.LogWarning($"Null reply from {ip}");
- }
- _replying--;
- if (_replying == 0) Debug.Log("All reply!");
- }
- private string GetHostName(string ipAddress) {
- try {
- var entry = Dns.GetHostEntry(ipAddress);
- return entry.HostName;
- } catch (SocketException e) {
- Debug.LogWarning(e.Message.ToString());
- }
- return null;
- }
- private string GetMacAddress(string ipAddress) {
- var process = new System.Diagnostics.Process {
- StartInfo = {
- FileName = "arp",
- Arguments = "-a " + ipAddress,
- UseShellExecute = false,
- RedirectStandardOutput = true,
- CreateNoWindow = true
- }
- };
- process.Start();
- var strOutput = process.StandardOutput.ReadToEnd();
- var substrings = strOutput.Split('-');
- if (substrings.Length < 8) return "OWN Machine";
- 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)}";
- return macAddress;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement