0

my requirement is to update the down server host with other available server name in a table in db. This record will be used a windows service which installed on all the servers on the network to run a scheduled task. need help to get server host name which is down on a network i tried using ping, tcpclient and WMI and everytime when a server is down on my network getting below error:

Message: GetDataBaseHandleWithAccess - INNER Exception:System.ComponentModel.Win32Exception (0x80004005): The RPC server is unavailable

Message: GetDataBaseHandleWithAccessServer Health CheckupSystem.InvalidOperationException: Cannot open Service Control Manager on computer 'VW144444'. This operation might require other privileges. ---> System.ComponentModel.Win32Exception: The RPC server is unavailable --- End of inner exception stack trace ---

below are link which i referred and tried detect-if-machine-is-online-or-offline-using-wmi-and-c-sharp

csharp-check-if-machine-is-online-or-offline: using Ping Service

how-to-check-a-server-is-alive : using TcpClient

Community
  • 1
  • 1
  • private static bool IsMachineOnline(string hostName) { bool retVal = false; ManagementScope scope = new ManagementScope(string.Format(@"\\{0}\root\cimv2", hostName)); ManagementClass os = new ManagementClass(scope, new ManagementPath("Win32_OperatingSystem"), null); try { ManagementObjectCollection instances = os.GetInstances(); retVal = true; } catch (Exception ex) { retVal = false; Console.WriteLine(ex.Message); } return retVal; } – rajeev aditya May 18 '17 at 19:56
  • 1
    Please put the code into the question and not in a comment. It will be much more legible that way. – Evan Frisch May 18 '17 at 20:51

1 Answers1

0

I think you can use the exception itself to handle the logic.If the exception contains RPC server unavailable, you can consider it as a server not up. A better approach would be use the ping but ping can be disabled (it uses ICMP echo and ICMP protocol can be disabled by your network administrator) .So if it is internal domain servers,chances are that it will be enabled. So you can use combination of all three techniques Ping, TcpClient and WMI.

My first preference is using Ping

using System;
using System.Net;
using System.Net.NetworkInformation;
using System.Text;

namespace PingTest
{
public class PingExample
{
    // args[0] can be an IPaddress or host name.
    public static void Main (string[] args)
    {
        Ping pingSender = new Ping ();
        PingOptions options = new PingOptions ();

        // Use the default Ttl value which is 128,
        // but change the fragmentation behavior.
        options.DontFragment = true;

        // 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);
        if (reply.Status == IPStatus.Success)
        {

        }
    }
  }
}

And if you are checking some particular service is up or not e.g web service ,then use TcpClient to connect to the port this service is listening same SO question you linked How to check a server is alive?

Community
  • 1
  • 1
Rohith
  • 5,527
  • 3
  • 27
  • 31
  • i tried catching Win32Exception and even used an if condition to check the error code but both were not successful – rajeev aditya May 20 '17 at 11:58
  • Are you saying you are not able to catch the win32 exception? If that is the case ,you might have to do this https://msdn.microsoft.com/en-us/bb264489.aspx – Rohith May 20 '17 at 14:21
  • Hi Rohith , when i do it from console application or windows app im able to ping, the issue is when i use it windows service im not able to – rajeev aditya May 24 '17 at 14:51
  • I need to query the status of a service in a remote computer and I can't use a admin user, I need to create a user with the least privileges necessary to do that. Looking at the Service Security and Access Rights, I think a user with the SC_MANAGER_ENUMERATE_SERVICE and the SC_MANAGER_CONNECT rights will be sufficient. Am I right? How can I grant this access rights to a user/group? – rajeev aditya May 24 '17 at 21:00
  • Going WMI way ,you will run into the windows permissions issue as Windows will not allow anyone to enumerate the services .If you still want to go that route , These links might help https://stackoverflow.com/questions/3917477/granting-remote-user-non-admin-the-ability-to-enumerate-services-in-win32-serv – Rohith May 27 '17 at 16:22
  • Going WMI way ,you will run into the windows permissions issue as Windows will not allow anyone to enumerate the services .Windows 2003 SP1 and above remote users will only get default permission SC_MANAGER_CONNECT . This KB talks about this issue https://support.microsoft.com/en-us/help/907460/non-administrators-cannot-remotely-access-the-service-control-manager-after-you-install-windows-server-2003-service-pack-1 – Rohith May 27 '17 at 16:30
  • Thanks a lot for the help this issue got resolved now.. I missed checking WMI service it was not running due to i was not able catch the exception once i changed it to auto start and made run this was hadled. – rajeev aditya May 28 '17 at 17:03