Advertisement
Guest User

Untitled

a guest
May 14th, 2017
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 9.51 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using Lib.Network.PingIP;
  6. using Lib.System.Command;
  7.  
  8.  
  9. namespace Brendan
  10. {
  11.     class Program
  12.     {
  13.         private const string prefixBeforeLogon = ">";
  14.         private const string prefixAfterLogon = "(BT):>>";
  15.         private const string uname = "Brendan";
  16.         private const string pwd = "password";
  17.  
  18.        
  19.         private enum EnumCommand
  20.         {
  21.             Invalid,
  22.             Exit,
  23.             Login,
  24.             Logout,
  25.             Username,
  26.             Ping,
  27.             CLS,
  28.             RDP
  29.         }
  30.  
  31.         /// <summary>
  32.         /// Main program
  33.         /// </summary>
  34.         /// <param name="args">The args as string array.</param>
  35.         static void Main(string[] args)
  36.         {
  37.             var loggedIn = false;
  38.  
  39.                                  
  40.             Console.WriteLine("Welcome! Use #Exit to close this program.");
  41.  
  42.             // Initial input
  43.             Console.Write(prefixBeforeLogon);
  44.             //var cCommand = GetCommand(Console.ReadLine());
  45.  
  46.             string userInput = Console.ReadLine();
  47.             string[] userParams = userInput.Split(' ');
  48.             var cCommand = GetCommand(userParams[0]);
  49.  
  50.  
  51.             // Keep this program running untill user exits
  52.             while (cCommand != EnumCommand.Exit)
  53.             {
  54.                 switch (cCommand)
  55.                 {
  56.                        
  57.                     case EnumCommand.Invalid:
  58.                         UserCommand.Invalid();
  59.                         Console.WriteLine("Invalid command. Use #Exit to quit!");
  60.                         break;
  61.                     case EnumCommand.Login:
  62.                         UserCommand.Login();
  63.                         Console.Write("Username: ");
  64.                         if (Console.ReadLine() == uname)
  65.                         {
  66.                             Console.Write("Password: ");
  67.                         }
  68.                         else
  69.                         {
  70.                             Console.WriteLine("Invalid Username!");
  71.                         }
  72.                         if (Console.ReadLine() == pwd)
  73.                         {
  74.                             loggedIn = true;
  75.                            
  76.                            
  77.                         }
  78.                         else
  79.                         {
  80.                             Console.WriteLine("Incorrect Password!");
  81.                         }
  82.                         break;
  83.                     case EnumCommand.Logout:
  84.                         UserCommand.Logout();
  85.                         Console.WriteLine("You are logged out!");
  86.                         loggedIn = false;
  87.                         break;
  88.                     case EnumCommand.Username:
  89.                         UserCommand.Username();
  90.                         Console.WriteLine(loggedIn ? "Brendan" : "Please login first");
  91.                         break;
  92.                     default:
  93.                         Console.WriteLine("Program error!");
  94.                         break;
  95.                     case EnumCommand.Ping:
  96.                         UserCommand.Ping();
  97.                         break;
  98.                     case EnumCommand.RDP:
  99.                         UserCommand.RDP();
  100.                         System.Diagnostics.Process.Start("C:\\Windows\\System32\\mstsc.exe");
  101.                         Console.WriteLine("Microsoft Remote Desktop Services Started!");
  102.                         break;
  103.  
  104.                     case EnumCommand.CLS:
  105.                         UserCommand.CLS();
  106.                         Console.Clear();
  107.                         break;
  108.  
  109.                 }
  110.                        
  111.  
  112.                 // Use of immidiate if - "statement ? if_true : if_false"
  113.                 Console.Write(loggedIn ? prefixAfterLogon : prefixBeforeLogon);
  114.  
  115.                 // Get next command
  116.                 //cCommand = GetCommand(Console.ReadLine());
  117.  
  118.                
  119.                 userInput = Console.ReadLine();
  120.                 userParams = userInput.Split(' ');
  121.                 cCommand = GetCommand(userParams[0]);
  122.  
  123.             }
  124.         }
  125.  
  126.         /// <summary>
  127.         /// Returns the enum value of the user command.
  128.         /// </summary>
  129.         /// <param name="userInput">The user input as string.</param>
  130.         /// <returns></returns>
  131.         private static EnumCommand GetCommand(string userInput)
  132.         {
  133.             switch (userInput.ToUpperInvariant())
  134.             {
  135.                 case "#EXIT":
  136.                     return EnumCommand.Exit;
  137.                 case "#LOGIN":
  138.                     return EnumCommand.Login;
  139.                 case "#LOGOUT":
  140.                     return EnumCommand.Logout;
  141.                 case "$USERNAME":
  142.                     return EnumCommand.Username;
  143.                 case "$PING":
  144.                     return EnumCommand.Ping;
  145.                 case "$CLS":
  146.                     return EnumCommand.CLS;
  147.                 case "$RDP":
  148.                     return EnumCommand.RDP;
  149.                 default:
  150.                     return EnumCommand.Invalid;
  151.             }
  152.         }
  153.     }
  154. }
  155.  
  156.  
  157.  
  158.  
  159.  
  160.  
  161.  
  162.  
  163. using System;
  164. using System.Text;
  165. using System.Net;
  166. using System.Net.NetworkInformation;
  167. using System.ComponentModel;
  168. using System.Threading;
  169.  
  170. namespace Lib.Network.PingIP
  171. {
  172.     public class PingStart
  173.     {
  174.         public static void PingIP(string ipToPing)
  175.         {
  176.             if (ipToPing.Length == 0)
  177.                 throw new ArgumentException ("Ping needs a host or IP Address.");
  178.  
  179.             string who = ipToPing;
  180.             AutoResetEvent waiter = new AutoResetEvent (false);
  181.  
  182.             Ping pingSender = new Ping ();
  183.  
  184.             // When the PingCompleted event is raised,
  185.             // the PingCompletedCallback method is called.
  186.             pingSender.PingCompleted += new PingCompletedEventHandler (PingCompletedCallback);
  187.  
  188.             // Create a buffer of 32 bytes of data to be transmitted.
  189.             string data = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa";
  190.             byte[] buffer = Encoding.ASCII.GetBytes (data);
  191.  
  192.             // Wait 12 seconds for a reply.
  193.             int timeout = 12000;
  194.  
  195.             // Set options for transmission:
  196.             // The data can go through 64 gateways or routers
  197.             // before it is destroyed, and the data packet
  198.             // cannot be fragmented.
  199.             PingOptions options = new PingOptions (64, true);
  200.  
  201.             Console.WriteLine ("Time to live: {0}", options.Ttl);
  202.             Console.WriteLine ("Don't fragment: {0}", options.DontFragment);
  203.  
  204.             // Send the ping asynchronously.
  205.             // Use the waiter as the user token.
  206.             // When the callback completes, it can wake up this thread.
  207.             pingSender.SendAsync(who, timeout, buffer, options, waiter);
  208.  
  209.             // Prevent this example application from ending.
  210.             // A real application should do something useful
  211.             // when possible.
  212.             waiter.WaitOne ();
  213.             Console.WriteLine ("Ping example completed.");
  214.         }
  215.  
  216.         private static void PingCompletedCallback (object sender, PingCompletedEventArgs e)
  217.         {
  218.             // If the operation was canceled, display a message to the user.
  219.             if (e.Cancelled)
  220.             {
  221.                 Console.WriteLine ("Ping canceled.");
  222.  
  223.                 // Let the main thread resume.
  224.                 // UserToken is the AutoResetEvent object that the main thread
  225.                 // is waiting for.
  226.                 ((AutoResetEvent)e.UserState).Set ();
  227.             }
  228.  
  229.             // If an error occurred, display the exception to the user.
  230.             if (e.Error != null)
  231.             {
  232.                 Console.WriteLine ("Ping failed:");
  233.                 Console.WriteLine (e.Error.ToString ());
  234.  
  235.                 // Let the main thread resume.
  236.                 ((AutoResetEvent)e.UserState).Set();
  237.             }
  238.  
  239.             PingReply reply = e.Reply;
  240.  
  241.             DisplayReply (reply);
  242.  
  243.             // Let the main thread resume.
  244.             ((AutoResetEvent)e.UserState).Set();
  245.         }
  246.  
  247.         public static void DisplayReply (PingReply reply)
  248.         {
  249.             if (reply == null)
  250.                 return;
  251.  
  252.             Console.WriteLine ("ping status: {0}", reply.Status);
  253.             if (reply.Status == IPStatus.Success)
  254.             {
  255.                 Console.WriteLine("Address: {0}", reply.Address.ToString());
  256.                 Console.WriteLine("RoundTrip time: {0}", reply.RoundtripTime);
  257.                 Console.WriteLine("Time to live: {0}", reply.Options.Ttl);
  258.                 Console.WriteLine("Don't fragment: {0}", reply.Options.DontFragment);
  259.                 Console.WriteLine("Buffer size: {0}", reply.Buffer.Length);
  260.             }
  261.         }
  262.     }
  263. }
  264.  
  265.  
  266.  
  267.  
  268.  
  269.  
  270.  
  271. using System;
  272. using System.Collections.Generic;
  273. using System.Linq;
  274. using System.Text;
  275. using Lib.Network.PingIP;
  276. using Brendan.Program;
  277.  
  278.  
  279. namespace Lib.System.Command
  280. {
  281.    
  282.     class UserCommand
  283.     {
  284.        
  285.         public static void Invalid() {}
  286.         public static void Login() {}
  287.         public static void Logout() {}
  288.         public static void Username() {}
  289.         public static void Ping(string uInput)
  290.         {
  291.             //Console.Write("Enter Hostname/IP: ");
  292.             //PingStart.PingIP(Console.ReadLine());
  293.             PingStart.PingIP(uInput);
  294.  
  295.         }
  296.         public static void CLS() {}
  297.         public static void RDP() {}
  298.         public static void Exit() {}
  299.     }
  300. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement