在 Bash 中比较两个 IP 地址
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/25043883/
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
Comparing two IP Addresses in Bash
提问by CS3000911
I am trying to find if two ip addresses are the same or not. I admit that I am new with bash, but I see no reason this would not work:
我试图找出两个 IP 地址是否相同。我承认我是 bash 新手,但我认为没有理由这行不通:
if [[ "$IPAddress" != "$OLDIPAddress" ]]
then
echo "IP Not the Same"
else
echo "IP Same"
fi
For testing purposes, I have even hard coded the values for the two variables, and they still don't show up as the same. I know you don't always see your own typos, but I do not see why this would not work. Any ideas?
出于测试目的,我什至对两个变量的值进行了硬编码,但它们仍然没有显示为相同的值。我知道您并不总是看到自己的错别字,但我不明白为什么这行不通。有任何想法吗?
采纳答案by hek2mgl
While your command should work, you can use the simple test operator (just a single bracket). The advantage is that it will work with any (POSIX) shell. However, the [[
operator should work too.
虽然您的命令应该可以工作,但您可以使用简单的测试运算符(只需一个括号)。优点是它可以与任何(POSIX)shell 一起使用。但是,[[
操作员也应该工作。
Can you reproduce this little example? (Should output 'yes'):
你能重现这个小例子吗?(应该输出“是”):
IPAddress="127.0.0.1"
OLDIPAddress="127.0.0.1"
if [ "$IPAddress" != "$OLDIPAddress" ] ; then
echo "no"
else
echo "yes"
fi
回答by MagicHyman
Quick demo script for comparing differently formated ip addresses, "normal" vs. DHCP always 3 digit octets for instance. Hopefully a couple of useful tricks you can use for parsing and validation in addition to compares, and a catalyst for some more from the community. (This is my first post with code so hopefully it comes through ok - as always YMMV.)
用于比较不同格式的 ip 地址的快速演示脚本,例如,“正常”与 DHCP 总是 3 位八位字节。希望除了比较之外,您还可以使用一些有用的技巧进行解析和验证,并成为社区中更多技巧的催化剂。(这是我第一篇带有代码的帖子,所以希望它能通过 - 一如既往的 YMMV。)
#!/bin/bash
# demo.sh - parse, validate, compare ip addresses
# usage: demo.sh {ipaddress1} {ipaddress2}
#--- IP ADDRESS 1
#--- Is there anything besides digits and three dots?
#--- Parse out the octets
#--- There should be exactly 4 populated octets
#--- All 4 octets must not be > 255
[ "${1//[0-9]/}" != '...' ] && echo "invalid" && exit 1
O1=(${1//./ })
[ -z "${O1[3]}" -o -n "${O1[4]}" ] && echo "invalid" && exit 1
[ ${O1[0]} -gt 255 -o ${O1[1]} -gt 255 -o ${O1[2]} -gt 255 -o ${O1[3]} -gt 255 ] && echo "invalid" && exit 1
#--- IP ADDRESS 2
[ "${2//[0-9]/}" != '...' ] && echo "invalid" && exit 1
O2=(${2//./ })
[ -z "${O2[3]}" -o -n "${O2[4]}" ] && echo "invalid" && exit 1
[ ${O2[0]} -gt 255 -o ${O2[1]} -gt 255 -o ${O2[2]} -gt 255 -o ${O2[3]} -gt 255 ] && echo "invalid" && exit 1
#--- Numeric compares of each octet
echo "Comparing each octect"
if [ ${O1[0]} -eq ${O2[0]} ]
then
echo " ${O1[0]} == ${O2[0]}"
else
echo " ${O1[0]} != ${O2[0]}"
fi
if [ ${O1[1]} -eq ${O2[1]} ]
then
echo " ${O1[1]} == ${O2[1]}"
else
echo " ${O1[1]} != ${O2[1]}"
fi
if [ ${O1[2]} -eq ${O2[2]} ]
then
echo " ${O1[2]} == ${O2[2]}"
else
echo " ${O1[2]} != ${O2[2]}"
fi
if [ ${O1[3]} -eq ${O2[3]} ]
then
echo " ${O1[3]} == ${O2[3]}"
else
echo " ${O1[3]} != ${O2[3]}"
fi
#--- Numeric IP address compare
echo "Compare via long if"
if [ ${O1[0]} -eq ${O2[0]} -a ${O1[1]} -eq ${O2[1]} -a ${O1[2]} -eq ${O2[2]} -a ${O1[3]} -eq ${O2[3]} ]
then
echo " == "
else
echo " != "
fi
#--- Loop for numeric compare
echo "Compare via loop"
SAME="Y"
for I in 0 1 2 3
do
echo " loop ${I} compare ${O1[$I]} ${O2[$I]}"
[ ${O1[$I]} -ne ${O2[$I]} ] && SAME="" && break
done
echo " result"
if [ -n "${SAME}" ]
then
echo " == "
else
echo " != "
fi
exit
$ ./demo.sh 1.2.3.4 1.2.3
invalid
$ ./demo.sh 1.2.3.4 1.2.q.4
invalid
$ ./demo.sh 1.2.3.4 1.2.3.4.
invalid
$ ./demo.sh 1.2.3.4 1.2.3.4.5
invalid
$ ./demo.sh 1.2.3.4 1.2.300.4
invalid
$ ./demo.sh 1.02.3.004 01.2.003.4
Comparing each octect
1 == 01
02 == 2
3 == 003
004 == 4
Compare via long if
1.02.3.004 == 01.2.003.4
Compare via loop
loop 0 compare 1 01
loop 1 compare 02 2
loop 2 compare 3 003
loop 3 compare 004 4
result
1.02.3.004 == 01.2.003.4
$ ./demo.sh 1.02.3.004 01.2.030.4
Comparing each octect
1 == 01
02 == 2
3 != 030
004 == 4
Compare via long if
1.02.3.004 != 01.2.030.4
Compare via loop
loop 0 compare 1 01
loop 1 compare 02 2
loop 2 compare 3 030
result
1.02.3.004 != 01.2.030.4
$