用于提取IP地址的Linux bash脚本

时间:2021-11-21 16:51:10

I want to make big script on my Debian 7.3 ( something like translated and much more new user friendly enviroment ). I have a problem. I want to use only some of the informations that commands give me. For example my ifconfig looks like:

我想在我的Debian 7.3上做一个大的脚本(类似于翻译和更多的新用户友好环境)。我有一个问题。我只想使用命令给出的一些信息。例如,我的ifconfig看起来如下:

eth0      Link encap:Ethernet  HWaddr 08:00:27:a3:e3:b0  
          inet addr:192.168.1.103  Bcast:192.168.1.255  Mask:255.255.255.0
          inet6 addr: fe80::a00:27ff:fea3:e3b0/64 Scope:Link
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:1904 errors:0 dropped:0 overruns:0 frame:0
          TX packets:2002 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000 
          RX bytes:1309425 (1.2 MiB)  T

I want to display only the IP address in line: echo "Your IP address is: (IP_ADDRESS )". Is there any command that allow me to do such a thing, to search in stream for informations I want to get?. I know about grep and sed but I am not really good with them.

我只想显示一行中的IP地址:echo“您的IP地址是:(IP_ADDRESS)”。有没有命令允许我做这样的事情,在流中搜索我想要的信息?我知道grep和sed,但是我不太擅长它们。

Edit: Firstly to say thank you for helping me with this problem, now I know much more. Secondly to say project is in progress. If anyone would be interested in it just pm me.

编辑:首先感谢你帮我解决这个问题,现在我知道的更多了。第二,项目正在进行中。如果有人对它感兴趣,请告诉我。

10 个解决方案

#1


23  

To just get your IP address:

获取你的IP地址:

echo `ifconfig eth0 2>/dev/null|awk '/inet addr:/ {print $2}'|sed 's/addr://'`

This will give you the IP address of eth0.

这会给你eth0的IP地址。

Edit: Due to name changes of interfaces in recent versions of Ubuntu, this doesn't work anymore. Instead, you could just use this:

编辑:由于最近版本的Ubuntu界面名称的改变,这已经不能工作了。相反,你可以用这个:

hostname --all-ip-addresses or hostname -I, which does the same thing (gives you ALL IP addresses of the host).

主机名——全IP地址或主机名-I,它做同样的事情(为您提供主机的所有IP地址)。

#2


69  

If the goal is to find the IP address connected in direction of internet, then this should be a good solution.

如果目标是在互联网的方向上找到连接的IP地址,那么这应该是一个很好的解决方案。

Find the IP and interface that is used:

查找所使用的IP和接口:

ip route get 8.8.8.8
8.8.8.8 via 10.10.10.1 dev eno1 src 192.168.3.33
    cache

Then to extract the IP:

然后提取IP:

ip route get 8.8.8.8 | awk 'NR==1 {print $NF}'
192.168.3.33

ip route does not open any connection out, it just shows the route needed to get to 8.8.8.8. 8.8.8.8 is Google's DNS.

ip路由没有打开任何连接,它只显示到达8.8.8.8的路由。8.8.8.8是谷歌的DNS。


Why other solution may fail:

为什么其他解决方案可能失败:

ifconfig eth0

ifconfig eth0

  • If the interface you have has another name (eno1, wifi, venet0 etc)
  • 如果您的接口有另一个名称(eno1、wifi、venet0等)
  • If you have more than one interface
  • 如果您有多个接口
  • IP connecting direction is not the first in a list of more than one IF
  • IP连接方向不是多个IF列表中的第一个

Hostname -I

主机名我

  • May get only the 127.0.1.1
  • 可能只得到127.0.1.1
  • Does not work on all systems.
  • 不能在所有系统上工作。

If you like to store this into a variable, do:

如果您想将其存储到一个变量中,请执行以下操作:

my_ip=$(ip route get 8.8.8.8 | awk 'NR==1 {print $NF}')

my_interface=$(ip route get 8.8.8.8 | awk '/dev/ {f=NR} f&&NR-1==f' RS=" ")

#3


29  

If you want to get a space separated list of your IPs, you can use the hostname command with the --all-ip-addresses (short -I) flag

如果您想获得一个独立的IPs列表,可以使用hostname命令和-all-ip地址(短-I)标志

hostname -I

as described here: Putting IP Address into bash variable. Is there a better way?

如本文所述:将IP地址放入bash变量。有更好的方法吗?

#4


7  

ip -4 addr show eth0 | grep -oP "(?<=inet ).*(?=/)"

#5


4  

Take your pick:

随便你挑,

$ cat file
eth0      Link encap:Ethernet  HWaddr 08:00:27:a3:e3:b0
          inet addr:192.168.1.103  Bcast:192.168.1.255  Mask:255.255.255.0
          inet6 addr: fe80::a00:27ff:fea3:e3b0/64 Scope:Link
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:1904 errors:0 dropped:0 overruns:0 frame:0
          TX packets:2002 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000
          RX bytes:1309425 (1.2 MiB)  T

$ awk 'sub(/inet addr:/,""){print $1}' file
192.168.1.103

$ awk -F'[ :]+' '/inet addr/{print $4}' file
192.168.1.103

#6


4  

  /sbin/ifconfig eth0 | grep 'inet addr:' | cut -d: -f2 | awk '{ print $1}'

#7


2  

Just a note, since I just spent some time trouble-shooting a botched upgrade on a server. Turned out, that (years ago) I had implemented a test to see if dynamically added interfaces (e.g. eth0:1) were present, and if so, I would bind certain proggis to the 'main' IP on eth0. Basically it was a variation on the 'ifconfig|grep...|sed... ' solution (plus checking for 'eth0:' presence).
The upgrade brought new net-tools, and with it the output has changed slightly:

我只是提醒一下,因为我刚刚花了一些时间在服务器上解决一个拙劣的升级。结果是,(几年前)我实现了一个测试,看看是否存在动态添加的接口(例如eth0:1),如果有,我会将某些proggis绑定到eth0上的“main”IP。基本上是ifconfig|grep…|sed…“溶液”(加上“eth0:”存在”检查)。这次升级带来了新的网络工具,与此同时,输出略有变化:

old ifconfig:

老ifconfig:

eth0      Link encap:Ethernet  HWaddr 42:01:0A:F0:B0:1D
          inet addr:10.240.176.29  Bcast:10.240.176.29  Mask:255.255.255.255
          UP BROADCAST RUNNING MULTICAST  MTU:1460  Metric:1
          ...<SNIP>

whereas the new version will display this:

而新版本将会显示:

eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1460
      inet 10.240.212.165  netmask 255.255.255.255  broadcast 10.240.212.165
      ...<SNIP>

rendering the hunt for 'eth0:' as well as 'inet addr:' search busted (never mind interfaces called 'em0','br0' or 'wlan0'...). Sure you could check for 'inet ' (or 'inet6'), and make the addr: part optional, but looking closer, you'll see that more or less everything has changed, 'Mask' is now 'netmask',...

呈现搜索“eth0:”以及“inet addr:”search busted(更别提叫做“em0”、“br0”或“wlan0”…)。当然,您可以检查“inet”(或“inet6”),并使addr: part optional部分,但仔细看,您会发现几乎所有的东西都发生了变化,“Mask”现在是“netmask”,……

The 'ip route ...' suggestion's pretty nifty - so maybe:

“ip路由……“建议很漂亮——所以也许:

_MyIP="$( ip route get 8.8.8.8 | awk 'NR==1 {print $NF}' )"
if [ "A$_MyIP" == "A" ]
then
    _MyIPs="$( hostname -I )"
    for _MyIP in "$_MyIPs"
    do
        echo "Found IP: \"$_MyIP\""
    done
else
    echo "Found IP: $_MyIP"
fi

Well, something of that sort anyway. Since all proposed solutions seem to have circumstances where they fail, check for possible edge cases - no eth, multiple eth's & lo's, when would 'hostname -i' fail,... and then decide on best solution, check it worked, otherwise 2nd best.

嗯,反正就是这样。由于所有提出的解决方案似乎都有失败的情况,请检查可能的边缘情况——不是eth,多重eth & lo,什么时候“主机名-i”会失败,……然后决定最好的解决方案,检查它是否有效,否则是第二好的。

Cheers 'n' beers!

欢呼“n”啤酒!

#8


2  

ip route get 8.8.8.8| grep src| sed 's/.*src \(.*\)$/\1/g'

#9


2  

May be not for all cases (especially if you have several NIC's), this will help:

可能不是所有的情况(特别是如果你有几个NIC),这将有助于:

hostname -I | awk '{ print $1 }'

#10


0  

In my opinion the simplest and most elegant way to achieve what you need is this:

在我看来,实现你所需要的最简单、最优雅的方法就是:

ip route get 8.8.8.8 | tr -s ' ' | cut -d' ' -f7

ip route get [host] - gives you the gateway used to reach a remote host e.g.:

ip路由获取[主机]-为您提供用于访问远程主机的网关,例如:

8.8.8.8 via 192.168.0.1 dev enp0s3  src 192.168.0.109

tr -s ' ' - removes any extra spaces, now you have uniformity e.g.:

tr -s ' -消除任何额外的空间,现在您拥有了一致性,例如:

8.8.8.8 via 192.168.0.1 dev enp0s3 src 192.168.0.109

cut -d' ' -f7 - truncates the string into ' 'space separated fields, then selects the field #7 from it e.g.:

cut -d' -f7 -将字符串截断为'space separated fields,然后从中选择field #7,例如:

192.168.0.109

#1


23  

To just get your IP address:

获取你的IP地址:

echo `ifconfig eth0 2>/dev/null|awk '/inet addr:/ {print $2}'|sed 's/addr://'`

This will give you the IP address of eth0.

这会给你eth0的IP地址。

Edit: Due to name changes of interfaces in recent versions of Ubuntu, this doesn't work anymore. Instead, you could just use this:

编辑:由于最近版本的Ubuntu界面名称的改变,这已经不能工作了。相反,你可以用这个:

hostname --all-ip-addresses or hostname -I, which does the same thing (gives you ALL IP addresses of the host).

主机名——全IP地址或主机名-I,它做同样的事情(为您提供主机的所有IP地址)。

#2


69  

If the goal is to find the IP address connected in direction of internet, then this should be a good solution.

如果目标是在互联网的方向上找到连接的IP地址,那么这应该是一个很好的解决方案。

Find the IP and interface that is used:

查找所使用的IP和接口:

ip route get 8.8.8.8
8.8.8.8 via 10.10.10.1 dev eno1 src 192.168.3.33
    cache

Then to extract the IP:

然后提取IP:

ip route get 8.8.8.8 | awk 'NR==1 {print $NF}'
192.168.3.33

ip route does not open any connection out, it just shows the route needed to get to 8.8.8.8. 8.8.8.8 is Google's DNS.

ip路由没有打开任何连接,它只显示到达8.8.8.8的路由。8.8.8.8是谷歌的DNS。


Why other solution may fail:

为什么其他解决方案可能失败:

ifconfig eth0

ifconfig eth0

  • If the interface you have has another name (eno1, wifi, venet0 etc)
  • 如果您的接口有另一个名称(eno1、wifi、venet0等)
  • If you have more than one interface
  • 如果您有多个接口
  • IP connecting direction is not the first in a list of more than one IF
  • IP连接方向不是多个IF列表中的第一个

Hostname -I

主机名我

  • May get only the 127.0.1.1
  • 可能只得到127.0.1.1
  • Does not work on all systems.
  • 不能在所有系统上工作。

If you like to store this into a variable, do:

如果您想将其存储到一个变量中,请执行以下操作:

my_ip=$(ip route get 8.8.8.8 | awk 'NR==1 {print $NF}')

my_interface=$(ip route get 8.8.8.8 | awk '/dev/ {f=NR} f&&NR-1==f' RS=" ")

#3


29  

If you want to get a space separated list of your IPs, you can use the hostname command with the --all-ip-addresses (short -I) flag

如果您想获得一个独立的IPs列表,可以使用hostname命令和-all-ip地址(短-I)标志

hostname -I

as described here: Putting IP Address into bash variable. Is there a better way?

如本文所述:将IP地址放入bash变量。有更好的方法吗?

#4


7  

ip -4 addr show eth0 | grep -oP "(?<=inet ).*(?=/)"

#5


4  

Take your pick:

随便你挑,

$ cat file
eth0      Link encap:Ethernet  HWaddr 08:00:27:a3:e3:b0
          inet addr:192.168.1.103  Bcast:192.168.1.255  Mask:255.255.255.0
          inet6 addr: fe80::a00:27ff:fea3:e3b0/64 Scope:Link
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:1904 errors:0 dropped:0 overruns:0 frame:0
          TX packets:2002 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000
          RX bytes:1309425 (1.2 MiB)  T

$ awk 'sub(/inet addr:/,""){print $1}' file
192.168.1.103

$ awk -F'[ :]+' '/inet addr/{print $4}' file
192.168.1.103

#6


4  

  /sbin/ifconfig eth0 | grep 'inet addr:' | cut -d: -f2 | awk '{ print $1}'

#7


2  

Just a note, since I just spent some time trouble-shooting a botched upgrade on a server. Turned out, that (years ago) I had implemented a test to see if dynamically added interfaces (e.g. eth0:1) were present, and if so, I would bind certain proggis to the 'main' IP on eth0. Basically it was a variation on the 'ifconfig|grep...|sed... ' solution (plus checking for 'eth0:' presence).
The upgrade brought new net-tools, and with it the output has changed slightly:

我只是提醒一下,因为我刚刚花了一些时间在服务器上解决一个拙劣的升级。结果是,(几年前)我实现了一个测试,看看是否存在动态添加的接口(例如eth0:1),如果有,我会将某些proggis绑定到eth0上的“main”IP。基本上是ifconfig|grep…|sed…“溶液”(加上“eth0:”存在”检查)。这次升级带来了新的网络工具,与此同时,输出略有变化:

old ifconfig:

老ifconfig:

eth0      Link encap:Ethernet  HWaddr 42:01:0A:F0:B0:1D
          inet addr:10.240.176.29  Bcast:10.240.176.29  Mask:255.255.255.255
          UP BROADCAST RUNNING MULTICAST  MTU:1460  Metric:1
          ...<SNIP>

whereas the new version will display this:

而新版本将会显示:

eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1460
      inet 10.240.212.165  netmask 255.255.255.255  broadcast 10.240.212.165
      ...<SNIP>

rendering the hunt for 'eth0:' as well as 'inet addr:' search busted (never mind interfaces called 'em0','br0' or 'wlan0'...). Sure you could check for 'inet ' (or 'inet6'), and make the addr: part optional, but looking closer, you'll see that more or less everything has changed, 'Mask' is now 'netmask',...

呈现搜索“eth0:”以及“inet addr:”search busted(更别提叫做“em0”、“br0”或“wlan0”…)。当然,您可以检查“inet”(或“inet6”),并使addr: part optional部分,但仔细看,您会发现几乎所有的东西都发生了变化,“Mask”现在是“netmask”,……

The 'ip route ...' suggestion's pretty nifty - so maybe:

“ip路由……“建议很漂亮——所以也许:

_MyIP="$( ip route get 8.8.8.8 | awk 'NR==1 {print $NF}' )"
if [ "A$_MyIP" == "A" ]
then
    _MyIPs="$( hostname -I )"
    for _MyIP in "$_MyIPs"
    do
        echo "Found IP: \"$_MyIP\""
    done
else
    echo "Found IP: $_MyIP"
fi

Well, something of that sort anyway. Since all proposed solutions seem to have circumstances where they fail, check for possible edge cases - no eth, multiple eth's & lo's, when would 'hostname -i' fail,... and then decide on best solution, check it worked, otherwise 2nd best.

嗯,反正就是这样。由于所有提出的解决方案似乎都有失败的情况,请检查可能的边缘情况——不是eth,多重eth & lo,什么时候“主机名-i”会失败,……然后决定最好的解决方案,检查它是否有效,否则是第二好的。

Cheers 'n' beers!

欢呼“n”啤酒!

#8


2  

ip route get 8.8.8.8| grep src| sed 's/.*src \(.*\)$/\1/g'

#9


2  

May be not for all cases (especially if you have several NIC's), this will help:

可能不是所有的情况(特别是如果你有几个NIC),这将有助于:

hostname -I | awk '{ print $1 }'

#10


0  

In my opinion the simplest and most elegant way to achieve what you need is this:

在我看来,实现你所需要的最简单、最优雅的方法就是:

ip route get 8.8.8.8 | tr -s ' ' | cut -d' ' -f7

ip route get [host] - gives you the gateway used to reach a remote host e.g.:

ip路由获取[主机]-为您提供用于访问远程主机的网关,例如:

8.8.8.8 via 192.168.0.1 dev enp0s3  src 192.168.0.109

tr -s ' ' - removes any extra spaces, now you have uniformity e.g.:

tr -s ' -消除任何额外的空间,现在您拥有了一致性,例如:

8.8.8.8 via 192.168.0.1 dev enp0s3 src 192.168.0.109

cut -d' ' -f7 - truncates the string into ' 'space separated fields, then selects the field #7 from it e.g.:

cut -d' -f7 -将字符串截断为'space separated fields,然后从中选择field #7,例如:

192.168.0.109