Advertisement
Guest User

Untitled

a guest
Jun 28th, 2010
291
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.02 KB | None | 0 0
  1. /*use all System here*/
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Text.RegularExpressions;
  5.  
  6. /*
  7.  * using System.Text;
  8.  * using System.Collections.Generic;
  9.  * using System.Linq;
  10. */
  11.  
  12. using System.Net;
  13.  
  14. namespace Serco.Main
  15. {
  16.     class Program
  17.     {
  18.         static void Main(string[] args)
  19.         {
  20.             show_main_menu(true);
  21.             string option = show_main_menu();
  22.  
  23.             switch (option)
  24.             {
  25.                 case "1":
  26.                     FindHostByAssetTag();
  27.                 break;
  28.             }
  29.         }
  30.  
  31.         static string show_main_menu(bool header = false)
  32.         {
  33.             if (header == true)
  34.             {
  35.                 Console.Clear();
  36.                 Console.Write("Network Toolbox (V1) - Serco ICT Team - Developed by Robert Pitt\n\n");
  37.                 return "";
  38.             }
  39.  
  40.             //Show the menu options
  41.             Console.WriteLine("1) Find Host via Asset Tag");
  42.             Console.WriteLine("To exit type 'exit' or 'x'");
  43.             Console.WriteLine("\n\n");
  44.             Console.Write("Please enter an option: ");
  45.             string o = Console.ReadLine().ToString();
  46.            
  47.             return o;
  48.         }
  49.  
  50.         /*
  51.          * methods for each function!
  52.          */
  53.         static void FindHostByAssetTag(string error = "")
  54.         {
  55.             show_main_menu(true);
  56.             Console.WriteLine("Asset Tag Searcher\n");
  57.             if (error != "")
  58.             {
  59.                 Console.WriteLine("Error: {0}\n",error);
  60.             }
  61.             Console.Write("Enter Asset Tag or * for all: ");
  62.  
  63.             string assetTag = Console.ReadLine().ToString();
  64.             Regex AssetMatch = new Regex("[0-9]{6}|[*]{1}");
  65.             if (AssetMatch.Match(assetTag).Success == false)
  66.             {
  67.                 FindHostByAssetTag("Asset tag failed validation, please use exactly six digits");
  68.                 return;
  69.             }
  70.  
  71.             /*lests start the search*/
  72.             string localH = Dns.GetHostName();
  73.             //Console.WriteLine("Current Host:" + localH);
  74.             Console.WriteLine("Searching...");
  75.  
  76.             /*
  77.              *  DNS Searching Now
  78.              */
  79.             IPHostEntry localHostname = Dns.GetHostEntry(Dns.GetHostName());
  80.             if (localHostname.AddressList.Length == 0)
  81.             {
  82.                 FindHostByAssetTag("unable to detect any hosts at all. make sure a connection is active");
  83.                 return;
  84.             }
  85.             int count = 0;
  86.             Dictionary<string, IPAddress> storage = new Dictionary<string, IPAddress>();
  87.  
  88.             foreach (IPAddress ip in localHostname.AddressList)
  89.             {
  90.                 IPHostEntry Address = Dns.GetHostEntry(ip);
  91.                 string current_host = Address.HostName.ToString();
  92.                 if (assetTag == "*" || current_host.IndexOf(assetTag) > 0)
  93.                 {
  94.                     count++;
  95.                     storage.Add(count.ToString(),ip);//Store as string
  96.                     Console.WriteLine("("+count+")\t" + Address.HostName.ToString());
  97.                 }
  98.             }
  99.             if (count == 0)
  100.             {
  101.                 FindHostByAssetTag("Unable to find any hosts matching (" + assetTag + ")");
  102.                 return;
  103.             }
  104.  
  105.             //Show the menu select menu for the selected asset tag
  106.             Console.WriteLine("Please select an ID for options: ");
  107.             string selected = Console.ReadLine();
  108.             if (Int32.Parse(selected) > 0 && storage.ContainsKey(selected))
  109.             {
  110.                 if (storage.ContainsKey(selected))
  111.                 {
  112.                     show_main_menu(true);
  113.                     Console.WriteLine("options for item {0}", selected);
  114.                     string option = Console.ReadLine();
  115.                 }
  116.             }
  117.             else
  118.             {
  119.                 Console.WriteLine("item could not be found!");
  120.                 Console.Read();
  121.             }
  122.         }
  123.     }
  124. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement