shell转换IP

简介:

老大写的一个shell转换IP的 很好用 ,分享下!

#!/bin/bash
hexip=""
hextoip() {
    hex=$1
ip1=`printf "%d." 0x${hex:6:2}`
ip2=`printf "%d." 0x${hex:4:2}`
    ip3=`printf "%d." 0x${hex:2:2}`
ip4=`printf "%d" 0x${hex:0:2}`
ip=$ip1$ip2$ip3$ip4
hexip=$ip
#return $ip
}


iptohex() {
    IFS=.
    for str in $1
    do
        printf "%02X" $str
    done
}


iptoaddr ()
{
#exec < $1
#while read a
for a in `cat $1`
do
#sring=`curl -s  "http://ip138.com/ips138.asp?ip=${a}&action=2" |grep '<ul class="ul1"><li>' |awk -F'[><]+' '{print $5}'`
#sring=$(wget -q "http://www.ip138.com/ips.asp?ip=${a}&action=2" -O -| iconv -f gbk -t utf-8 | grep "数据" | awk -F"ul" '{print $3}' | awk -F"" '{print $1$2}' | awk -F"" '{print $2"\n"$3}' | head -1)
hex=`printf "%X" ${a}`
#echo $a $hex
hextoip $hex
#hexip=$?
#echo $a $hexip
sring=$(wget -q "http://ip138.com/ips138.asp?ip=${hexip}&action=2" -O -| grep '<ul class="ul1"><li>' |awk -F'[><]+' '{print $5}')
echo $a $hexip $sring
done
}


case $1 in
-f)
        shift
        iptoaddr $1
;;
-i)
        shift
        #sring=`curl -s  "http://ip138.com/ips138.asp?ip=${1}&action=2" |grep '<ul class="ul1"><li>' |awk -F'[><]+' '{print $5}'`
#sring=$(wget -q "http://ip138.com/ips138.asp?ip=${1}&action=2" -O -| iconv -f gbk -t utf-8 | grep "数据" | awk -F"ul" '{print $3}' | awk -F"" '{print $1$2}' | awk -F"" '{print $2"\n"$3}' | head -1)
#sring=$(wget -q "http://ip138.com/ips138.asp?ip=${1}&action=2" -O -| grep "数据" | awk -F"ul" '{print $3}' | awk -F"" '{print $1$2}' | awk -F"" '{print $2"\n"$3}' | head -1)
sring=$(wget -q "http://ip138.com/ips138.asp?ip=${1}&action=2" -O -| grep '<ul class="ul1"><li>' |awk -F'[><]+' '{print $5}')
        echo $1 $sring
;;
-h)
        shift
hex=`echo "ibase=10;obase=16;$1"|bc`
echo $hex
        hextoip $hex
;;
-x)
        shift
        iptohex $1
;;
*)
        echo "[Help]
        $0 need -f or -i
        -f ------- argument is a file
        -i ------- argument is a IP
[For example]:
        $0 -f filename
        $0 -i ipadress
        "
;;
esac











本文转自 chengxuyonghu 51CTO博客,原文链接:http://blog.51cto.com/6226001001/1583507,如需转载请自行联系原作者
目录
相关文章
|
3天前
|
运维 Shell
Shell脚本判断IP是否合法性(多种方法)
Shell脚本判断IP是否合法性(多种方法)
|
25天前
|
Shell
Shell一键检测本地存活ip是否存活
Shell一键检测本地存活ip是否存活
11 0
|
3月前
|
网络协议 Shell Linux
系统初始化shell,包括:挂载镜像、搭建yum、设置主机名及IP地址和主机名映射、配置动态IP、关闭防火墙和selinux
系统初始化shell,包括:挂载镜像、搭建yum、设置主机名及IP地址和主机名映射、配置动态IP、关闭防火墙和selinux
37 2
|
4月前
|
存储 Shell Perl
使用shell脚本从ifconfig命令结果中提取IP地址
使用shell脚本从ifconfig命令结果中提取IP地址
67 0
|
5月前
|
运维 网络协议 Shell
shell脚本批量配置多台主机静态ip
shell脚本批量配置多台主机静态ip
54 0
|
8月前
|
Shell Perl
获取slb日志里ip地址的shell脚本
获取slb日志里ip地址的shell脚本
46 1
|
8月前
|
运维 Shell
使用shell获取IP归属地与运营商信
使用shell获取IP归属地与运营商信
|
9月前
|
Shell Windows
shell实战案例-批量ping ip(多进程限制并发)
shell实战案例-批量ping ip(多进程限制并发)
260 0
|
监控 Shell
shell 获取 IP 位置
shell 获取 IP 位置
|
缓存 网络协议 Shell
看我如何使用 shell 来获取所有 KVM 虚拟机的 IP 地址
看我如何使用 shell 来获取所有 KVM 虚拟机的 IP 地址
438 0