文章详情

  • 游戏榜单
  • 软件榜单
关闭导航
热搜榜
热门下载
热门标签
php爱好者> php文档>Script to get network number and broadcast ad..

Script to get network number and broadcast ad..

时间:2004-10-13  来源:lightspeed

The script can get network number and broadcast addres from ip and mask.

$ ./1 192.168.0.1 255.255.255.0

Network number: 192.168.0.0
Broadcast address: 192.168.0.255
Netmask bits: 24
$
$ ./1 192.168.0.1 255.255.255.192

Network number: 192.168.0.0
Broadcast address: 192.168.0.63
Netmask bits: 26
$
$ ./1 192.168.0.1 255.255.255.191

255.255.255.191 is a bad netamsk with holes

$ ./1 10.0.0.1 252.0.0.0

Network number: 8.0.0.0
Broadcast address: 11.255.255.255
Netmask bits: 6



Ksh vresion:

#!/bin/ksh

set -A ip ${1//(D)/ }
set -A mask ${2//(D)/ }

for i in ${mask[*]}
do
j=7
tag=1
while [ $j -ge 0 ]
do
k=$((pow(2,$j)))
if [ $(( $i & $k )) -eq $k ]; then
if [ $tag -eq 1 ]; then
(( n++ ))
else
echo "n$2 is a bad netamsk with holesn"
exit
fi
else
tag=0
fi
(( j -- ))
done
done

for i in 0 1 2 3
do
a=$a${a:+.}$((${ip[i]} & ${mask[i]}))
b=$b${b:+.}$((${ip[i]} | (${mask[i]} ^ 255)))
done

echo
echo Network number: $a
echo Broadcast address: $b
echo Netmask bits: $n

# ---------- end -------------

Bash vresion:

#!/bin/bash

ip=(${1//[![:digit:]]/ })
mask=(${2//[![:digit:]]/ })

for i in ${mask[*]}
do
j=7
tag=1
while [ $j -ge 0 ]
do
k=$((2**$j))
if [ $(( $i & $k )) -eq $k ]; then
if [ $tag -eq 1 ]; then
(( n += 1 ))
else
echo -e "n$2 is a bad netamsk with holesn"
exit
fi
else
tag=0
fi
(( j -= 1 ))
done
done

for i in 0 1 2 3
do
a=$a${a:+.}$((${ip[i]} & ${mask[i]}))
b=$b${b:+.}$((${ip[i]} | (${mask[i]} ^ 255)))
done

echo
echo Network number: $a
echo Broadcast address: $b
echo Netmask bits: $n

相关阅读 更多 +
排行榜 更多 +
找茬脑洞的世界安卓版

找茬脑洞的世界安卓版

休闲益智 下载
滑板英雄跑酷2手游

滑板英雄跑酷2手游

休闲益智 下载
披萨对对看下载

披萨对对看下载

休闲益智 下载