Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Remotely checking Windows services in java

 
sadhu charan
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi good Afternoon,
I am trying to check some particular windows services running or stop remotely throw java program that program works fine in local system and give the out put but when i put Ip address of remote machine that is not showing anything thing but in command prompt it display the output of remote computer not in java program, My java program is like that

package com.sadhu;

import java.sql.*;
import java.io.*;
import java.text.*;
public class CheckServices
{
public static void main(String args[])
{
try
{
Process p=Runtime.getRuntime().exec("sc query ALG");

BufferedReader reader=new BufferedReader(new InputStreamReader(p.getInputStream()));

String line=reader.readLine();
while(line!=null)
{
if(line.trim().startsWith("STATE"))

{

if (line.trim().substring(line.trim().indexOf(":")+1,line.trim().indexOf(":")+4).trim().equals("1"))
System.out.println("Stopped");
else
if (line.trim().substring(line.trim().indexOf(":")+1,line.trim().indexOf(":")+4).trim().equals("2"))
System.out.println("Startting....");
else
if (line.trim().substring(line.trim().indexOf(":")+1,line.trim().indexOf(":")+4).trim().equals("3"))
System.out.println("Stopping....");
else
if (line.trim().substring(line.trim().indexOf(":")+1,line.trim().indexOf(":")+4).trim().equals("4"))
System.out.println("Running");

}
line=reader.readLine();
}

}

catch(IOException e1) { }



}
}


It works fine for Local system but not working for remote server , i put command line "sc \\43.45.46.100 query ALG" it display the status of Remote Services ,When i put "sc \\43.45.46.100 query ALG" in place of "sc query ALG" then it is not working or not showing anything


Thanks in Advance
SCP

 
Rob Spoor
Sheriff
Pie
Posts: 20510
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Did you escape the \\ in your Java String to \\\\?
 
sadhu charan
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Rob Spoor That is solved I did like this sc \\\\43.45.46.100 and it work fine , Thanks once again
 
Rob Spoor
Sheriff
Pie
Posts: 20510
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're welcome.
 
sadhu charan
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Rob Spoor It is working for Services which only contain one word like "ALG" but which window services contain space in middle like "SOS SIT Standby-38087" that is not working in this command like
sc \\\\43.45.46.100 query SOS SIT Standby-38087 or i did like sc \\\\?43.45.46.100 query SOS SIT Standby-38087 boths are not working can you help me ,
Thanks in advance
SCP
 
Campbell Ritchie
Sheriff
Posts: 48645
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Moving thread because it is too difficult for “beginning”.
 
Rob Spoor
Sheriff
Pie
Posts: 20510
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Quote the service name. The command is sc \\43.45.46.100 query "SOS SIT Standby-38087" so as a String it becomes this (escaping the \ and " characters):
But are you sure that's the service name, and not just the display name?
 
sadhu charan
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks to all it is working which is suggested by Rob Spoor

Thank you
 
Rob Spoor
Sheriff
Pie
Posts: 20510
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're welcome.
 
sravan padala
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The above code mentioned in the question is throwing the below error! Can any one help me out!

[SC] OpenSCManager FAILED 5:
 
Campbell Ritchie
Sheriff
Posts: 48645
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Don't know myself, but welcome to the Ranch
 
Rob Spoor
Sheriff
Pie
Posts: 20510
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Error code 5 means ERROR_ACCESS_DENIED. That means the user you are running the command as simply doesn't have enough rights to perform the operation.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic