Shell Shell script to get ethernet interfaces informations

Hello,
I'm trying to write a script in shell that prints me a list of the IP addresses with masks in format aaa.bbb.ccc.ddd/AA but, no way to get the mask in decimal notation.
here is the result :
root@uc1-bejaia [ /uccenos/bin ] # sh uc.notwan
127.0.0.1 0xff000000
192.168.100.254 0xffffff00

The script is the following:
sh:
#!/bin/sh
############################################################################
# not ready .
# trying to get ip/mask but not succed   192.168.2.22/24
############################################################################

# Return default interface address to the internet
_if_wan=$(route -n get default | grep 'if address:' | grep -o '[^ ]*$')

# Return all address on the system but not Wan
_if_unbound=$(ifconfig -a inet| grep inet | grep -v "$_if_wan" | awk '{ print $2, $4 }')
  
echo "$_if_unbound"

I've did some search on the internet and found theses 2 expression in perl, but did not find the way to use theme ( integrate into the script )

sh:
echo "ffffff00" | perl -pe '$_ = join(".", map(hex, /.{2}/g))'
=> 255.255.255.0
echo "ffffff00" | perl -pe '$_ = unpack("B32", pack("H*", $_)); s/0+$//g; $_ = length'
=> 24


So, ... help needed please.

Thank you in advence.
 
Code:
# ./if.sh
127.0.0.1/8 
::1/128 
fe80::1%lo0/64 
172.0.0.1/32 
10.0.0.1/10

sh:
#!/bin/sh 
 
if2addr() 
{ 
        ifconfig -f inet:cidr,inet6:cidr "$1" | grep inet | sed -Ee 's/.+inet6? (.+\/[0-9]+).*/\1/' 
} 
 
get_default_interface() 
{ 
        route -n get default | grep interface | sed -Ee 's/.+interface: (.+)/\1/' 
} 
 
interfaces=$(ifconfig -l) 
exclude=$(get_default_interface) 
 
for interface in ${interfaces}; do 
        if [ "${interface}" = "${exclude}" ]; then 
                continue 
        fi 
 
        if2addr ${interface} 
done

Note: You should also check whether or not you get the default interface when using route(8).
 
Hi DtxdF , thank you for your answer,
The code works well on FreeBSD, but, I tried it on OpenBSD, it do not, since ```inet:cidr``` do not exists in ifconfig.
I've tried to rework my script for OpenBSD,
Here is it.
sh:
#!/bin/sh
# Return default interface address to the internet
_if_wan=$(route -n get default | grep 'if address:' | grep -o '[^ ]*$')
# Return all address on the system but not Wan
_if_unbound=$(ifconfig -a inet| grep inet | grep -v "$_if_wan" | awk '{ print $2, $4 }')
#
echo "$_if_unbound" | \
    awk '{
        lret = "0";   
        function DoConvert(AhexMask){       
            if (AhexMask=="0xff000000" ){         
              lret = "8";
            } else if (AhexMask=="0xffffff00" ){   
              lret = "24";
            }
            return lret;           
        };
        
       Cidr=DoConvert($2);
       printf("%s%s\n", $1, Cidr); 
};'

But erros occure, and I do not understand what they means.

# sh uc.notwan
awk: syntax error at source line 3
context is
>>> function <<< DoConvert(AhexMask){
awk: illegal statement at source line 3
awk: syntax error at source line 5

Any idea ??
Help please.
 
Ooooof,
I have done this, It works but esthetically not really beautifull
Bash:
#!/bin/sh
# Return default interface address to the internet
_if_wan=$(route -n get default | grep 'if address:' | grep -o '[^ ]*$')

# Return all address on the system but not Wan
_if_unbound=$(ifconfig -a inet| grep inet | grep -v "$_if_wan" | awk '{ print $2, $4 }')

#
echo "$_if_unbound" | awk '{
    _ip=$1;
    _ms=$2;
         if (_ms == "0x80000000" ) {  _ms = "/1";  }
    else if (_ms == "0xc0000000" ) {  _ms = "/2";  }
    else if (_ms == "0xe0000000" ) {  _ms = "/3";  }
    else if (_ms == "0xf0000000" ) {  _ms = "/4";  }
    else if (_ms == "0xf8000000" ) {  _ms = "/5";  }
    else if (_ms == "0xfc000000" ) {  _ms = "/6";  }
    else if (_ms == "0xfe000000" ) {  _ms = "/7";  }
    else if (_ms == "0xff000000" ) {  _ms = "/8";  }
    else if (_ms == "0xff800000" ) {  _ms = "/9";  }
    else if (_ms == "0xffc00000" ) {  _ms = "/10"; }
    else if (_ms == "0xffe00000" ) {  _ms = "/11"; }
    else if (_ms == "0xfff00000" ) {  _ms = "/12"; }
    else if (_ms == "0xfff80000" ) {  _ms = "/13"; }
    else if (_ms == "0xfffc0000" ) {  _ms = "/14"; }
    else if (_ms == "0xfffe0000" ) {  _ms = "/15"; }
    else if (_ms == "0xffff0000" ) {  _ms = "/16"; }
    else if (_ms == "0xffff8000" ) {  _ms = "/17"; }
    else if (_ms == "0xffffc000" ) {  _ms = "/18"; }
    else if (_ms == "0xffffe000" ) {  _ms = "/19"; }
    else if (_ms == "0xfffff000" ) {  _ms = "/20"; }
    else if (_ms == "0xfffff800" ) {  _ms = "/21"; }
    else if (_ms == "0xfffffc00" ) {  _ms = "/22"; }
    else if (_ms == "0xfffffe00" ) {  _ms = "/23"; }
    else if (_ms == "0xffffff00" ) {  _ms = "/24"; }
    else if (_ms == "0xffffff80" ) {  _ms = "/25"; }
    else if (_ms == "0xffffffc0" ) {  _ms = "/26"; }
    else if (_ms == "0xffffffe0" ) {  _ms = "/27"; }
    else if (_ms == "0xfffffff0" ) {  _ms = "/28"; }
    else if (_ms == "0xfffffff8" ) {  _ms = "/29"; }
    else if (_ms == "0xfffffffc" ) {  _ms = "/30"; }
    else if (_ms == "0xfffffffe" ) {  _ms = "/31"; }
    else if (_ms == "0xffffffff" ) {  _ms = "/32"; }
    else {   _ms = "/none";                        }

    printf("%s%s\n", _ip, _ms);
};'
 
Bash:
#!/bin/sh
m2cidr()
{
# echo -n "$1 => "
A=$1
j=32;while [ $((A & 1)) -eq 0 ];do j=$(($j - 1));A=$(($A >> 1));done
echo $j
}
# testing
m2cidr "0x80000000"
m2cidr "0xc0000000"
m2cidr "0xe0000000"
m2cidr "0xf0000000"
m2cidr "0xf8000000"
m2cidr "0xfc000000"
m2cidr "0xfe000000"
m2cidr "0xff000000"
m2cidr "0xff800000"
m2cidr "0xffc00000"
m2cidr "0xffe00000"
m2cidr "0xfff00000"
m2cidr "0xfff80000"
m2cidr "0xfffc0000"
m2cidr "0xfffe0000"
m2cidr "0xffff0000"
m2cidr "0xffff8000"
m2cidr "0xffffc000"
m2cidr "0xffffe000"
m2cidr "0xfffff000"
m2cidr "0xfffff800"
m2cidr "0xfffffc00"
m2cidr "0xfffffe00"
m2cidr "0xffffff00"
m2cidr "0xffffff80"
m2cidr "0xffffffc0"
m2cidr "0xffffffe0"
m2cidr "0xfffffff0"
m2cidr "0xfffffff8"
m2cidr "0xfffffffc"
m2cidr "0xfffffffe"
m2cidr "0xffffffff"
 
Hello, thank you covacat for this answer.
I think I miss some thing.
What I want is to give the output of theses 2 commands in this format

192.168.10.1 | 0xffffff00 | 255.255.255.0 | 24
The ` | ` here is just to say a separator ( space suffice ).
The mask is displayed in 3 forms, Hexa, dotted & cidr.

the shell script I use is :
Bash:
#!/bin/sh
  # Do conversion from hex2cidr for mask addresses on BSD
  m2cidr() {
    _mask=$1
    _mskd=32
    while [ $(($_mask & 1)) -eq 0 ]; do
      _mskd=$(($_mskd - 1))
      _mask=$(($_mask >> 1))
    done
    echo $_mskd
  }

# Get IP address linked to the iNet connection.
_if_wan=$(route -n get default | grep 'if address:' | grep -o '[^ ]*$')

# Return IP/MASK.
#  HOW TO USE THSE 2 Expression inside the following script ??
# echo "ffffff00" | perl -pe '$_ = join(".", map(hex, /.{2}/g))'   | => 255.255.255.0
# echo "ffffff00" | perl -pe '$_ = unpack("B32", pack("H*", $_)); s/0+$//g; $_ = length'    | => 24
ifconfig -a inet | grep inet | grep -v -e "$_if_wan" -e "127.0.0.1" | \
    awk '{
        _mask=sprintf("%s", $4);
        _res=32;
        while ( (and(_mask,1)) == 0 ){
           _res = _res-1;
           rshift(_mask,1);         
        }
        printf("%s/%s\n", $2, _res);   
    };'

exit 0

Tried to incorportate the function you give me inside the `awk` script inside this shell, but not succeed.
I converted the shell function you give me in awk .... but do not work.
I've found 2 Perl expression in the internet that do the job, but how incorportate them in this shell script.

Thank you for help

PS, I use OpenBSD.
 
rshift seems to convert numbers to signed 32 bit and wont work properly
this works
awk -v A=0xFFFFFF00 'BEGIN {B=32;while ((A%2) == 0) {B--;A = (A - (A%2))/2}; print B}'
 
Back
Top