linux下获取IP地址的N种方法

第一种方法:ifconfig|grep inet |awk '{print $2}'|sed '2d'|awk -F : '{print $2}'

第二种方法:ifconfig|grep inet|sed -n '1p' |awk '{print $2}'|awk -F : '{print $2}'

第三种方法:ifconfig|grep "inet addr"|grep -v "127.0.0"|awk -F":" '{print $2}'|awk '{print $1}'|head -1

第四种方法:ifconfig eth0|grep -oE '([0-9]{1,3}\.?){4}'|head -n 1

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
获取Linux操作系统的IP地址和网关可以通过使用命令行工具来实现,以下是获取IP地址和网关的命令: 1. 获取IP地址: ``` ifconfig ``` 该命令可以列出网络接口信息,其中包括IP地址以及其他网络相关的信息。 2. 获取网关: ``` route -n ``` 该命令可以列出当前系统的路由表信息,其中包括网关的IP地址以及其他网络相关的信息。 如果你是使用Java程序来获取IP地址和网关的话,可以通过使用NetworkInterface和InetAddress类来实现,以下是示例代码: 获取IP地址: ```java import java.net.InetAddress; import java.net.NetworkInterface; import java.util.Enumeration; public class GetIPAddress { public static void main(String[] args) throws Exception { Enumeration<NetworkInterface> interfaces = NetworkInterface.getNetworkInterfaces(); while (interfaces.hasMoreElements()) { NetworkInterface iface = interfaces.nextElement(); if (iface.isLoopback() || !iface.isUp()) { continue; } Enumeration<InetAddress> addresses = iface.getInetAddresses(); while (addresses.hasMoreElements()) { InetAddress addr = addresses.nextElement(); if (addr.isLinkLocalAddress() || addr.isLoopbackAddress() || addr.isMulticastAddress()) { continue; } System.out.println("IP地址:" + addr.getHostAddress()); } } } } ``` 获取网关: ```java import java.net.InetAddress; import java.net.NetworkInterface; import java.net.SocketException; import java.util.Enumeration; public class GetGateway { public static void main(String[] args) throws SocketException { Enumeration<NetworkInterface> interfaces = NetworkInterface.getNetworkInterfaces(); while (interfaces.hasMoreElements()) { NetworkInterface iface = interfaces.nextElement(); Enumeration<InetAddress> inetAddresses = iface.getInetAddresses(); while (inetAddresses.hasMoreElements()) { InetAddress inetAddress = inetAddresses.nextElement(); if (!inetAddress.isLinkLocalAddress() && !inetAddress.isLoopbackAddress() && inetAddress instanceof java.net.Inet4Address) { Enumeration<NetworkInterface> subInterfaces = iface.getSubInterfaces(); while (subInterfaces.hasMoreElements()) { NetworkInterface subInterface = subInterfaces.nextElement(); Enumeration<InetAddress> subInetAddresses = subInterface.getInetAddresses(); while (subInetAddresses.hasMoreElements()) { InetAddress subInetAddress = subInetAddresses.nextElement(); if (!subInetAddress.isLinkLocalAddress() && !subInetAddress.isLoopbackAddress() && subInetAddress instanceof java.net.Inet4Address) { System.out.println("网关地址:" + subInetAddress.getHostAddress()); } } } } } } } } ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

wooowlili

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值