File tree Expand file tree Collapse file tree 1 file changed +36
-0
lines changed
src/main/java/com/mycmd/commands Expand file tree Collapse file tree 1 file changed +36
-0
lines changed Original file line number Diff line number Diff line change 1+ import java .io .BufferedReader ;
2+ import java .io .InputStreamReader ;
3+
4+ public class PingCommand {
5+ public static void main (String [] args ) {
6+ if (args .length < 1 ) {
7+ System .out .println ("Usage: java PingCommand <hostname>" );
8+ return ;
9+ }
10+
11+ String host = args [0 ];
12+
13+ try {
14+ // Run the ping command
15+ Process process = Runtime .getRuntime ().exec ("cmd.exe /c ping " + host );
16+
17+ // Read the output of the ping command
18+ BufferedReader reader = new BufferedReader (
19+ new InputStreamReader (process .getInputStream ())
20+ );
21+
22+ String line ;
23+ while ((line = reader .readLine ()) != null ) {
24+ System .out .println (line );
25+ }
26+
27+ // Wait for the process to finish
28+ int exitCode = process .waitFor ();
29+ System .out .println ("Ping command exited with code: " + exitCode );
30+
31+ } catch (Exception e ) {
32+ System .out .println ("Error executing ping command." );
33+ e .printStackTrace ();
34+ }
35+ }
36+ }
You can’t perform that action at this time.
0 commit comments