How to Get List of IP addresses in BASH using hostname and tr co
- 时间:2020-09-17 11:25:43
- 分类:网络文摘
- 阅读:111 次
The hostname shows or sets the system’s hostname. The option ‘-I’ or ‘–all-ip-addresses’ list the IP addresses on one single line.
1 2 3 4 5 6 7 8 | -i, --ip-address Display the network address(es) of the host name. Note that this works only if the host name can be resolved. Avoid using this option; use hostname --all-ip-addresses instead. -I, --all-ip-addresses Display all network addresses of the host. This option enumerates all configured addresses on all network interfaces. The loopback inter- face and IPv6 link-local addresses are omitted. Contrary to option -i, this option does not depend on name resolution. Do not make any assumptions about the order of the output. |
-i, --ip-address
Display the network address(es) of the host name. Note that this works only if the host name can be resolved. Avoid using this option;
use hostname --all-ip-addresses instead.
-I, --all-ip-addresses
Display all network addresses of the host. This option enumerates all configured addresses on all network interfaces. The loopback inter-
face and IPv6 link-local addresses are omitted. Contrary to option -i, this option does not depend on name resolution. Do not make any
assumptions about the order of the output.For example,
1 2 | $ hostname -I 45.76.xx.xx 2001:19f0:7400:yy.yy.yy 12.34.55.33 |
$ hostname -I 45.76.xx.xx 2001:19f0:7400:yy.yy.yy 12.34.55.33
We can use the tr command to turn the one-line into multi-lines, by converting the spaces (i.e. ‘ ‘) into newline characters e.g. ‘\n’.
1 2 3 4 | $ hostname -I | tr ' ' '\n' 45.76.xx.xx 2001:19f0:7400:yy.yy.yy 12.34.55.33 |
$ hostname -I | tr ' ' '\n' 45.76.xx.xx 2001:19f0:7400:yy.yy.yy 12.34.55.33
tr can be quite useful to split the one-line string by converting the delimiters into newline. For example, if we want to split a IP address into four, we can do this:
1 2 3 4 5 | $ echo "1.2.3.4" | tr '.' \n 1 2 3 4 |
$ echo "1.2.3.4" | tr '.' \n 1 2 3 4

bash-shellshock
–EOF (The Ultimate Computing & Technology Blog) —
推荐阅读:三种适合春分时节吃的常见养生食物 电脑族需要多吃一些明目功能的食物 有些食物发芽后营养价值会变得更高 春季健康食谱:可以适当吃些新鲜蚕豆 多个品牌婴幼儿配方乳粉被曝配方都一样 新食品安全法“史上最严”建立惩罚制度 适合在夏天吃的既简单又美味的凉拌菜 薏米与红豆的多种搭配煮法可健脾养血 好时巧克力1年3上黑榜 违规使用维生素E 近些年被曝光的大米安全事件一览
- 评论列表
-
- 添加评论