如何在cidr中创建ipcal的ip地址列表

时间:2015-02-03 08:37:28

标签: bash grep ip pipe netmask

我能够使用下面的代码让ipcal给我NETMASK。我可以使用cidr表示法列表中的地址,如41.80.0.0/17,并只给我netmask.if的ip地址我管道一个IP地址列表(在cidr formart中)它会工作。感谢

#!/bin/bash

echo -n "Enter IP address in CIDR notation: (eg. 192.168.0.0/24) "
read IP_ADDRESS

# Calculate network values
BROADCAST=$(ipcalc -b $IP_ADDRESS)
NETWORK=$(ipcalc -n $IP_ADDRESS)
NETMASK=$(ipcalc -m $IP_ADDRESS)

echo "Set up your ifcfg file with the following values:"
echo $BROADCAST
echo $NETWORK
echo $NETMASK

2 个答案:

答案 0 :(得分:0)

以下是我从ipcalc -b 192.168.0.0/23

获得的内容
Address:   192.168.0.0
Netmask:   255.255.254.0 = 23
Wildcard:  0.0.1.255
=>
Network:   192.168.0.0/23
HostMin:   192.168.0.1
HostMax:   192.168.1.254
Broadcast: 192.168.1.255
Hosts/Net: 510                   Class C, Private Internet

所以要把你需要的东西变成:

BROADCAST=$(ipcalc -b 192.168.0.0/23 | awk '/Broadcast/ {print $2}')
NETWORK=$(ipcalc -b 192.168.0.0/23 | awk '/Network/ {print $2}')
NETMASK=$(ipcalc -b 192.168.0.0/23 | awk '/Netmask/ {print $2}')

echo $BROADCAST
192.168.1.255

echo $NETWORK
192.168.0.0/23

echo $NETMASK
255.255.254.0

答案 1 :(得分:0)

如果您在Awk中进行所有处理,您的脚本可以大大简化。

#!/bin/bash

read -p "Enter IP address in CIDR notation: (eg. 192.168.0.0/24) " IP_ADDRESS

ipcalc -b "$IP_ADDRESS" |
awk '/^Broadcast: / { b=$2 }
     /^Network: / { n=$2 }
     /^Netmask: / { m=$2 }
     END { print "Set up your ifcfg file with the following values:";
       print b; print n; print m; }'