Javaからシェルコマンドを実行する方法
Javaでは、 `Runtime.getRuntime()。exec`を使って外部シェルコマンドを実行することができます:
p = Runtime.getRuntime().exec("host -t a " + domain); p.waitFor(); BufferedReader reader = new BufferedReader(new InputStreamReader(p.getInputStream())); String line = ""; while ((line = reader.readLine())!= null) { sb.append(line + "\n"); }
1. PINGの例
`ping`コマンドを実行して出力を出力する古典的な例です。
ExecuteShellComand.java
package com.mkyong.shell; import java.io.BufferedReader; import java.io.InputStreamReader; public class ExecuteShellComand { public static void main(String[]args) { ExecuteShellComand obj = new ExecuteShellComand(); String domainName = "google.com"; //in mac oxs String command = "ping -c 3 " + domainName; //in windows //String command = "ping -n 3 " + domainName; String output = obj.executeCommand(command); System.out.println(output); } private String executeCommand(String command) { StringBuffer output = new StringBuffer(); Process p; try { p = Runtime.getRuntime().exec(command); p.waitFor(); BufferedReader reader = new BufferedReader(new InputStreamReader(p.getInputStream())); String line = ""; while ((line = reader.readLine())!= null) { output.append(line + "\n"); } } catch (Exception e) { e.printStackTrace(); } return output.toString(); } }
出力
PING google.com (74.125.135.x): 56 data bytes 64 bytes from 74.125.135.x: icmp__seq=0 ttl=53 time=8.289 ms 64 bytes from 74.125.135.x: icmp__seq=1 ttl=53 time=7.733 ms 64 bytes from 74.125.135.x: icmp__seq=2 ttl=53 time=8.343 ms --- google.com ping statistics --- 3パケット送信、3パケット受信、0.0%パケット損失 往復分/平均/最大/標準偏差= 7.733/8.122/8.343/0.276ms ヌル
2.ホストの例
シェルコマンド `host -t a google.com`を実行してすべてのコマンドを取得する例
google.comに接続されているIPアドレス。その後、定期的に
式を使用してすべてのIPアドレスを取得して表示します。
__P.S “host”コマンドは、** nixシステムのみで利用可能です。
ExecuteShellComand.java
パッケージcom.mkyong.shell; import java.io.BufferedReader; import java.io.InputStreamReader; import java.util.ArrayList; import java.util.List; import java.util.regex.Matcher; import java.util.regex.Pattern; パブリッククラスExecuteShellComand { プライベート静的最終文字列IPADDRESS__PATTERN = "([01]?\\ d \\ d?2[0-4]\\ d | 25[0-5])" \\。([01]?\\ d \\ d?| 2[0-4]\\ d | 25[0-5]) "\\。([01]?\\ d \\ d?2[0-4]25[0-5]) "\\。([01]?\\ d \\ d?2[0-4]\\ d | 25[0-5])"; プライベート静的パターンパターン= Pattern.compile(IPADDRESS__PATTERN);プライベートスタティックMatcher matcher; public static void main(String[]args){ ExecuteShellComand obj = new ExecuteShellComand(); String domainName = "google.com"; String command = "ホスト-t a" + domainName;文字列出力= obj.executeCommand(コマンド); //System.out.println(output); List<String> list = obj.getIpAddress(output); if(list.size()> 0){System.out.printf( "%sにはアドレスがあります:%n"、domainName); for(String ip:list){System.out.println(ip); }} else {System.out.printf( "%sにはアドレスがありません。"、domainName); } } プライベート文字列executeCommand(文字列コマンド){ StringBuffer出力=新しいStringBuffer(); プロセスp; try {p = Runtime.getRuntime()。exec(command); p.waitFor(); BufferedReader reader =新しいBufferedReader(新しいInputStreamReader(p.getInputStream())); 文字列line = ""; while((line = reader.readLine())!= null){output.append(行 "\ n"); } } catch(例外e){e.printStackTrace();} } return output.toString(); } パブリックリスト<String> getIpAddress(String msg){ リスト<String> ipList =新しいArrayList <String>(); if(msg == null || msg.equals( ""))戻り値ipList; matcher = pattern.matcher(msg); while(matcher.find()){ipList.add(matcher.group(0)); } return ipList; } }
出力
google.comには住所があります: 74.125.135.x 74.125.135.x 74.125.135.x 74.125.135.x 74.125.135.x 74.125.135.x
===参考文献