Linux 获取所有 IP 地址的 Bash 脚本

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12523872/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 14:25:10  来源:igfitidea点击:

Bash script to get all IP addresses

linuxbashshell

提问by Justin

I am trying to write a bashscript to get all IP addresses on a server. The script should work on all major distros. Here is what I have:

我正在尝试编写一个bash脚本来获取服务器上的所有 IP 地址。该脚本应该适用于所有主要发行版。这是我所拥有的:

ifconfig | grep 'inet addr:' | awk {'print '}

Resulting in:

导致:

addr:10.1.2.3
addr:50.1.2.3
addr:127.0.0.1

How can I first remove the addr:prefix? Second, how I can exclude 127.0.0.1?

我怎样才能先删除addr:前缀?其次,我如何排除127.0.0.1

采纳答案by Steve

There's no need for grep. Here's one way using awk:

没有必要grep。这是使用的一种方法awk

List only addr:

仅列出地址:

ifconfig | awk -F "[: ]+" '/inet addr:/ { if ( != "127.0.0.1") print  }'

List device and addr:

列出设备和地址:

ifconfig | awk -v RS="\n\n" '{ for (i=1; i<=NF; i++) if ($i == "inet" && $(i+1) ~ /^addr:/) address = substr($(i+1), 6); if (address != "127.0.0.1") printf "%s\t%s\n", , address }'

回答by timoph

ifconfig | grep 'inet addr:' | awk {'print '} | cut -d ":" -f 2

回答by John3136

Use grep -v to ignore 127.0.0.1

使用 grep -v 忽略 127.0.0.1

ifconfig | grep 'inet addr:' | awk {'print '} | grep -v '127.0.0.1'

Use sed to edit out the 'addr:'

使用 sed 编辑掉“addr:”

ifconfig | grep 'inet addr:' | awk {'print '}  | grep -v '127.0.0.1' | sed -e 's/addr://'

回答by Marcus

ifconfig | grep 'inet addr:' | awk {'print $2'} | awk 'BEGIN{FS=":"}{print $2}' | grep -v '127.0.0.1'

ifconfig | grep 'inet addr:' | awk {'print $2'} | awk 'BEGIN{FS=":"}{print $2}' | grep -v '127.0.0.1'

回答by Pedro Lacerda

ifconfigwas obsoleted by ip. It also has the flag -othat write outputs easy to parse. Use ip -4to show only IPV4 addresses. Note the simpler script, it already exclude the loopback address:

ifconfig已被 淘汰ip。它还具有-o写入易于解析的输出的标志。用于ip -4仅显示 IPV4 地址。注意更简单的脚本,它已经排除了环回地址:

ip -o addr | awk '!/^[0-9]*: ?lo|link\/ether/ {print " "}'

Or if you don't want the networks:

或者,如果您不想要网络:

ip -o addr | awk '!/^[0-9]*: ?lo|link\/ether/ {gsub("/", " "); print " "}'

回答by dschinn1001

Here is a similiar script for what I have tested to get an ip-range of addresses, but it is slowly - somebody might give a hint, how to accelerate this ? (the ip-range here is an example for to get all lines, who seems to be up - between Vancouver and Korea) :

这是一个类似的脚本,用于我测试获得地址的 ip 范围,但它很慢 - 有人可能会提示,如何加速?(这里的 ip-range 是获取所有线路的示例,这些线路似乎在温哥华和韩国之间):

#!/bin/bash

for ip in {209..210}.{125..206}.{5..231}.{65..72} # any ip between 209.126.230.71 and 210.205.6.66

do

printf ' === %s ===\n' "$ip"

whois "$ip" >> /home/$user/test001.txt

done

#!/bin/bash

对于 {209..210}.{125..206}.{5..231}.{65..72} 中的 ip # 209.126.230.71 和 210.205.6.66 之间的任何 ip

printf ' === %s ===\n' "$ip"

whois "$ip" >> /home/$user/test001.txt

完毕

If this is too trivial or some mistake in it here, simply answer or comment.

如果这太琐碎或这里有一些错误,只需回答或评论。

This script would last until finish about 5 to 8 hours.

这个脚本会持续到完成大约 5 到 8 个小时。

回答by Acumenus

This is merely a distillation of several prior answers and comments. Sample output is included.

这仅仅是几个先前答案和评论的提炼。包括示例输出。

To list IPs:

要列出 IP:

Using ip:

使用ip

(Restricted to IPv4 and global)

(仅限于 IPv4 和全球)

$ /sbin/ip -4 -o addr show scope global | awk '{gsub(/\/.*/,"",); print }'
192.168.82.134
138.225.11.92
138.225.11.2

Using ifconfig:

使用ifconfig

(Excluding 127.0.0.1)

(不包括 127.0.0.1)

$ /sbin/ifconfig | awk -F "[: ]+" '/inet addr:/ { if ( != "127.0.0.1") print  }'
192.168.82.134
138.225.11.92
138.225.11.2

To map IPs to hostnames, see this answer.

要将 IP 映射到主机名,请参阅此答案

回答by Yaron

It's a very tricky solution but it works:

这是一个非常棘手的解决方案,但它有效:

ip a | awk ' !/[0-9]+\: lo/ && /^[0-9]\:/ || /inet / && !/127\.0\.0\.1/ {print $2}'

ip a | awk ' !/[0-9]+\: lo/ && /^[0-9]\:/ || /inet / && !/127\.0\.0\.1/ {print $2}'

Output:

输出:

eth0: 192.168.0.1/24

eth0: 192.168.0.1/24

Better yet:

更好的是:

ip a | awk ' !/[0-9]+\: lo/ && /^[0-9]\:/ || /inet / && !/127\.0\.0\.1/ {print $2}' | perl -i -pe "s/:\n/: /g;" -pe "s/\/[\d]+$//"

ip a | awk ' !/[0-9]+\: lo/ && /^[0-9]\:/ || /inet / && !/127\.0\.0\.1/ {print $2}' | perl -i -pe "s/:\n/: /g;" -pe "s/\/[\d]+$//"

Output:

输出:

eth0: 192.168.0.1

eth0: 192.168.0.1

I don't have a machine with several non loopback interfaces I can check it with, feel free to post your findings.

我没有一台带有多个非环回接口的机器,我可以检查它,请随时发布您的发现。

回答by Hola Soy Edu Feliz Navidad

if it is only the "addr:" word that you'd like to remove I would use sedinstead of awklike

如果它只是你想删除的“addr:”这个词,我会用sed而不是awk

ifconfig | grep 'inet addr:' | awk {'print '}| grep -v 127.0.0.1 | sed -e 's/addr://'

回答by Gerard ONeill

I'm always surprised to see people using sed and awk instead of perl.

我总是很惊讶地看到人们使用 sed 和 awk 而不是 perl。

But first, using both grep and awk with an extra option, feel free to just:

但首先,使用带有额外选项的 grep 和 awk,请随意:

ifconfig | grep 'inet addr:' | awk {'print '} | awk -F: {'print '} | grep -v '127.0.0.1'

replacing the awks with perl:

用 perl 替换 awks:

ifconfig | grep 'inet addr:' | perl -F\s\|: -ane 'print "$F[2]\n"' | grep -v '127.0.0.1'

replacing the greps within the same perl script:

在同一个 perl 脚本中替换 grep:

ifconfig | perl -F\s\|: -ane 'next if !/^inet addr:/ or /127\.0\.0\.1/; print "$F[2]\n"'

and lastly just using the power of perl's regex:

最后只是使用 perl 正则表达式的强大功能:

ifconfig | perl -ne 'next if !/inet addr:(?<ip>[0-9.]+)/ or $+{ip} == "127.0.0.1"; print "$+{ip}\n"'