[英]Bash script to get all IP addresses
I am trying to write a bash
script to get all IP addresses on a server. 我正在尝试编写一个
bash
脚本来获取服务器上的所有IP地址。 The script should work on all major distros. 该脚本应适用于所有主要发行版。 Here is what I have:
这是我有的:
ifconfig | grep 'inet addr:' | awk {'print $2'}
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? 我怎么能先删除
addr:
前缀? Second, how I can exclude 127.0.0.1
? 第二,我如何排除
127.0.0.1
?
ifconfig
was obsoleted by ip
. ifconfig
被ip
淘汰了。 It also has the flag -o
that write outputs easy to parse. 它还有标志
-o
,写输出易于解析。 Use ip -4
to show only IPV4 addresses. 使用
ip -4
仅显示IPV4地址。 Note the simpler script, it already exclude the loopback address: 注意更简单的脚本,它已经排除了环回地址:
ip -o addr | awk '!/^[0-9]*: ?lo|link\/ether/ {print $2" "$4}'
Or if you don't want the networks: 或者如果您不想要网络:
ip -o addr | awk '!/^[0-9]*: ?lo|link\/ether/ {gsub("/", " "); print $2" "$4}'
There's no need for grep
. 没有必要
grep
。 Here's one way using awk
: 这是使用
awk
的一种方式:
List only addr: 仅列出地址:
ifconfig | awk -F "[: ]+" '/inet addr:/ { if ($4 != "127.0.0.1") print $4 }'
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", $1, address }'
Simply using hostname
you can get a list of all your IP addresses, using the -I
flag. 只需使用
hostname
您就可以使用-I
标志获取所有IP地址的列表。
ie 即
$ hostname --all-ip-addresses || hostname -I
10.10.85.100 10.20.85.100 10.30.85.100
whereas 而
$ hostname --ip-address || hostname -i
::1%1 127.0.0.1
Centos7 (k3.10.0) Centos7(k3.10.0)
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 ? 这是一个类似的脚本,我已经测试了获取ip范围的地址,但它很慢 - 有人可能会给出一个提示,如何加速这个? (the ip-range here is an example for to get all lines, who seems to be up - between Vancouver and Korea) :
(这里的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
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"
printf'===%s === \\ n'“$ ip”
whois "$ip" >> /home/$user/test001.txt
whois“$ ip”>> /home/$user/test001.txt
done
DONE
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个小时。
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(/\/.*/,"",$4); print $4}'
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 ($4 != "127.0.0.1") print $4 }'
192.168.82.134
138.225.11.92
138.225.11.2
To map IPs to hostnames, see this answer . 要将IP映射到主机名,请参阅此答案 。
Use grep -v to ignore 127.0.0.1 使用grep -v忽略127.0.0.1
ifconfig | grep 'inet addr:' | awk {'print $2'} | grep -v '127.0.0.1'
Use sed to edit out the 'addr:' 使用sed编辑'addr:'
ifconfig | grep 'inet addr:' | awk {'print $2'} | grep -v '127.0.0.1' | sed -e 's/addr://'
ifconfig | grep 'inet addr:' | awk {'print $2'} | awk 'BEGIN{FS=":"}{print $2}' | grep -v '127.0.0.1'
It's a very tricky solution but it works: 这是一个非常棘手的解决方案,但它有效:
ip a | awk ' !/[0-9]+\\: lo/ && /^[0-9]\\:/ || /inet / && !/127\\.0\\.0\\.1/ {print $2}'
Output: 输出:
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]+$//"
Output: 输出:
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. 我没有一台机器有几个非环回接口,我可以检查它,随时发布你的发现。
如果它只是你要删除的“addr:”字,我会使用sed
而不是像awk
一样
ifconfig | grep 'inet addr:' | awk {'print $2'}| grep -v 127.0.0.1 | sed -e 's/addr://'
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 $2'} | awk -F: {'print $2'} | 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脚本中的greps:
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"'
I would to introduce you to a command-line tool called OSQuery by Facebook which helps you get system info by making SQL-like queries. 我将向您介绍一个名为OSQuery by Facebook的命令行工具,它可以帮助您通过类似SQL的查询来获取系统信息。 For your case for instance, you would have enter;
例如,对于你的情况,你会输入;
osquery> select * from interface_addresses;
Which would output something like; 哪个会输出类似的东西;
interface = wlan0
address = 192.168.0.101
mask = 255.255.255.0
broadcast = 192.168.0.255
point_to_point =
Which I find a lot more neat and convenient. 我发现它更整洁方便。
ifconfig | grep 'inet addr:' | awk {'print $2'} | cut -d ":" -f 2
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.