using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Lib.Network.PingIP;
using Lib.System.Command;
namespace Brendan
{
class Program
{
private const string prefixBeforeLogon = ">";
private const string prefixAfterLogon = "(BT):>>";
private const string uname = "Brendan";
private const string pwd = "password";
private enum EnumCommand
{
Invalid,
Exit,
Login,
Logout,
Username,
Ping,
CLS,
RDP
}
/// <summary>
/// Main program
/// </summary>
/// <param name="args">The args as string array.</param>
static void Main(string[] args)
{
var loggedIn = false;
Console.WriteLine("Welcome! Use #Exit to close this program.");
// Initial input
Console.Write(prefixBeforeLogon);
//var cCommand = GetCommand(Console.ReadLine());
string userInput = Console.ReadLine();
string[] userParams = userInput.Split(' ');
var cCommand = GetCommand(userParams[0]);
// Keep this program running untill user exits
while (cCommand != EnumCommand.Exit)
{
switch (cCommand)
{
case EnumCommand.Invalid:
UserCommand.Invalid();
Console.WriteLine("Invalid command. Use #Exit to quit!");
break;
case EnumCommand.Login:
UserCommand.Login();
Console.Write("Username: ");
if (Console.ReadLine() == uname)
{
Console.Write("Password: ");
}
else
{
Console.WriteLine("Invalid Username!");
}
if (Console.ReadLine() == pwd)
{
loggedIn = true;
}
else
{
Console.WriteLine("Incorrect Password!");
}
break;
case EnumCommand.Logout:
UserCommand.Logout();
Console.WriteLine("You are logged out!");
loggedIn = false;
break;
case EnumCommand.Username:
UserCommand.Username();
Console.WriteLine(loggedIn ? "Brendan" : "Please login first");
break;
default:
Console.WriteLine("Program error!");
break;
case EnumCommand.Ping:
UserCommand.Ping();
break;
case EnumCommand.RDP:
UserCommand.RDP();
System.Diagnostics.Process.Start("C:\\Windows\\System32\\mstsc.exe");
Console.WriteLine("Microsoft Remote Desktop Services Started!");
break;
case EnumCommand.CLS:
UserCommand.CLS();
Console.Clear();
break;
}
// Use of immidiate if - "statement ? if_true : if_false"
Console.Write(loggedIn ? prefixAfterLogon : prefixBeforeLogon);
// Get next command
//cCommand = GetCommand(Console.ReadLine());
userInput = Console.ReadLine();
userParams = userInput.Split(' ');
cCommand = GetCommand(userParams[0]);
}
}
/// <summary>
/// Returns the enum value of the user command.
/// </summary>
/// <param name="userInput">The user input as string.</param>
/// <returns></returns>
private static EnumCommand GetCommand(string userInput)
{
switch (userInput.ToUpperInvariant())
{
case "#EXIT":
return EnumCommand.Exit;
case "#LOGIN":
return EnumCommand.Login;
case "#LOGOUT":
return EnumCommand.Logout;
case "$USERNAME":
return EnumCommand.Username;
case "$PING":
return EnumCommand.Ping;
case "$CLS":
return EnumCommand.CLS;
case "$RDP":
return EnumCommand.RDP;
default:
return EnumCommand.Invalid;
}
}
}
}
using System;
using System.Text;
using System.Net;
using System.Net.NetworkInformation;
using System.ComponentModel;
using System.Threading;
namespace Lib.Network.PingIP
{
public class PingStart
{
public static void PingIP(string ipToPing)
{
if (ipToPing.Length == 0)
throw new ArgumentException ("Ping needs a host or IP Address.");
string who = ipToPing;
AutoResetEvent waiter = new AutoResetEvent (false);
Ping pingSender = new Ping ();
// When the PingCompleted event is raised,
// the PingCompletedCallback method is called.
pingSender.PingCompleted += new PingCompletedEventHandler (PingCompletedCallback);
// Create a buffer of 32 bytes of data to be transmitted.
string data = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa";
byte[] buffer = Encoding.ASCII.GetBytes (data);
// Wait 12 seconds for a reply.
int timeout = 12000;
// Set options for transmission:
// The data can go through 64 gateways or routers
// before it is destroyed, and the data packet
// cannot be fragmented.
PingOptions options = new PingOptions (64, true);
Console.WriteLine ("Time to live: {0}", options.Ttl);
Console.WriteLine ("Don't fragment: {0}", options.DontFragment);
// Send the ping asynchronously.
// Use the waiter as the user token.
// When the callback completes, it can wake up this thread.
pingSender.SendAsync(who, timeout, buffer, options, waiter);
// Prevent this example application from ending.
// A real application should do something useful
// when possible.
waiter.WaitOne ();
Console.WriteLine ("Ping example completed.");
}
private static void PingCompletedCallback (object sender, PingCompletedEventArgs e)
{
// If the operation was canceled, display a message to the user.
if (e.Cancelled)
{
Console.WriteLine ("Ping canceled.");
// Let the main thread resume.
// UserToken is the AutoResetEvent object that the main thread
// is waiting for.
((AutoResetEvent)e.UserState).Set ();
}
// If an error occurred, display the exception to the user.
if (e.Error != null)
{
Console.WriteLine ("Ping failed:");
Console.WriteLine (e.Error.ToString ());
// Let the main thread resume.
((AutoResetEvent)e.UserState).Set();
}
PingReply reply = e.Reply;
DisplayReply (reply);
// Let the main thread resume.
((AutoResetEvent)e.UserState).Set();
}
public static void DisplayReply (PingReply reply)
{
if (reply == null)
return;
Console.WriteLine ("ping status: {0}", reply.Status);
if (reply.Status == IPStatus.Success)
{
Console.WriteLine("Address: {0}", reply.Address.ToString());
Console.WriteLine("RoundTrip time: {0}", reply.RoundtripTime);
Console.WriteLine("Time to live: {0}", reply.Options.Ttl);
Console.WriteLine("Don't fragment: {0}", reply.Options.DontFragment);
Console.WriteLine("Buffer size: {0}", reply.Buffer.Length);
}
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Lib.Network.PingIP;
using Brendan.Program;
namespace Lib.System.Command
{
class UserCommand
{
public static void Invalid() {}
public static void Login() {}
public static void Logout() {}
public static void Username() {}
public static void Ping(string uInput)
{
//Console.Write("Enter Hostname/IP: ");
//PingStart.PingIP(Console.ReadLine());
PingStart.PingIP(uInput);
}
public static void CLS() {}
public static void RDP() {}
public static void Exit() {}
}
}