1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273
|
/**
* Créer par Patrice le 29/09/2025
*/
package services;
import java.io.BufferedReader;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;
/**
* Une classe pour décrire un service Windows
*/
public class Service
{
private String nom;
private String alias;
private String type;
private String etat;
private String exitCodeWin;
private String exitCodeService;
private String checkPoint;
private String wait_hint;
private String pid;
private String flags;
public Service(String resultat)
{
String[] lignes = resultat.split("\n");
for (int i = 0; i < lignes.length; i++)
{
String ligne = lignes[i];
if (ligne.contains("SERVICE_NAME"))
{
nom = apresMarqueur(ligne, ":").trim();
continue;
}
if (ligne.contains("DISPLAY_NAME:"))
{
alias = apresMarqueur(ligne,
":").trim();
continue;
}
// TYPE :
if (ligne.contains("TYPE"))
{
type = apresMarqueur(ligne,
":").trim();
continue;
}
// STATE :
if (ligne.contains("STATE"))
{
etat = apresMarqueur(ligne,
":").trim();
i++;
ligne = lignes[i].trim();
etat += " " + ligne;
continue;
}
// WIN32_EXIT_CODE
if (ligne.contains("WIN32_EXIT_CODE"))
{
exitCodeWin = apresMarqueur(ligne,
":").trim();
continue;
}
// SERVICE_EXIT_CODE
if (ligne.contains("SERVICE_EXIT_CODE"))
{
exitCodeService = apresMarqueur(ligne,
":").trim();
continue;
}
// CHECKPOINT
if (ligne.contains("CHECKPOINT"))
{
checkPoint = apresMarqueur(ligne,
":").trim();
continue;
}
// WAIT_HINT
if (ligne.contains("WAIT_HINT"))
{
wait_hint = apresMarqueur(ligne,
":").trim();
continue;
}
// PID
if (ligne.contains("PID"))
{
pid = apresMarqueur(ligne,
":").trim();
continue;
}
// FLAGS
if (ligne.contains("FLAGS"))
flags = apresMarqueur(ligne,
":").trim();
}
}
/**
* Renvoie tout ce qui suit un marqueur donné (la première
* occurrence du
* marqueur).<br/>
* s Si le marqueur n'est pas trouvé, renvoie une chaîne vide.
*
* @param chaine La chaîne considérée.
* @param marqueur Le marqueur à utiliser.
* @return Ce qui suit le marqueur, une chaîne vide si le marqueur
* est absent.
*/
public static String apresMarqueur(String chaine, String marqueur)
{
int posMarqueur = chaine.indexOf(marqueur);
int debSuite = posMarqueur + marqueur.length();
if ((posMarqueur == -1) || (debSuite >= chaine.length()))
return "";
return chaine.substring(debSuite);
}
/**
* sauver un contenu texte dans un fichier (encodage UTF-8)
*
* @param fichier nom du fichier à écrire
* @param contenu texte à sauver dans le fichier
*/
public static void ecrireFichier(String fichier, String contenu)
{
try
{
sauverContenuTexte(contenu, fichier, "UTF-8");
} catch (IOException e)
{
e.printStackTrace();
}
}
/**
* Sauvegarde le texte dans le fichier dont le chemin est passé en
* paramètre.
*
* @param texte Le texte à sauvegarder.
* @param chemFic Le chemin du fichier dans lequel écrire.
* @param encodageFic Encodage à utiliser.
* @throws IOException En cas de problème d'accès au fichier.
*/
public static void sauverContenuTexte(String texte,
String chemFic, String encodageFic) throws IOException
{
texte = texte.replace("\n",
System.getProperty("line.separator"));
FileOutputStream ecrBin = null;
PrintWriter ecr = null;
try
{
ecrBin = new FileOutputStream(chemFic);
ecr = new PrintWriter(
new OutputStreamWriter(ecrBin, encodageFic));
ecr.write(texte);
ecrBin.getFD().sync();
ecr.close();
ecr = null;
} finally
{
if (ecr != null)
{
try
{
ecrBin.getFD().sync();
} catch (Throwable e)
{
}
try
{
ecr.close();
} catch (Throwable e)
{
}
}
}
}
@Override
public String toString()
{
return "nom = " + nom + "\nalias = " + alias
+ "\ntype = " + type + "\nétat = " + etat
+ "\nexit code Win = " + exitCodeWin
+ "\nexit code Service = " + exitCodeService
+ "\ncheckPoint = " + checkPoint
+ "\nwait_hint = " + wait_hint
+ "\npid = " + pid + "\nflags = " + flags;
}
public String getNom()
{
return nom;
}
public void setNom(String nom)
{
this.nom = nom;
}
/**
* @param args
*/
public static void main(String[] args)
{
String[] arguments = { "cmd.exe", "/c",
"sc queryex type=service state=all" };
List<Service> services = new ArrayList<Service>();
try
{
ProcessBuilder pb = new ProcessBuilder(arguments);
// on mélange les sorties du processus
pb = pb.redirectErrorStream(true);
Process p = pb.start();
InputStream is = p.getInputStream();
InputStreamReader isr = new InputStreamReader(is);
BufferedReader br = new BufferedReader(isr);
String ligne;
new StringBuilder(
"liste des services");
StringBuilder sb = new StringBuilder();
while (true)
{
ligne = br.readLine();
if (ligne == null)
break;
if ((ligne.length() != 0) && ligne.startsWith("SERVICE_NAME:"))
{
sb=new StringBuilder(ligne);
while(true)
{
ligne = br.readLine();
sb.append("\n" + ligne);
if (ligne.contains("FLAGS"))
break;
}
String resultat = sb.toString();
Service service = new Service(resultat);
services.add(service);
}
}
sb = new StringBuilder("Liste des services");
for (Service service : services)
{
sb.append("\n------------------------------\n");
sb.append(service);
}
String fichier = System.getProperty("user.dir") + "\\"
+ "services.txt";
System.out.println(
"Vous trouverez le résultat de ce programme dans le "
+ "fichier : "
+ fichier);
ecrireFichier(fichier, sb.toString());
} catch (IOException e)
{
e.printStackTrace();
}
}
} |
Partager