Bash lo始终是ip a命令的第一个接口吗
因此,我有一个脚本,它用于更改Linux客户端的IP。我有一个奇怪的错误,我当前获取接口的方法在“eno1”没有分配IP的情况下使用“lo”。当前获取接口的方法是Bash lo始终是ip a命令的第一个接口吗,bash,interface,Bash,Interface,因此,我有一个脚本,它用于更改Linux客户端的IP。我有一个奇怪的错误,我当前获取接口的方法在“eno1”没有分配IP的情况下使用“lo”。当前获取接口的方法是 iface=$(ifconfig | head -1 | awk '{print $1}' | sed "s/://g") 现在我的问题是。我找到了一个新方法来获取默认接口,它的工作原理如下: iface=$(ip a | grep "2:" | cut -d ":" -f2 | xargs) 我需要知道当使用命令ip a 或者,
iface=$(ifconfig | head -1 | awk '{print $1}' | sed "s/://g")
现在我的问题是。我找到了一个新方法来获取默认接口,它的工作原理如下:
iface=$(ip a | grep "2:" | cut -d ":" -f2 | xargs)
我需要知道当使用命令ip a
或者,如果有人知道一种更通用的方法来获取接口,我会很高兴听到它。为了安全起见:
ip a | grep "^[0-9]:" | grep -v lo: | cut -d ":" -f2 | xargs
为了安全起见:
ip a | grep "^[0-9]:" | grep -v lo: | cut -d ":" -f2 | xargs
一般性意见:
I need to know if "lo" is always the interface on the position 1: when using the command ip a
实际上,是的。但除非有文件证明,否则不要指望这一点
Or if anyone knows a more generic method to get the interface, I would appreciate to hear it.
ip是正确的使用方法,当然是在ifconfig上。一般评论:
I need to know if "lo" is always the interface on the position 1: when using the command ip a
实际上,是的。但除非有文件证明,否则不要指望这一点
Or if anyone knows a more generic method to get the interface, I would appreciate to hear it.
ip是正确的使用方法,当然是在ifconfig上。如果它有帮助,我不知道,但是在中提到的
C
中获取接口名称并使用它生成您自己的命令?@Mihir感谢链接,但是如果我能在没有任何其他实用程序的情况下这样做,我不知道。如果它有帮助,我不知道,但是,如中所述,在C
中获取接口名称并使用它生成您自己的命令?@Mihir感谢您提供的链接,但如果不使用任何其他实用程序,我希望能够做到这一点。哦,我怎么能忘记grep-v,非常感谢。我想这会解决我的问题。哦,我怎么能忘记grep-v,谢谢。我想这会解决我的问题。