2017-09-13 48 views
-3

我的代碼如何在Java中將arp -a掃描控制檯結果轉換爲表格格式?

public static void main(String args[]){ 
    try { 
     Process proc = Runtime.getRuntime().exec("arp -a "); 
     BufferedReader stdInput = new BufferedReader(new InputStreamReader(proc.getInputStream())); 
     BufferedReader stdError = new BufferedReader(new InputStreamReader(proc.getErrorStream())); 
     // read the output from the command 
     String ps = null; 
     while ((ps = stdInput.readLine()) != null) { 
      System.out.println(ps); 
      // read any errors from the attempted command 
      while ((ps = stdError.readLine()) != null) { 
       System.err.println(ps); 
      } 
     } 
    }catch (IOException ex) { 
     System.err.println(ex); 
    } 
} 

我的控制檯輸出

Interface: 192.168.201.1 --- 0x6 
Internet Address Physical Address Type 
192.168.201.2 48-88-ca-c8-87-4a static 
192.168.201.4 00-6f-64-be-6e-e8 static 
192.168.201.255 ff-ff-ff-ff-ff-ff static 
224.0.0.2 01-00-5e-00-00-02 static 
224.0.0.22 01-00-5e-00-00-16 static 
224.0.0.251 01-00-5e-00-00-fb static 
224.0.0.252 01-00-5e-00-00-fc static 
224.0.0.253 01-00-5e-00-00-fd static 
224.0.1.60 01-00-5e-00-01-3c static 
239.192.152.143 01-00-5e-40-98-8f static 
239.255.100.100 01-00-5e-7f-64-64 static 
239.255.255.250 01-00-5e-7f-ff-fa static 
239.255.255.253 01-00-5e-7f-ff-fd static 
255.255.255.255 ff-ff-ff-ff-ff-ff static 

我想這個輸出應該顯示在表format..please說我該怎麼辦呢。 回覆

+2

請了解如何使用降價。我這次修復了你的格式。至於你的請求_「說我怎麼做」_這不是如何StackOverflow的工作原理。我們不會爲你做你的工作。請訪問[幫助]並閱讀[問]以瞭解此處的主題。 –

+0

好嗎.....所以即使更換該行仍顯示輸出,而不表後給出的建議爲 –

回答

0

如果更換線

System.out.println(ps); 

與以下,

if (ps.matches("^(Internet\\s|\\d{1,3}\\.).*")) { 
    System.out.printf("%-18s %-18s %s\n", (Object[]) ps.split("\\s++(?!Address)")); 
} else { 
    System.out.println(ps); 
} 

輸出將是這樣的:

Interface: 192.168.201.1 --- 0x6 
Internet Address Physical Address Type 
192.168.201.2  48-88-ca-c8-87-4a static 
192.168.201.4  00-6f-64-be-6e-e8 static 
192.168.201.255 ff-ff-ff-ff-ff-ff static 
224.0.0.2   01-00-5e-00-00-02 static 
224.0.0.22   01-00-5e-00-00-16 static 
224.0.0.251  01-00-5e-00-00-fb static 
224.0.0.252  01-00-5e-00-00-fc static 
224.0.0.253  01-00-5e-00-00-fd static 
224.0.1.60   01-00-5e-00-01-3c static 
239.192.152.143 01-00-5e-40-98-8f static 
239.255.100.100 01-00-5e-7f-64-64 static 
239.255.255.250 01-00-5e-7f-ff-fa static 
239.255.255.253 01-00-5e-7f-ff-fd static 
255.255.255.255 ff-ff-ff-ff-ff-ff static 

詳見Format String Syntax

+0

... –

+0

不要你,我把在回答輸出?或者你想要別的東西嗎? – howlger

+0

是我沒有得到的輸出如表... –