Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.IO;
- using System.Net;
- using System.Net.NetworkInformation;
- using System.Text;
- namespace IpDiscover
- {
- class Program
- {
- static IPAddress[] GetIps1()
- {
- return Dns.GetHostAddresses(Dns.GetHostName());
- }
- static IPAddress[] GetIps2()
- {
- List<IPAddress> list = new List<IPAddress>();
- NetworkInterface[] interfaces = NetworkInterface.GetAllNetworkInterfaces();
- foreach (NetworkInterface adapter in interfaces)
- {
- if (adapter.OperationalStatus == OperationalStatus.Up)
- {
- foreach (UnicastIPAddressInformation ipu in adapter.GetIPProperties().UnicastAddresses)
- {
- IPAddress ip = ipu.Address;
- list.Add(ip);
- }
- }
- }
- return list.ToArray();
- }
- static void Main(string[] args)
- {
- string out1 = "";
- string out2 = "";
- Console.WriteLine("Please wait...");
- IPAddress[] ips = GetIps2();
- foreach (IPAddress ip in ips)
- {
- try
- {
- if (ip.AddressFamily != System.Net.Sockets.AddressFamily.InterNetwork)
- throw new Exception("Not an ipv4");
- var request = (HttpWebRequest)HttpWebRequest.Create("http://www.clodo.it/projects/whatismyip/");
- request.KeepAlive = false;
- request.Timeout = 5000;
- request.ReadWriteTimeout = 5000;
- request.ServicePoint.BindIPEndPointDelegate = delegate
- {
- return new IPEndPoint(ip, 0);
- };
- var response = (HttpWebResponse)request.GetResponse();
- Stream receiveStream = response.GetResponseStream();
- StreamReader readStream = new StreamReader(receiveStream, Encoding.UTF8);
- string result = readStream.ReadToEnd().Trim();
- out1 += result + "\r\n";
- out2 += ip.ToString() + ", " + result + "\r\n";
- response.Close();
- }
- catch (Exception e)
- {
- out2 += ip.ToString() + ", " + e.Message + "\r\n";
- }
- }
- Console.WriteLine("");
- //Console.WriteLine("Result:");
- Console.WriteLine(out1);
- //Console.WriteLine("Debug:");
- //Console.WriteLine(out2);
- Console.WriteLine("Press any key to close.");
- Console.ReadKey();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement