在 bash 中使用 whois 查找提供商名称
Find out Provider name with whois in bash
TL;DR
我想以自动方式从 whois
的输出中提取提供者、范围开始、范围结束和提供者的名称,但输出因提供者而异,所以我需要帮助为任何提供商提取这三条信息。
详情
我有一个 IP 列表。我想借助 whois 为我的脚本检查每个 IP 的提供商,并想使用 mysql.
将其通过管道传输到数据库
我想用提供商的 ipranges 填充 mySQL Table,但前提是提供商的范围已经存在。
即IP=187.187.187.187
whois $IP
然后获取供应商名称和细分范围,但前提是我的 Table
中还没有它
我的计划应该是这样的:
function ip2dec ...
function dec2ip ...
function awhois (){ ...
THEPROVIDER=$(whois | grep PROVIDER) #<- i don't know how to grep
THERANGESTART=$(whois | grep START) #<- i don't know how to grep
THERANGEENDING=$(whois | grep END) #<- i don't know how to grep
}
while read line; do
DECIP=`ip2dec $line`
if [[ ! $(mysql -u$THEUSER -p$PASSWORD -h$THEHOST -e "select iprangestart, iprangeend from $DATABASE.$TABLE where $DECIP BETWEEN iprangestart and iprangeend" 2>/dev/null) ]];
then
awhois $line
mysql -u$THEUSER -p$PASSWORD -h$THEHOST -e
"INSERT INTO $DATABASE.$TABLE (iprangestart, iprangeend, provider)
VALUES ( \"$THERANGESTART\", \"$THERANGEENDING\", \"$THEPROVIDER\")" 2>/dev/null
fi
done < data/allips
但我真的不知道如何 grep 提供者和范围,因为提供者之间的模式不同
在 whois 的帮助下尝试了很多不同的方法来获取提供商名称后,我放弃了。并尝试 this 代替。
我不得不使用 wget,因为 curl 没有给我任何有用的东西。
这是最终结果:
function ip2dec(){ # Convert an IPv4 IP number to its decimal equivalent.
declare -i a b c d;
IFS=. read a b c d <<<"";
echo "$(((a<<24)+(b<<16)+(c<<8)+d))";
}
while read ipadd; do
DECIP=`ip2dec $ipadd`
if [[ $(mysql -N -u$THEUSER -p$PASSWORD -h$THEHOST -e "select providername from $DATABASE.$TABLE where $DECIP >= providerrangestart and $DECIP <= providerrangeende" 2>/dev/null| wc -l) -eq 0 ]];
then
INET=$(whois $ipadd | egrep "inetnum")
NETRAN=$(whois $ipadd | egrep "netrange")
ISPP=`wget https://www.whoismyisp.org/ip/$ipadd 2>/dev/null`
THEPROVIDER=`cat $ISPP | grep -oP -m1 '(?<=isp">).*(?=</p)' | cut -c -31`
if [[ $(echo "$THEPROVIDER" | egrep -i 'D2VODAFONE|Arcor|Vodafone D2') ]]; then
THEPROVIDER="Vodafone GmbH"
elif [[ $(echo "$THEPROVIDER" | egrep -i "Telefonica|Telefonica O2") ]]; then
THEPROVIDER="Deutsche Telekom AG"
elif [[ $(echo "$THEPROVIDER" | egrep -i "1&1 Versatel Deutschland GmbH|1&1 Internet SE") ]]; then
THEPROVIDER="1&1 GmbH"
fi
if [[ $INET ]];then
echo "new dump"
THERANGESTART=`echo $INET | awk '{print }'`
DECRANGESTART=`ip2dec $RANGESTART`
echo "Rangestart: $DECRANGESTART"
RANGEEND=`echo $INET | awk '{print }'`
DECRANGEEND=`ip2dec $RANGEEND`
echo "Rangeend: $RANGEEND"
echo "Provider: $THEPROVIDER"
echo ""
mysql -u$THEUSER -p$PASSWORD -h$THEHOST -e "INSERT INTO $DATABASE.$TABLE (providerrangestart, providerrangeende, providername) VALUES ( \"$DECRANGESTART\", \"$DECRANGEEND\", \"$THEPROVIDER\")" 2>/dev/null
elif [[ $NETRAN ]]; then
echo "new dump"
THERANGESTART=`echo $NETRAN | awk '{print }'`
DECRANGESTART=`ip2dec $RANGESTART`
echo "Rangestart: $RANGESTART"
RANGEEND=`echo $NETRAN | awk '{print }'`
DECRANGEEND=`ip2dec $RANGEEND`
echo "Rangeend: $RANGEEND"
echo "Provider: $THEPROVIDER"
echo ""
mysql -u$THEUSER -p$PASSWORD -h$THEHOST -e "INSERT INTO $DATABASE.$TABLE (providerrangestart, providerrangeende, providername) VALUES ( \"$DECRANGESTART\", \"$DECRANGEEND\", \"$THEPROVIDER\")" 2>/dev/null
else
echo "Failed to optain ISP and IP-Range"
echo -e "$ipadd \n"
fi
rm $ipadd
fi
done < data/allips
rm data/allips
TL;DR
我想以自动方式从 whois
的输出中提取提供者、范围开始、范围结束和提供者的名称,但输出因提供者而异,所以我需要帮助为任何提供商提取这三条信息。
详情
我有一个 IP 列表。我想借助 whois 为我的脚本检查每个 IP 的提供商,并想使用 mysql.
将其通过管道传输到数据库我想用提供商的 ipranges 填充 mySQL Table,但前提是提供商的范围已经存在。
即IP=187.187.187.187
whois $IP
然后获取供应商名称和细分范围,但前提是我的 Table
我的计划应该是这样的:
function ip2dec ...
function dec2ip ...
function awhois (){ ...
THEPROVIDER=$(whois | grep PROVIDER) #<- i don't know how to grep
THERANGESTART=$(whois | grep START) #<- i don't know how to grep
THERANGEENDING=$(whois | grep END) #<- i don't know how to grep
}
while read line; do
DECIP=`ip2dec $line`
if [[ ! $(mysql -u$THEUSER -p$PASSWORD -h$THEHOST -e "select iprangestart, iprangeend from $DATABASE.$TABLE where $DECIP BETWEEN iprangestart and iprangeend" 2>/dev/null) ]];
then
awhois $line
mysql -u$THEUSER -p$PASSWORD -h$THEHOST -e
"INSERT INTO $DATABASE.$TABLE (iprangestart, iprangeend, provider)
VALUES ( \"$THERANGESTART\", \"$THERANGEENDING\", \"$THEPROVIDER\")" 2>/dev/null
fi
done < data/allips
但我真的不知道如何 grep 提供者和范围,因为提供者之间的模式不同
在 whois 的帮助下尝试了很多不同的方法来获取提供商名称后,我放弃了。并尝试 this 代替。
我不得不使用 wget,因为 curl 没有给我任何有用的东西。
这是最终结果:
function ip2dec(){ # Convert an IPv4 IP number to its decimal equivalent.
declare -i a b c d;
IFS=. read a b c d <<<"";
echo "$(((a<<24)+(b<<16)+(c<<8)+d))";
}
while read ipadd; do
DECIP=`ip2dec $ipadd`
if [[ $(mysql -N -u$THEUSER -p$PASSWORD -h$THEHOST -e "select providername from $DATABASE.$TABLE where $DECIP >= providerrangestart and $DECIP <= providerrangeende" 2>/dev/null| wc -l) -eq 0 ]];
then
INET=$(whois $ipadd | egrep "inetnum")
NETRAN=$(whois $ipadd | egrep "netrange")
ISPP=`wget https://www.whoismyisp.org/ip/$ipadd 2>/dev/null`
THEPROVIDER=`cat $ISPP | grep -oP -m1 '(?<=isp">).*(?=</p)' | cut -c -31`
if [[ $(echo "$THEPROVIDER" | egrep -i 'D2VODAFONE|Arcor|Vodafone D2') ]]; then
THEPROVIDER="Vodafone GmbH"
elif [[ $(echo "$THEPROVIDER" | egrep -i "Telefonica|Telefonica O2") ]]; then
THEPROVIDER="Deutsche Telekom AG"
elif [[ $(echo "$THEPROVIDER" | egrep -i "1&1 Versatel Deutschland GmbH|1&1 Internet SE") ]]; then
THEPROVIDER="1&1 GmbH"
fi
if [[ $INET ]];then
echo "new dump"
THERANGESTART=`echo $INET | awk '{print }'`
DECRANGESTART=`ip2dec $RANGESTART`
echo "Rangestart: $DECRANGESTART"
RANGEEND=`echo $INET | awk '{print }'`
DECRANGEEND=`ip2dec $RANGEEND`
echo "Rangeend: $RANGEEND"
echo "Provider: $THEPROVIDER"
echo ""
mysql -u$THEUSER -p$PASSWORD -h$THEHOST -e "INSERT INTO $DATABASE.$TABLE (providerrangestart, providerrangeende, providername) VALUES ( \"$DECRANGESTART\", \"$DECRANGEEND\", \"$THEPROVIDER\")" 2>/dev/null
elif [[ $NETRAN ]]; then
echo "new dump"
THERANGESTART=`echo $NETRAN | awk '{print }'`
DECRANGESTART=`ip2dec $RANGESTART`
echo "Rangestart: $RANGESTART"
RANGEEND=`echo $NETRAN | awk '{print }'`
DECRANGEEND=`ip2dec $RANGEEND`
echo "Rangeend: $RANGEEND"
echo "Provider: $THEPROVIDER"
echo ""
mysql -u$THEUSER -p$PASSWORD -h$THEHOST -e "INSERT INTO $DATABASE.$TABLE (providerrangestart, providerrangeende, providername) VALUES ( \"$DECRANGESTART\", \"$DECRANGEEND\", \"$THEPROVIDER\")" 2>/dev/null
else
echo "Failed to optain ISP and IP-Range"
echo -e "$ipadd \n"
fi
rm $ipadd
fi
done < data/allips
rm data/allips