Wednesday, March 10, 2010

Quick Console App to Ping a Server

This is based mostly off of something in the Microsoft documentation. It's just a quick hack to let me know when a remote server comes back up.

static void Main(string[] args)
{
Ping pingSender = new Ping();
PingOptions options = new PingOptions();
// Create a buffer of 32 bytes of data to be transmitted.
string data = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa";
byte[] buffer = Encoding.ASCII.GetBytes(data);
int timeout = 120;
//PingReply reply = pingSender.Send(args[0], timeout, buffer, options);
PingReply reply = pingSender.Send("server.domain.com", timeout, buffer, options);
for (; ; )
{
if (reply.Status == IPStatus.Success)
{
Console.WriteLine("Address: {0}", reply.Address.ToString());
for (int i = 0; i < 5; i++) Console.WriteLine("\a");
// Or System.Media.SystemSounds.Beep.Play();
// Or Console.Beep();
break;
}
else
{
Console.WriteLine("Failed " + System.DateTime.Now);
Thread.Sleep(30000);
}
}
Console.ReadKey();
}