Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 36 additions & 0 deletions src/main/java/com/mycmd/commands/PingCommand.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import java.io.BufferedReader;
import java.io.InputStreamReader;

public class PingCommand {
public static void main(String[] args) {
if (args.length < 1) {
System.out.println("Usage: java PingCommand <hostname>");
return;
}

String host = args[0];

try {
// Run the ping command
Process process = Runtime.getRuntime().exec("cmd.exe /c ping " + host);

// Read the output of the ping command
BufferedReader reader = new BufferedReader(
new InputStreamReader(process.getInputStream())
);

String line;
while ((line = reader.readLine()) != null) {
System.out.println(line);
}

// Wait for the process to finish
int exitCode = process.waitFor();
System.out.println("Ping command exited with code: " + exitCode);

} catch (Exception e) {
System.out.println("Error executing ping command.");
e.printStackTrace();
}
}
}