LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Need Help with Bash Script (https://www.linuxquestions.org/questions/linux-newbie-8/need-help-with-bash-script-4175653998/)

ymhass 05-16-2019 03:26 AM

Need Help with Bash Script
 
Hi all.

I'm quite new to bash scripting.
I have some queries with a bash script I wrote.
I was wondering how to print the default gw only on the first IP, for example:

Quote:

./ip.sh
IP ::192.168.5.38,255.255.255.0,44:8a:5b:78:7e:b3,192.168.5.254
IP ::1.1.1.1,255.255.255.0,44:8a:5b:78:7e:b3,192.168.5.254
IP ::1.1.1.2,255.255.255.0,44:8a:5b:78:7e:b3,192.168.5.254
The Output i want to be:

Quote:

./ip.sh
IP ::192.168.5.38,255.255.255.0,44:8a:5b:78:7e:b3,192.168.5.254
IP ::1.1.1.1,255.255.255.0,44:8a:5b:78:7e:b3
IP ::1.1.1.2,255.255.255.0,44:8a:5b:78:7e:b3
My script:

Code:

#!/bin/bash

interface=`ifconfig | awk 'BEGIN { FS = "\n"; RS = "" } { print $1 }' | sed -e 's/ .*inet addr:/,/' -e 's/ .*//' | egrep -v 'lo|sit0' | sed 's/\:$//'`
for i in $interface; do
        ip=`(ifconfig $i | awk /'inet / {print $2}' | cut -f2 -d":" )`
        mask=`(ifconfig $i | awk /'inet / {print $4}' | cut -f2 -d":" )`
        hwaddr=`(ifconfig $i | awk /'ether/ {print $2}' | cut -f2 -d" " )`
        gateway=`(netstat -rn | grep UG  | tr -s " " | cut -d" " -f2)`

if [ -n "$ip" -a -n "$mask" -a -n "$hwaddr" -a -n "$gateway" ];then

            echo -e IP ::"$ip","$mask","$hwaddr","$gateway"

fi

done

Would appreciate it if you guys could help, Thanks.

Turbocapitalist 05-16-2019 04:08 AM

You'll have to do an extra test and provide an alterative echo statement to avoid printing the gateway each time:

Code:

#!/bin/bash

interface=$(/sbin/ifconfig | awk 'BEGIN {FS="\n";RS=""} \
        $1!~/^lo:/&&$1!~/^sit0:/{sub(/:.*$/,"",$1);print $1;}')

gateway=$(netstat -rn | grep UG  | tr -s " " | cut -d" " -f2)

for i in $interface; do
        ip=$(ifconfig $i | awk /'inet / {print $2}' | cut -f2 -d":" )
        mask=$(ifconfig $i | awk /'inet / {print $4}' | cut -f2 -d":" )
        hwaddr=$(ifconfig $i | awk /'ether/ {print $2}' | cut -f2 -d" " )

        if [ -n "$ip" -a -n "$mask" -a -n "$hwaddr" -a -n "$gateway" ];then
                echo -e C_IP ::"$ip","$mask","$hwaddr","$gateway"
                unset gateway
        elif [ -n "$ip" -a -n "$mask" -a -n "$hwaddr" -a -z "$gateway" ];then
                echo -e C_IP ::"$ip","$mask","$hwaddr"             
        fi

done

The way you use AWK for the $interfaces variable can be simplified. See the example above. The other uses of AWK could be simplified as well.

ymhass 05-16-2019 04:44 AM

I don't know what to say. Thank you very much! very appreciate it!

MadeInGermany 05-16-2019 11:05 AM

A universal ifconfig parser
 
With shell built-ins:
Code:

set -f
skip=
ifconfig |
while IFS= read line
do
  case $line in
  ( lo[\ :0-9]*|sit[\ :0-9]* )
  skip=1
  ;;
  ( [A-Za-z]* )
    if=${line%% *}
    line=${line#$if}
    if=${if%:}
    set -- $(netstat -rn | grep -w UG | grep -w "$if")
    gw=$2
  ;;
  ( "" )
    if [ -z "$skip" ]
    then
      echo "if=$if ip=$ip mask=$mask hwaddr=$hwaddr gw=$gw"
    fi
    skip=
    if= gw= ip= bcast= mask= hwaddr=
    continue
  ;;
  esac
  [ -n "$skip" ] && continue
  set -- $line
  case $1 in
  ( inet )
    ip=${2%*:}
    bcast=${3%*:}
    mask=${4%*:}
  ;;
  ( [Ll]ink )
    nxt=
    for i
    do
      if [ -n "$nxt" ]
      then
        hwaddr=$i
        nxt=
      fi
      case $i in ( [Hh][Ww]addr ) nxt=1;; esac
    done
  ;;
  ( [Ee]ther )
    hwaddr=$2
  ;;
  esac
done

This is not fully working on RedHat/CentOS yet...

JJJCR 05-16-2019 08:26 PM

Well not a bash script but if you need to find default gateway:
Quote:

ip route | grep default
default via 192.168.11.1 dev eth0 proto static


All times are GMT -5. The time now is 02:54 PM.