DOS command
| Command |
meaning |
| NET view |
Get all host names in the LAN |
| Ipconfig-all |
Get local IP, host name, MAC address |
| Arp-a |
Get all IP addresses and physical addresses on this local area network |
| Ping-a x.x.x.x |
Get host name for x.x.x.x |
| NBTSTAT-A Host Name |
Get MAC Address |
Java exec
Execute external command
"net view"new BufferedReader(new InputStreamReader(p .getInputStream()));
Extracting LAN IP
Public StaticList<string>getips() {list<string> List =NewArraylist<string> ();BooleanFlag =false;intCount=0; Runtime r = Runtime.getruntime (); Process p;Try{p = r.exec ("Arp-a"); BufferedReader br =NewBufferedReader (NewInputStreamReader (P. getInputStream ())); String inline; while(inline = Br.readline ())! =NULL) {if(Inline.indexof ("Interface") >-1) {flag =!flag;if(!flag) {//Hit the next "interface" Exit Loop Break; } }if(flag) {count++;if(Count >2){//Valid IPString[] Str=inline.split (' {4} '); List.add (str[0]); }} System.out.println (inline); } br.close (); }Catch(IOException e) {//TODO auto-generated catch blockE.printstacktrace (); } System.out.println (list);returnList }
Extract host names based on IP
Public StaticMap<string,string>Gethostnames(list<string> IPs) {Map<string,string> Map =NewHashmap<string,string> (); System.out.println ("extracting hostname ..."); for(String ip:ips) {String Command ="Ping-a"+ IP; Runtime r = Runtime.getruntime (); Process p;Try{p = r.exec (command); BufferedReader br =NewBufferedReader (NewInputStreamReader (P. getInputStream ())); String inline; while(inline = Br.readline ())! =NULL) {if(Inline.indexof ("[") >-1){intStart = Inline.indexof ("Ping");intEnd = Inline.indexof ("["); String hostname = inline.substring (start+"Ping". Length (), end-1); SYSTEM.OUT.PRINTLN (hostname); Map.put (Ip,hostname); }} br.close (); }Catch(IOException e) {//TODO auto-generated catch blockE.printstacktrace (); }} System.out.println ("The extraction is over!" ");returnMap }
Results
{ 222.26.28.51=BY-201507012043, 224.0.0.2=all-routers.mcast.net, 222.26.28.20=xxx-PC, 224.0.0.22=igmp.mcast.net, 222.26.28.218=xxx-PC, 222.26.28.223=xxx-PC}
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Java gets all host names and IP addresses in the LAN