
1、连接上相应的linux主机,进入到等待输入shell指令的linux命令行状态下。
2、在linux命令行下输入shell指令:ip addr eth0。
3、键盘按“回车键”运行shell指令,此时会查询到eth0网卡的ip地址。。
查看本机ip地址方法:
*** 作环境:联想拯救者Y7000,windows10系统等。
1、首先打开控制面板,在窗口中,点击网络和Internet选项。
2、在网络和共享中心中,网络状态和任务,在下个页面中,就可以看见电脑接入的网络的名称。
3、点击连接旁边的网络名称,在跳出来的窗口中,点击详情信息,在这个页面中,就可以看见自己的IP地址了。
以安卓手机为例,查询ip地址查询定位的方法是:
*** 作环境:华为P40Pro手机、300202系统等。
1、首先打开手机设置应用,然后找到WLAN,点击该项进入详细页面。
2、接着在WLAN管理页面,则可以看到已经连接的WiFi网络。
3、其次点击该页面下方的配置,进入配置管理页面。
4、之后在配置管理页面,即可看到有IP地址的选项,则在选项的右侧即为本机的IP地址的查询定位。
import javaioBufferedReader;
import javaioIOException;
import javaioInputStreamReader;
/
与系统相关的一些常用工具方法
@author stephen
@version 100
/
public class SystemTool {
/
获取当前 *** 作系统名称
return *** 作系统名称 例如:windows xp,linux 等
/
public static String getOSName() {
return SystemgetProperty("osname")toLowerCase();
}
/
获取unix网卡的mac地址
非windows的系统默认调用本方法获取如果有特殊系统请继续扩充新的取mac地址方法
@return mac地址
/
public static String getUnixMACAddress() {
String mac = null;
BufferedReader bufferedReader = null;
Process process = null;
try {
process = RuntimegetRuntime()exec("ifconfig eth0");// linux下的命令,一般取eth0作为本地主网卡 显示信息中包含有mac地址信息
bufferedReader = new BufferedReader(new InputStreamReader(process
getInputStream()));
String line = null;
int index = -1;
while ((line = bufferedReaderreadLine()) != null) {
index = linetoLowerCase()indexOf("hwaddr");// 寻找标示字符串[hwaddr]
if (index >= 0) {// 找到了
mac = linesubstring(index +"hwaddr"length()+ 1)trim();// 取出mac地址并去除2边空格
break;
}
}
} catch (IOException e) {
eprintStackTrace();
} finally {
try {
if (bufferedReader != null) {
bufferedReaderclose();
}
} catch (IOException e1) {
e1printStackTrace();
}
bufferedReader = null;
process = null;
}
return mac;
}
/
获取widnows网卡的mac地址
@return mac地址
/
public static String getWindowsMACAddress() {
String mac = null;
BufferedReader bufferedReader = null;
Process process = null;
try {
process = RuntimegetRuntime()exec("ipconfig /all");// windows下的命令,显示信息中包含有mac地址信息
bufferedReader = new BufferedReader(new InputStreamReader(process
getInputStream()));
String line = null;
int index = -1;
while ((line = bufferedReaderreadLine()) != null) {
index = linetoLowerCase()indexOf("physical address");// 寻找标示字符串[physical address]
if (index >= 0) {// 找到了
index = lineindexOf(":");// 寻找":"的位置
if (index>=0) {
mac = linesubstring(index + 1)trim();// 取出mac地址并去除2边空格
}
break;
}
}
} catch (IOException e) {
eprintStackTrace();
} finally {
try {
if (bufferedReader != null) {
bufferedReaderclose();
}
} catch (IOException e1) {
e1printStackTrace();
}
bufferedReader = null;
process = null;
}
return mac;
}
/
测试用的main方法
@param argc
运行参数
/
public static void main(String[] argc) {
String os = getOSName();
Systemoutprintln(os);
if(osstartsWith("windows")){
//本地是windows
String mac = getWindowsMACAddress();
Systemoutprintln(mac);
}else{
//本地是非windows系统 一般就是unix
String mac = getUnixMACAddress();
Systemoutprintln(mac);
}
}
}
-------------------------------------------------------------------------
本程序可以正确获得本机IP地址和网卡"eth0"的MAC地址,已经在windowsXP和ubuntu-Linux上测试过
(注意:如果有多块网卡,可能出错)
下面给出代码:
import javanet;import javautil;
public class Test { public static void main(String[] args) { Test t = new Test(); Systemoutprintln(tgetLocalIP()); Systemoutprintln(tgetMacAddr()); }
public String getMacAddr() { String MacAddr = ""; String str = ""; try { NetworkInterface NIC = NetworkInterfacegetByName("eth0"); byte[] buf = NICgetHardwareAddress(); for (int i = 0; i < buflength; i++) { str = str + byteHEX(buf[i]); } MacAddr = strtoUpperCase(); } catch (SocketException e) { eprintStackTrace(); Systemexit(-1); } return MacAddr; }
public String getLocalIP() { String ip = ""; try { Enumeration<> e1 = (Enumeration<>) NetworkInterface getNetworkInterfaces(); while (e1hasMoreElements()) { NetworkInterface ni = (NetworkInterface) e1nextElement(); if (!nigetName()equals("eth0")) { continue; } else { Enumeration<> e2 = nigetInetAddresses(); while (e2hasMoreElements()) { InetAddress ia = (InetAddress) e2nextElement(); if (ia instanceof Inet6Address) continue; ip = iagetHostAddress(); } break; } } } catch (SocketException e) { eprintStackTrace(); Systemexit(-1); } return ip; }
/ 一个将字节转化为十六进制ASSIC码的函数 / public static String byteHEX(byte ib) { char[] Digit = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f' }; char[] ob = new char[2]; ob[0] = Digit[(ib >>> 4) & 0X0F]; ob[1] = Digit[ib & 0X0F]; String s = new String(ob); return s; }}
以上就是关于linux下如何查看某个网卡当前使用的IP。全部的内容,包括:linux下如何查看某个网卡当前使用的IP。、linux查看本机ip命令、有谁知道linux系统环境下,怎样在后台才能获取到mac地址等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)