Rob van der Woude's Scripting Pages
Powered by GeSHi

Source code for wanip.cs

(view source code of wanip.cs as plain text)

  1. namespace RobvanderWoude
  2. {
  3. 	class WANIP
  4. 	{
  5. 		static string progver = "1.03";
  6.  
  7.  
  8. 		static int Main( string[] args )
  9. 		{
  10. 			// These are the URLs the program uses to try and get the computer's WAN IP address; if
  11. 			// a site fails, the next one is tried; if all fail, their error messages are displayed
  12. 			string[] urls = { "https://www.robvanderwoude.com/wanip.php", "https://www.robvanderwoude.net/wanip.php", "http://automation.whatismyip.com/n09230945.asp" };
  13.  
  14. 			if ( args.Length == 0 )
  15. 			{
  16. 				bool found = false;
  17. 				string errormessage = System.String.Empty;
  18. 				string ipPattern = @"^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$";
  19. 				string wanip = System.String.Empty;
  20. 				// The next couple of ServicePointManager lines are required for secure connections only
  21. 				System.Net.ServicePointManager.Expect100Continue = true;
  22. 				System.Net.ServicePointManager.SecurityProtocol |= System.Net.SecurityProtocolType.Tls12;
  23. 				System.Net.ServicePointManager.SecurityProtocol |= System.Net.SecurityProtocolType.Ssl3;
  24. 				System.Net.WebClient myWebClient = new System.Net.WebClient( );
  25. 				foreach ( string url in urls )
  26. 				{
  27. 					if ( !found )
  28. 					{
  29. 						try
  30. 						{
  31. 							System.IO.Stream myStream = myWebClient.OpenRead( url );
  32. 							System.IO.StreamReader myStreamReader = new System.IO.StreamReader( myStream );
  33. 							wanip = myStreamReader.ReadToEnd( );
  34. 							if ( System.Text.RegularExpressions.Regex.IsMatch( wanip, ipPattern ) )
  35. 							{
  36. 								System.Console.WriteLine( wanip );
  37. 								found = true;
  38. 							}
  39. 							else
  40. 							{
  41. 								errormessage += System.String.Format( "\n\nThe URL did not return a valid IP address: {0}", url );
  42. 							}
  43. 						}
  44. 						catch ( System.Exception e )
  45. 						{
  46. 							errormessage += System.String.Format( "\n\n{0} ({1})", e.Message, url );
  47. 						}
  48. 					}
  49. 				}
  50. 				if ( found )
  51. 				{
  52. 					return 0;
  53. 				}
  54. 				else
  55. 				{
  56. 					if ( !System.String.IsNullOrEmpty( errormessage ) )
  57. 					{
  58. 						System.Console.Error.WriteLine( errormessage );
  59. 					}
  60. 					return 1;
  61. 				}
  62. 			}
  63. 			else
  64. 			{
  65. 				System.Console.Error.WriteLine( );
  66.  
  67. 				System.Console.Error.WriteLine( "WANIP.exe,  Version {0}", progver );
  68.  
  69. 				System.Console.Error.WriteLine( "Return the computer's WAN IP address" );
  70.  
  71. 				System.Console.Error.WriteLine( );
  72.  
  73. 				System.Console.Error.WriteLine( "Usage:  WANIP" );
  74.  
  75. 				System.Console.Error.WriteLine( );
  76.  
  77. 				System.Console.Error.WriteLine( "Note:   The program tries {0} different URLs to get the WAN IP address", urls.Length );
  78.  
  79. 				System.Console.Error.WriteLine( );
  80.  
  81. 				System.Console.Error.WriteLine( "Written by Rob van der Woude" );
  82.  
  83. 				System.Console.Error.WriteLine( "https://www.robvanderwoude.com" );
  84.  
  85. 				return 1;
  86. 			}
  87. 		}
  88. 	}
  89. }
  90.  

page last uploaded: 2018-12-04, 10:47