Shell脚本之IP网络地址和广播地址计算
2016-01-27 14:39
330 查看
这是一篇旧时习作,Shell脚本练得多了,想练练移位计算,就有了下面的脚本。
本文出自 “终南山下” 博客,谢绝转载!
#!/bin/bash IPPattern="(\<([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])\>\.){3}\<([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])\>" maskPattern="[1-9]|[12][0-9]|3[01]" while : do echo -e -n "Please input a legal IP address [such as 192.168.1.1/16]: " read userInput IPMask=`echo $userInput | egrep "$IPPattern/$maskPattern"` #Such as 192.168.1.1/24 if [ -z "$IPMask" ] ;then echo "Please input the right format. [such as 192.168.1.1/1-31] " continue fi IPAddr=`echo $IPMask | cut -d/ -f1` IPType[1]=`echo $IPAddr | cut -d. -f1` IPType[2]=`echo $IPAddr | cut -d. -f2` IPType[3]=`echo $IPAddr | cut -d. -f3` IPType[4]=`echo $IPAddr | cut -d. -f4` mask=`echo $IPMask | cut -d/ -f2` echo "IP address is ${IPType[1]}*${IPType[2]}*${IPType[3]}*${IPType[4]} , Mask is $mask ." ((IPHex[1]=IPType[1]<<24)) ((IPHex[2]=IPType[2]<<16)) ((IPHex[3]=IPType[3]<<8)) ((IPHex[4]=IPType[4])) #192 c0 ; 1 1 1 ((iph=${IPHex[1]}+${IPHex[2]}+${IPHex[3]}+${IPHex[4]})) #echo $iph #0xffffffff #declare -i strMask1=4294967295 declare -i strMask1=0xffffffff #echo $strMask1 ((strMask1=strMask1<<(32-mask) & 0xffffffff)) #echo $strMask1 ((strMask2=~strMask1)) #echo $strMask2 ((networkAddr=iph & strMask1)) ((bcastAddr= (iph | strMask2) & 0xffffffff)) #echo $networkAddr | awk '{printf "%x\n",$0}' #echo $bcastAddr | awk '{printf "%x\n",$0}' ((IPHex[1]=networkAddr>>24 & 0x000000ff)) ((IPHex[2]=networkAddr>>16 & 0x000000ff)) ((IPHex[3]=networkAddr>>8 & 0x000000ff)) ((IPHex[4]=networkAddr & 0x000000ff)) echo -e "Network Address : ${IPHex[1]}.${IPHex[2]}.${IPHex[3]}.${IPHex[4]}" ((IPHex[1]=bcastAddr>>24 & 0x000000ff)) ((IPHex[2]=bcastAddr>>16 & 0x000000ff)) ((IPHex[3]=bcastAddr>>8 & 0x000000ff)) ((IPHex[4]=bcastAddr & 0x000000ff)) echo -e "Broadcast Address : ${IPHex[1]}.${IPHex[2]}.${IPHex[3]}.${IPHex[4]}" done
本文出自 “终南山下” 博客,谢绝转载!
相关文章推荐
- java.lang.ClassNotFoundException: org.apache.axis2.transport.http.AxisServlet
- Jboss添加https支持
- 网络传输数据如何加密
- TCPdump抓包命令详解
- Wireshark和TcpDump抓包分析心得
- http 状态
- 学习搭建自己的网络MNIST在caffe上训练学习
- 产品经理的浓缩总结--转载自http://www.chanpin100.com/archives/32854
- 广州易幻网络科技有限公司Android笔面经 (未通过)
- HttpClient和HttpURLConnection的区别
- 他妈的 Python(1):怎么发起一个同步的 HTTP 请求
- 《2》IP+TCP协议的简单介绍
- http响应状态码大全
- iOS网络编程实践--蓝牙对等网络通信实例讲解
- 236,HTTP
- 《1》7层+4层网络模型
- 235,网络基础
- HttpClient教程
- [置顶] 【机器学习】神经网络-激活函数-面面观(Activation Function)
- 网络请求类NSURLConnection的使用