LinuxQuestions.org
Review your favorite Linux distribution.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 05-16-2019, 03:26 AM   #1
ymhass
LQ Newbie
 
Registered: Dec 2017
Posts: 2

Rep: Reputation: Disabled
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.

Last edited by ymhass; 05-16-2019 at 04:00 AM.
 
Old 05-16-2019, 04:08 AM   #2
Turbocapitalist
Senior Member
 
Registered: Apr 2005
Distribution: Linux Mint, Devuan, OpenBSD
Posts: 3,964
Blog Entries: 3

Rep: Reputation: 1891Reputation: 1891Reputation: 1891Reputation: 1891Reputation: 1891Reputation: 1891Reputation: 1891Reputation: 1891Reputation: 1891Reputation: 1891Reputation: 1891
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.
 
1 members found this post helpful.
Old 05-16-2019, 04:44 AM   #3
ymhass
LQ Newbie
 
Registered: Dec 2017
Posts: 2

Original Poster
Rep: Reputation: Disabled
I don't know what to say. Thank you very much! very appreciate it!
 
Old 05-16-2019, 11:05 AM   #4
MadeInGermany
Senior Member
 
Registered: Dec 2011
Location: Simplicity
Posts: 1,093

Rep: Reputation: 478Reputation: 478Reputation: 478Reputation: 478Reputation: 478
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...

Last edited by MadeInGermany; 05-16-2019 at 11:21 AM.
 
1 members found this post helpful.
Old 05-16-2019, 08:26 PM   #5
JJJCR
Senior Member
 
Registered: Apr 2010
Posts: 1,598

Rep: Reputation: 274Reputation: 274Reputation: 274
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
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] Running bash script from another bash script bulletproof.rs Programming 5 12-10-2017 04:22 AM
[SOLVED] BASH Script - What am I doing wrong in this test? - BASH Script BW-userx Programming 34 04-08-2017 01:36 PM
SSH connection from BASH script stops further BASH script commands tardis1 Linux - Newbie 3 12-06-2010 08:56 AM
BASH - I Need a job - Need help with a script worm5252 Programming 32 01-03-2010 07:57 PM
Bash script to create bash script jag7720 Programming 10 09-10-2007 07:01 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 12:07 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration