Java ile konsoldan bir ip adresine ping ve telnet yapan basit bir kod.
Henüz ip format validasyonlarını eklememiş olsamda çalışan güzel bir uygulama.
Çalıştırdığınızda konsolda "Ping 10.1.1.10" yada "Telnet 10.1.1.10 22" şeklinde girmeniz yeterli.
package Tests;import java.io.BufferedReader;import java.io.IOException;import java.io.InputStreamReader;import java.io.PrintWriter;import java.net.InetAddress;import java.net.Socket;public class test1 {public static void main(String[] args) {System.out.println("Telecom Tools Menu\n");System.out.println("--------------------\n");System.out.println("Commands : [Ping <IP Address>] | [Telnet <IP Add>]\n");BufferedReader br = null;try {br = new BufferedReader(new InputStreamReader(System.in));while (true) {System.out.print("Enter Command : ");String input = br.readLine();if ("q".equals(input)) {System.out.println("Exit!");System.exit(0);}/* Ping Block Start */if(input.toLowerCase().contains("ping")){String[] CommandParts = input.split(" ");int ArrSize = CommandParts.length;if(ArrSize != 2){System.out.println("Wrong Command. Ex : Ping 10.1.1.10");} else if (ArrSize == 2) {String ip = CommandParts[1];InetAddress inet = InetAddress.getByName(ip);System.out.println("Sending Ping Request to " + ip);System.out.println(inet.isReachable(5000) ? "Host is reachable" : "Host is NOT reachable");}/* Ping Block End */ /* Telnet Block Start */} else if (input.toLowerCase().contains("telnet")){String[] CommandParts = input.split(" ");int ArrSize = CommandParts.length;if(ArrSize!=3){System.out.println("Wrong Command Format. Ex : Telnet 10.1.1.10 22");} else {String ip = CommandParts[1];Integer port = Integer.parseInt(CommandParts[2]) ;Socket pingSocket = null;PrintWriter out = null;BufferedReader in = null;try {pingSocket = new Socket(ip, port);out = new PrintWriter(pingSocket.getOutputStream(), true);in = new BufferedReader(new InputStreamReader(pingSocket.getInputStream()));} catch (IOException e) {return;}System.out.println(in.readLine());out.close();in.close();pingSocket.close();}} else {System.out.println("Command Not Found or Wrong Command Format");}/* Telnet Block End */}} catch (IOException e) {e.printStackTrace();} finally {if (br != null) {try {br.close();} catch (IOException e) {e.printStackTrace();}}}}}
Teşekkürler