OpenWrt Forum Archive

Topic: list network number give IP address and netmask

The content of this topic has been archived on 19 Apr 2018. There are no obvious gaps in this topic, but there may still be some posts missing at the end.

I am writing some scripts on Kamikaze, but there is not 'ipcalc' on the busybox.
Could someone help me to work out network number given IP address
and netmask  ( in the notation as show in /etc/config/network ) ?

This is what I will do if there is 'ipcalc' :-

$ ipcalc -n 192.168.128.115 255.255.192.0
NETWORK=192.168.128.0

How to do it in the absence of 'ipcalc' ?

;-)

OpenWrt Kamikaze uses a seperate ipcalc shell script (/bin/ipcalc.sh). Here is a example with UCI. The shell script below reads the LAN IP address and the netmask from /etc/config/network, calculates and finally prints the network number.

/ipcalc-test.sh

#!/bin/sh

. /etc/functions.sh

config_load "network"
local section="lan"
config_get "ipaddr"  "$section" "ipaddr"
config_get "netmask" "$section" "netmask"

eval "$(/bin/ipcalc.sh $ipaddr $netmask)"

echo "Network: $NETWORK"

Test it

root@OpenWrt:/# ./ipcalc-test.sh
Network: 192.168.178.0
root@OpenWrt:/#

My configuration (uci show network.lan):

network.lan=interface
network.lan.type=bridge
network.lan.ifname=eth0.0
network.lan.proto=static
network.lan.ipaddr=192.168.178.3
network.lan.netmask=255.255.255.0
network.lan.dns=192.168.178.1
network.lan.gateway=192.168.178.1

Have fun smile

(Last edited by forum2006 on 25 Sep 2007, 17:03)

The discussion might have continued from here.