본문

ICMP Ping 명령 로직(JAVA)

반응형
description)
OS(Window/Linux/Unix), IP(Ipv4/Ipv6)를 구분하여 ICMP Ping 명령을 내리는 로직

source) CommandPing.java
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
package com.source.common;
 
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Pattern;
 
/** 
 * CommandPing.java 
 * 
 * @author        Sung Han LIM 
 *
 */
 
public class CommandPing {
    private static String regexIPv4 = "^(25[0-5]|2[0-4]\\d|[0-1]?\\d?\\d)(\\.(25[0-5]|2[0-4]\\d|[0-1]?\\d?\\d)){3}$";
    private static String regexIPv6 = "^(([0-9a-fA-F]{1,4}:){7,7}[0-9a-fA-F]{1,4}|([0-9a-fA-F]{1,4}:){1,7}:|([0-9a-fA-F]{1,4}:){1,6}:[0-9a-fA-F]{1,4}|([0-9a-fA-F]{1,4}:){1,5}(:[0-9a-fA-F]{1,4}){1,2}|([0-9a-fA-F]{1,4}:){1,4}(:[0-9a-fA-F]{1,4}){1,3}|([0-9a-fA-F]{1,4}:){1,3}(:[0-9a-fA-F]{1,4}){1,4}|([0-9a-fA-F]{1,4}:){1,2}(:[0-9a-fA-F]{1,4}){1,5}|[0-9a-fA-F]{1,4}:((:[0-9a-fA-F]{1,4}){1,6})|:((:[0-9a-fA-F]{1,4}){1,7}|:)|fe80:(:[0-9a-fA-F]{0,4}){0,4}%[0-9a-zA-Z]{1,}|::(ffff(:0{1,4}){0,1}:){0,1}((25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9]).){3,3}(25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])|([0-9a-fA-F]{1,4}:){1,4}:((25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9]).){3,3}(25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9]))$";
    private static String regexIPv4andIPv6 = "^(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)|(([0-9a-fA-F]{1,4}:){7,7}[0-9a-fA-F]{1,4}|([0-9a-fA-F]{1,4}:){1,7}:|([0-9a-fA-F]{1,4}:){1,6}:[0-9a-fA-F]{1,4}|([0-9a-fA-F]{1,4}:){1,5}(:[0-9a-fA-F]{1,4}){1,2}|([0-9a-fA-F]{1,4}:){1,4}(:[0-9a-fA-F]{1,4}){1,3}|([0-9a-fA-F]{1,4}:){1,3}(:[0-9a-fA-F]{1,4}){1,4}|([0-9a-fA-F]{1,4}:){1,2}(:[0-9a-fA-F]{1,4}){1,5}|[0-9a-fA-F]{1,4}:((:[0-9a-fA-F]{1,4}){1,6})|:((:[0-9a-fA-F]{1,4}){1,7}|:)|fe80:(:[0-9a-fA-F]{0,4}){0,4}%[0-9a-zA-Z]{1,}|::(ffff(:0{1,4}){0,1}:){0,1}((25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9]).){3,3}(25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])|([0-9a-fA-F]{1,4}:){1,4}:((25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9]).){3,3}(25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9]))$";
    private static String OS = System.getProperty("os.name").toLowerCase();
    private static String OSValue;
    
    public static void main(String[] args) throws IOException {
        String ipAddress = null;
        String cmdResult = null;
        String packetSize = null
        String count = null;
        String os;
        
        ipAddress = "127.0.0.1";
        packetSize = "64"
        count = "3";
        
        os = checkOS();
 
        if (os.equals("WIN")) {
            cmdResult = cmdWindowsPing(packetSize, count, ipAddress);
        } else if (os.equals("LINX")) {
            cmdResult = cmdLinuxPing(packetSize, count, ipAddress);
        }
        
        System.out.println(cmdResult);
        
        
    }
    
    public static String cmdWindowsPing(String packetSize, String count, String ipAddress) throws IOException {
        Pattern pattern;
        Runtime runTime = Runtime.getRuntime();
        Process process;
        String cmdResult = "";
        String line;
        String param = "";
        
        if (ipAddress == null) {
            return "There is no ip address.";
        }
 
        pattern = Pattern.compile(regexIPv4andIPv6);
        if (ipAddress == null || pattern.matcher(ipAddress).matches() == false) {
            return "Does not fit on the IPv4 & IPv6 type.";
        } else {
            // IPv4
            pattern = Pattern.compile(regexIPv4);
            if (pattern.matcher(ipAddress).matches() == true) {
                param = "ping -n " + count + " -l " + packetSize + " " + ipAddress;
            }
 
            // IPv6
            pattern = Pattern.compile(regexIPv6);
            if (pattern.matcher(ipAddress).matches() == true) {
                param = "ping -6 -n " + count + " -l " + packetSize + " " + ipAddress;
            }
 
            try {
                process = runTime.exec("cmd /c chcp 437 & " + param);
 
                InputStream inputStream = process.getInputStream();
                InputStreamReader inputStreamReader = new InputStreamReader(inputStream);
                BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
 
                while ((line = bufferedReader.readLine()) != null) {
                    cmdResult += line + "\n";
                }
 
                inputStream.close();
            } catch (IOException e) {
                e.printStackTrace();
                return "FAIL";
            }
        }
        
        return cmdResult;
    }
    
    public static String cmdLinuxPing(String packetSize, String count, String ipAddress) throws IOException {
        Pattern pattern;
        List<String> commands = new ArrayList<String>();
 
        if (ipAddress == null) {
            return "There is no ip address.";
        }
 
        pattern = Pattern.compile(regexIPv4andIPv6);
        if (ipAddress == null || pattern.matcher(ipAddress).matches() == false) {
            return "Does not fit on the IPv4 & IPv6 type.";
 
        } else {
            // IPv4일 경우
            pattern = Pattern.compile(regexIPv4);
            if (pattern.matcher(ipAddress).matches() == true) {
                commands.add("ping");
                commands.add("-c");
                commands.add(count);
                commands.add("-s");
                commands.add(packetSize);
                commands.add(ipAddress);
            }
 
            // IPv6일 경우
            pattern = Pattern.compile(regexIPv6);
            if (pattern.matcher(ipAddress).matches() == true) {
                commands.add("ping6");
                commands.add("-c");
                commands.add(count);
                commands.add("-s");
                commands.add(packetSize);
                commands.add(ipAddress);
            }
        }
 
        // 명령 실행 영역 (S)
        ProcessBuilder pb = new ProcessBuilder(commands);
        Process process = pb.start();
        BufferedReader stdInput = new BufferedReader(new InputStreamReader(process.getInputStream()));
        String str = "";
        String cmdResult = "";
 
        try {
            while ((str = stdInput.readLine()) != null) {
                cmdResult += str + "\n";
            }
 
            if (cmdResult == "") {
                return "Network is unreachable";
 
            }
        } catch (Exception e) {
            e.printStackTrace();
            return "FAIL";
        }
        // 명령 실행 영역 (E)
 
        return cmdResult;
    }
 
    public static String checkOS() {
        String os = null;
        if (isWindows()) {
            setOSValue("win");
            return os = "WIN";
        }
        if (isUnix()) { // Unix and Linux 계열
            setOSValue("unix");
            return os = "LINX";
        }
 
        return os;
    }
 
    public String getOSValue() {
        return OSValue;
    }
 
    public static void setOSValue(String oSValue) {
        OSValue = oSValue;
    }
 
    public static boolean isWindows() {
        return (OS.indexOf("win">= 0);
    }
 
    public static boolean isUnix() {
        return (OS.indexOf("nix">= 0 || OS.indexOf("nux">= 0 || OS.indexOf("aix"> 0);
    }
}
 
 
cs

result)
1
2
3
4
5
6
7
8
9
10
11
12
13
Active code page: 437
 
Pinging 127.0.0.1 with 64 bytes of data:
Reply from 127.0.0.1: bytes=64 time<1ms TTL=128
Reply from 127.0.0.1: bytes=64 time<1ms TTL=128
Reply from 127.0.0.1: bytes=64 time<1ms TTL=128
 
Ping statistics for 127.0.0.1:
    Packets: Sent = 3, Received = 3, Lost = 0 (0% loss),
Approximate round trip times in milli-seconds:
    Minimum = 0ms, Maximum = 0ms, Average = 0ms
 
 
cs



반응형

공유

댓글