欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

TCP/IP协议ip头解析

程序员文章站 2022-06-15 09:36:43
#!/bin/bashIPHEAD_HEXSTR=$1if [ $# -eq 0 ];then echo -e "Usage:$0 hexstr" exitfiif [ ${#IPHEAD_HEXSTR}%2 -ne 0 ];then echo -e "${IPHEAD_HEXSTR} error" exitfi#4位版本号HEX_VERSION_NO=${IPHEAD_HEXSTR:0:1}DEC_VERSION_NO=`echo "obase=1...
#!/bin/bash
#输入16进制ip头,解析ip头各字段

function bin_padd()
{
	HEX_STR=$1
	for((i=0;i<${#HEX_STR};i++))
	do
      BIN_STR_TMP=`echo "obase=2;ibase=16;${HEX_STR:${i}:1}" | bc`
      BIN_STR_TMP=`printf "%04s" ${BIN_STR_TMP} | sed "s/ /0/g"`
      BIN_STR=`printf "%s%s" ${BIN_STR} ${BIN_STR_TMP}`
	done
	
	echo ${BIN_STR}
}
IPHEAD_HEXSTR=`echo "$1" | tr "a-z" "A-Z"`
if [ $# -eq 0 ];then
    echo -e "Usage:$0 hexstr"
    exit
fi

if [ $((${#IPHEAD_HEXSTR}%2)) -ne 0 ];then
    echo -e "${IPHEAD_HEXSTR} error"  
    exit
fi

#4位版本号
HEX_VERSION_NO=${IPHEAD_HEXSTR:0:1}
DEC_VERSION_NO=`echo "obase=10;ibase=16;${HEX_VERSION_NO}" | bc`

#4位首部长度
HEX_HEAD_LEN=${IPHEAD_HEXSTR:1:1}
DEC_HEAD_LEN=`echo "obase=10;ibase=16;${HEX_HEAD_LEN}" | bc`

#8位服务类型TOS
HEX_SERVICE_TYPE=${IPHEAD_HEXSTR:2:2}
BIN_SERVICE_TYPE=`bin_padd ${HEX_SERVICE_TYPE}`

#16位总长度
HEX_DATA_LEN=${IPHEAD_HEXSTR:4:4}
DEC_DATA_LEN=`echo "obase=10;ibase=16;${HEX_DATA_LEN}" | bc`

#16为标识
HEX_SEQ_NO=${IPHEAD_HEXSTR:8:4}
DEC_SEQ_NO=`echo "obase=10;ibase=16;${HEX_SEQ_NO}" | bc`

#3位标志+13位偏移
HEX_FLG_OFFSET=${IPHEAD_HEXSTR:12:4}
BIN_FLG_OFFSET=`bin_padd ${HEX_FLG_OFFSET}`
BIN_FLG=${BIN_FLG_OFFSET:0:3}
BIN_OFFSET=${BIN_FLG_OFFSET:3:16}
DEC_OFFSET=`echo "obase=10;ibase=2;${BIN_OFFSET}" | bc`

#8位生存时间TTL
HEX_TTL=${IPHEAD_HEXSTR:16:2}
DEC_TTL=`echo "obase=10;ibase=16;${HEX_TTL}" | bc`

#8位协议
HEX_PRO=${IPHEAD_HEXSTR:18:2}
BIN_PRO=`bin_padd ${HEX_PRO}`


#16位校验和
HEX_CHK_SUM=${IPHEAD_HEXSTR:20:4}

#32位源地址
HEX_SRC_IP=${IPHEAD_HEXSTR:24:8}
DEC_SRC_IP1=`echo "obase=10;ibase=16;${HEX_SRC_IP:0:2}" | bc`
DEC_SRC_IP2=`echo "obase=10;ibase=16;${HEX_SRC_IP:2:2}" | bc`
DEC_SRC_IP3=`echo "obase=10;ibase=16;${HEX_SRC_IP:4:2}" | bc`
DEC_SRC_IP4=`echo "obase=10;ibase=16;${HEX_SRC_IP:6:2}" | bc`

#32位目的地址
HEX_DES_IP=${IPHEAD_HEXSTR:32:8}
DEC_DES_IP1=`echo "obase=10;ibase=16;${HEX_DES_IP:0:2}" | bc`
DEC_DES_IP2=`echo "obase=10;ibase=16;${HEX_DES_IP:2:2}" | bc`
DEC_DES_IP3=`echo "obase=10;ibase=16;${HEX_DES_IP:4:2}" | bc`
DEC_DES_IP4=`echo "obase=10;ibase=16;${HEX_DES_IP:6:2}" | bc`

printf "4位版本号:[%d]|4位首部长度:[%d]|8位服务类型TOS:[%s]|16位总长度:[%d]\n" ${DEC_VERSION_NO} ${DEC_HEAD_LEN} ${BIN_SERVICE_TYPE} ${DEC_DATA_LEN}
printf "16为标识:[%d]|3位标志:[%s]|13位偏移:[%s]\n" ${DEC_SEQ_NO} ${BIN_FLG} ${DEC_OFFSET}
printf "8位生存时间TTL:[%d]|8位协议:[%s]|16位校验和:[%s]\n" ${DEC_TTL} ${BIN_PRO} ${HEX_CHK_SUM}
printf "32位源地址:%d.%d.%d.%d\n" ${DEC_SRC_IP1} ${DEC_SRC_IP2} ${DEC_SRC_IP3} ${DEC_SRC_IP4}
printf "32位目的地址:%d.%d.%d.%d\n" ${DEC_DES_IP1} ${DEC_DES_IP2} ${DEC_DES_IP3} ${DEC_DES_IP4}

运行:./iphead 4500002ebe5500007a1151acdeb77ee3c0a81223
4位版本号:[4]|4位首部长度:[5]|8位服务类型TOS:[00000000]|16位总长度:[46]
16为标识:[48725]|3位标志:[000]|13位偏移:[0]
8位生存时间TTL:[122]|8位协议:[00010001]|16位校验和:[51AC]
32位源地址:222.183.126.227
32位目的地址:192.168.18.35

本文地址:https://blog.csdn.net/CodeHouse/article/details/112792870

相关标签: TCP/IP IP头解析