Programmers are lazy, you know.
Recently in development, you need to use the server's IP and Mac information. But the server is installed on the Linux system, for the multiple network, in the acquisition of IP generated a lot of problems. The following is a method for Java to obtain a local IP on a Windows system. Paste Code:
Package com.herman.test;
Import java.net.InetAddress;
/**
* @see get computer IP
* @author herman.xiong
* @date May 16, 2014 09:35:38/public
class Test {
public static void Main (string[] args) {
test0 ();
}
/**
* @see get IP on Windows system (single NIC)
* @author herman.xiong
* @date May 16, 2014 09:36:29/
Public
static void Test0 () {
try {
inetaddress addr = Inetaddress.getlocalhost ();
String ip=addr.gethostaddress (). ToString ()//obtains native IP
String address=addr.gethostname (). ToString ()//Get native name
System.out.println ("Obtain native IP:" +ip);
System.out.println ("Get Native Name:" +address);
} catch (Exception e) {
e.printstacktrace ();}}}
For more information, paste the code:
/**
* @see access to Windows System Card information
* @author herman.xiong
* @date May 16, 2014 10:17:30
*
* Suppresswarnings ("unchecked") public
static void Test1 () {
enumeration netinterfaces = null;
try {
netinterfaces = networkinterface.getnetworkinterfaces ();
while (Netinterfaces.hasmoreelements ()) {
NetworkInterface ni = (networkinterface) netinterfaces.nextelement ();
System.out.println ("DisplayName:" + ni.getdisplayname ());
System.out.println ("Name:" + ni.getname ());
Enumeration ips = Ni.getinetaddresses ();
while (Ips.hasmoreelements ()) {
System.out.println ("IP:" + (inetaddress) ips.nextelement ()). Gethostaddress ()) ;
}
}
} catch (Exception e) {
e.printstacktrace ();
}
}
Run Effect chart:
Well, look at the print above, you know, there are multiple IPs, and the situation on Linux is more complex. This troublesome situation, I ruled out, I used a new method, that is, the shell script on Linux. The syntax code is as follows:
#linux中的shell脚本的学习 (so Easy)
#1. Note
#在进行shell编程时, the sentence at the beginning of the # represents the comment until the end of the line.
#我们真诚地建议您在程序中使用注释. If you use annotations,
#那么即使相当长的时间内没有使用该脚本, you can understand how the script works in a very short period of time.
#2变量
#在其他编程语言中您必须使用变量. In shell programming, all variables are composed of strings, and you do not need to declare variables. To assign to a variable, you can write:
#变量名 = value
#取出变量值可以加一个美元符号 ($) in front of the variable:
#hello world
#!/bin/sh
#对变量赋值:
hw= " Hello World "
# now print variable HW content:
echo" Variable HW value is: "
echo $HW
Here's the shell script code to get the IP:
#!/bin/bash
#get Net export
network= ' cat/nac/config/nac_sys.conf | grep ' manager ' |awk ' {print $} '
# Get net export local IP
ifconfig $network |egrep "inet addr: |cut-d": "-f2|awk ' {print $} '
Script VI has been written, put a random position. Then Java calls, which is the Java in Linux to increase the use of Shell script command:
/**
* @see Execute script to get IP on linux
* @author herman.xiong
* @date May 16, 2014 10:33:23
* @return
* * public static string Execshell () {
string ip= "";
Gets the current program's running Process object
Runtime Runtime = Runtime.getruntime ();
Declaration processing class Object
process process = null;
return row Info
//input stream
inputstream is = null;
BYTE stream
InputStreamReader ISR = null;
Buffer stream
BufferedReader br = null;
Result
try {
//execute ping command
process = runtime.exec ("/var/script/herman.sh");
The instantiated input stream is
= Process.getinputstream ();
Convert input flow to byte stream
ISR = new InputStreamReader (is);
Reads text from bytes
br = new BufferedReader (ISR);
String line= "";
while (line = Br.readline ())!= null) {
ip+=line;
}
Is.close ();
Isr.close ();
Br.close ();
} catch (IOException e) {
e.printstacktrace ();
Runtime.exit (1);
}
return IP;
}
OK, it's all done.
Welcome everyone to pay attention to my blog, if you have questions, please add QQ group 135430763 to learn together.