用于将 IP 地址字符串转换为十六进制格式的 Bash 脚本
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6149238/
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
Bash script for converting IP Addr string to Hexadecimal format
提问by Santhosh
IP_ADDR=192.168.1.128
IP_ADDR=192.168.1.128
printf '%02X' ${IP_ADDR//./ }; echo
printf '%02X' ${IP_ADDR//./ }; echo
Can some one explain how this simple oneliner converts IP_ADDR to hexadecimal format? I am banging my head trying to find some documentation about this behavior.
有人可以解释一下这个简单的oneliner如何将IP_ADDR转换为十六进制格式吗?我正在努力寻找有关这种行为的文档。
采纳答案by Ignacio Vazquez-Abrams
$ IP_ADDR=192.168.1.128
$ echo ${IP_ADDR//./ }
192 168 1 128
$ printf '%02X' 192 168 1 128 ; echo
C0A80180
回答by Benjamin Drung
You can simply use gethostip(from syslinux-utils on Debian/Ubuntu):
您可以简单地使用gethostip(来自 Debian/Ubuntu 上的 syslinux-utils):
$ gethostip -x 192.168.1.128
C0A80180

