下面的代码是我打印出IP
private static void getIp() throws Exception {
Enumeration<NetworkInterface> enumeration = NetworkInterface.getNetworkInterfaces();
while (enumeration.hasMoreElements()) {
NetworkInterface networkInterface = enumeration.nextElement();
Enumeration<InetAddress> addressEnumeration = networkInterface.getInetAddresses();
while (addressEnumeration.hasMoreElements()) {
InetAddress netAddress = addressEnumeration.nextElement();
String host = netAddress.getHostAddress();
String name = netAddress.getHostName();
System.err.println(networkInterface.isUp() + " " + name + " " + host);
}
}
}但是很奇怪,得到的eth1的isUp()状态总是false。
[root@mysql_master ~]# java -jar a.jar
false mysql_master 192.168.144.196
true fe80:0:0:0:e205:c5ff:fe72:ee7d%2 fe80:0:0:0:e205:c5ff:fe72:ee7d%2
true 192.168.144.59 192.168.144.59
true localhost6.localdomain6 0:0:0:0:0:0:0:1%1
true 127.0.0.1 127.0.0.1
[root@mysql_master ~]# ifconfig
eth0 Link encap:Ethernet HWaddr E0:05:C5:72:EE:7D
inet addr:192.168.144.59 Bcast:192.168.144.255 Mask:255.255.255.0
inet6 addr: fe80::e205:c5ff:fe72:ee7d/64 Scope:Link
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:5375 errors:0 dropped:0 overruns:0 frame:0
TX packets:6539 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:373935 (365.1 KiB) TX bytes:9269688 (8.8 MiB)
Interrupt:233 Base address:0x2000 eth1 Link encap:Ethernet HWaddr 6C:F0:49:C7:3D:7D
inet addr:192.168.144.196 Bcast:192.168.144.255 Mask:255.255.255.0
UP BROADCAST MULTICAST MTU:1500 Metric:1
RX packets:0 errors:0 dropped:0 overruns:0 frame:0
TX packets:0 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:0 (0.0 b) TX bytes:0 (0.0 b)
Interrupt:50 Base address:0xc000 lo Link encap:Local Loopback
inet addr:127.0.0.1 Mask:255.0.0.0
inet6 addr: ::1/128 Scope:Host
UP LOOPBACK RUNNING MTU:16436 Metric:1
RX packets:2592 errors:0 dropped:0 overruns:0 frame:0
TX packets:2592 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:0
RX bytes:4168212 (3.9 MiB) TX bytes:4168212 (3.9 MiB)我看了API,是这样描述的
isUp() throws SocketException
返回网络接口是否已经开启并运行。
return
如果接口已经开启并运行,则返回 true。
Throws SocketException:
如果发生 I/O 错误。
since 1.6我确定192.168.144.196这个IP是通的,而且我用XShell连接的就是这个IP,但是为什么networkInterface.isUp()返回的是false呢,请问有人知道是什么原因吗?
private static void getIp() throws Exception {
Enumeration<NetworkInterface> enumeration = NetworkInterface.getNetworkInterfaces();
while (enumeration.hasMoreElements()) {
NetworkInterface networkInterface = enumeration.nextElement();
Enumeration<InetAddress> addressEnumeration = networkInterface.getInetAddresses();
while (addressEnumeration.hasMoreElements()) {
InetAddress netAddress = addressEnumeration.nextElement();
String host = netAddress.getHostAddress();
String name = netAddress.getHostName();
System.err.println(networkInterface.isUp() + " " + name + " " + host);
}
}
}但是很奇怪,得到的eth1的isUp()状态总是false。
[root@mysql_master ~]# java -jar a.jar
false mysql_master 192.168.144.196
true fe80:0:0:0:e205:c5ff:fe72:ee7d%2 fe80:0:0:0:e205:c5ff:fe72:ee7d%2
true 192.168.144.59 192.168.144.59
true localhost6.localdomain6 0:0:0:0:0:0:0:1%1
true 127.0.0.1 127.0.0.1
[root@mysql_master ~]# ifconfig
eth0 Link encap:Ethernet HWaddr E0:05:C5:72:EE:7D
inet addr:192.168.144.59 Bcast:192.168.144.255 Mask:255.255.255.0
inet6 addr: fe80::e205:c5ff:fe72:ee7d/64 Scope:Link
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:5375 errors:0 dropped:0 overruns:0 frame:0
TX packets:6539 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:373935 (365.1 KiB) TX bytes:9269688 (8.8 MiB)
Interrupt:233 Base address:0x2000 eth1 Link encap:Ethernet HWaddr 6C:F0:49:C7:3D:7D
inet addr:192.168.144.196 Bcast:192.168.144.255 Mask:255.255.255.0
UP BROADCAST MULTICAST MTU:1500 Metric:1
RX packets:0 errors:0 dropped:0 overruns:0 frame:0
TX packets:0 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:0 (0.0 b) TX bytes:0 (0.0 b)
Interrupt:50 Base address:0xc000 lo Link encap:Local Loopback
inet addr:127.0.0.1 Mask:255.0.0.0
inet6 addr: ::1/128 Scope:Host
UP LOOPBACK RUNNING MTU:16436 Metric:1
RX packets:2592 errors:0 dropped:0 overruns:0 frame:0
TX packets:2592 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:0
RX bytes:4168212 (3.9 MiB) TX bytes:4168212 (3.9 MiB)我看了API,是这样描述的
isUp() throws SocketException
返回网络接口是否已经开启并运行。
return
如果接口已经开启并运行,则返回 true。
Throws SocketException:
如果发生 I/O 错误。
since 1.6我确定192.168.144.196这个IP是通的,而且我用XShell连接的就是这个IP,但是为什么networkInterface.isUp()返回的是false呢,请问有人知道是什么原因吗?
[root@mysql_master ~]# service network status
配置设备:
lo eth0 eth1
当前的活跃设备:
lo eth0 eth1