use of java.net.NetworkInterface in project teaTime by ancfdy.
the class AppSysMgr method getSysLocalIpAddress.
/**
* 获取手机IP地址
* @return String 手机IP地址
*/
public String getSysLocalIpAddress() {
String hostAddress = null;
try {
for (Enumeration<NetworkInterface> en = NetworkInterface.getNetworkInterfaces(); en.hasMoreElements(); ) {
NetworkInterface intf = en.nextElement();
for (Enumeration<InetAddress> enumIpAddr = intf.getInetAddresses(); enumIpAddr.hasMoreElements(); ) {
InetAddress inetAddress = enumIpAddr.nextElement();
if (!inetAddress.isLoopbackAddress()) {
hostAddress = inetAddress.getHostAddress();
}
}
}
} catch (SocketException e) {
e.printStackTrace();
AppLogMessageMgr.e("AppSysMgr-->>getSysLocalIpAddress", e.getMessage().toString());
}
AppLogMessageMgr.i("AppSysMgr-->>getSysLocalIpAddress", hostAddress);
return hostAddress;
}
use of java.net.NetworkInterface in project teaTime by ancfdy.
the class AppSysMgr method getSysLocalIpAddress.
/**
* 获取手机IP地址
* @return String 手机IP地址
*/
public String getSysLocalIpAddress() {
String hostAddress = null;
try {
for (Enumeration<NetworkInterface> en = NetworkInterface.getNetworkInterfaces(); en.hasMoreElements(); ) {
NetworkInterface intf = en.nextElement();
for (Enumeration<InetAddress> enumIpAddr = intf.getInetAddresses(); enumIpAddr.hasMoreElements(); ) {
InetAddress inetAddress = enumIpAddr.nextElement();
if (!inetAddress.isLoopbackAddress()) {
hostAddress = inetAddress.getHostAddress();
}
}
}
} catch (SocketException e) {
e.printStackTrace();
AppLogMessageMgr.e("AppSysMgr-->>getSysLocalIpAddress", e.getMessage().toString());
}
AppLogMessageMgr.i("AppSysMgr-->>getSysLocalIpAddress", hostAddress);
return hostAddress;
}
use of java.net.NetworkInterface in project cloudstack by apache.
the class ServerResourceBase method configure.
@Override
public boolean configure(final String name, Map<String, Object> params) throws ConfigurationException {
_name = name;
String publicNic = (String) params.get("public.network.device");
if (publicNic == null) {
publicNic = "xenbr1";
}
String privateNic = (String) params.get("private.network.device");
if (privateNic == null) {
privateNic = "xenbr0";
}
final String storageNic = (String) params.get("storage.network.device");
final String storageNic2 = (String) params.get("storage.network.device.2");
_privateNic = getNetworkInterface(privateNic);
_publicNic = getNetworkInterface(publicNic);
_storageNic = getNetworkInterface(storageNic);
_storageNic2 = getNetworkInterface(storageNic2);
if (_privateNic == null) {
s_logger.warn("Nics are not specified in properties file/db, will try to autodiscover");
Enumeration<NetworkInterface> nics = null;
try {
nics = NetworkInterface.getNetworkInterfaces();
if (nics == null || !nics.hasMoreElements()) {
throw new ConfigurationException("Private NIC is not configured");
}
} catch (final SocketException e) {
throw new ConfigurationException("Private NIC is not configured");
}
while (nics.hasMoreElements()) {
final NetworkInterface nic = nics.nextElement();
final String nicName = nic.getName();
// try {
if (//nic.isUp() &&
!nic.isVirtual() && !nicName.startsWith("vnif") && !nicName.startsWith("vnbr") && !nicName.startsWith("peth") && !nicName.startsWith("vif") && !nicName.startsWith("virbr") && !nicName.contains(":")) {
final String[] info = NetUtils.getNicParams(nicName);
if (info != null && info[0] != null) {
_privateNic = nic;
s_logger.info("Designating private to be nic " + nicName);
break;
}
}
// } catch (final SocketException e) {
// s_logger.warn("Error looking at " + nicName, e);
// }
s_logger.debug("Skipping nic " + nicName);
}
if (_privateNic == null) {
throw new ConfigurationException("Private NIC is not configured");
}
}
String[] infos = NetUtils.getNetworkParams(_privateNic);
if (infos == null) {
s_logger.warn("Incorrect details for private Nic during initialization of ServerResourceBase");
return false;
}
params.put("host.ip", infos[0]);
params.put("host.mac.address", infos[1]);
return true;
}
use of java.net.NetworkInterface in project cloudstack by apache.
the class ServerResourceBase method getNetworkInterface.
protected NetworkInterface getNetworkInterface(String nicName) {
s_logger.debug("Retrieving network interface: " + nicName);
if (nicName == null) {
return null;
}
if (nicName.trim().length() == 0) {
return null;
}
nicName = nicName.trim();
NetworkInterface nic;
try {
nic = NetworkInterface.getByName(nicName);
if (nic == null) {
s_logger.debug("Unable to get network interface for " + nicName);
return null;
}
return nic;
} catch (final SocketException e) {
s_logger.warn("Unable to get network interface for " + nicName, e);
return null;
}
}
use of java.net.NetworkInterface in project cloudstack by apache.
the class NetUtils method getDefaultHostIp.
public static String getDefaultHostIp() {
if (SystemUtils.IS_OS_WINDOWS) {
final Pattern pattern = Pattern.compile("\\s*0.0.0.0\\s*0.0.0.0\\s*(\\S*)\\s*(\\S*)\\s*");
try {
final Process result = Runtime.getRuntime().exec("route print -4");
final BufferedReader output = new BufferedReader(new InputStreamReader(result.getInputStream()));
String line = output.readLine();
while (line != null) {
final Matcher matcher = pattern.matcher(line);
if (matcher.find()) {
return matcher.group(2);
}
line = output.readLine();
}
} catch (final IOException e) {
s_logger.debug("Caught IOException", e);
}
return null;
} else {
NetworkInterface nic = null;
final String pubNic = getDefaultEthDevice();
if (pubNic == null) {
return null;
}
try {
nic = NetworkInterface.getByName(pubNic);
} catch (final SocketException e) {
return null;
}
String[] info = null;
try {
info = NetUtils.getNetworkParams(nic);
} catch (final NullPointerException ignored) {
s_logger.debug("Caught NullPointerException when trying to getDefaultHostIp");
}
if (info != null) {
return info[0];
}
return null;
}
}
Aggregations