Find the answer to your Linux question:
Results 1 to 10 of 10
Like Tree3Likes
  • 1 Post By watael
  • 1 Post By watael
  • 1 Post By watael
Hi, I’m looking for a way to have my Shell Bash script find out which ethernet interface is used. Eth0, Eth1 or something else. And then I want to be ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1
    Just Joined!
    Join Date
    Aug 2013
    Posts
    17

    Need help to find out which ethernet interface is used (Shell Bash)


    Hi,

    I’m looking for a way to have my Shell Bash script find out which ethernet interface is used. Eth0, Eth1 or something else. And then I want to be able to use that info to insert the IP address used on that interface. Lastly, I need my script to ask the user if he wants to use that IP address or specify one manually.

    Any help is appreciated. 

    Thanks

  2. #2
    Linux Newbie
    Join Date
    Nov 2012
    Posts
    224
    ...few googling later...
    Code:
    $ /sbin/route -n
    Kernel IP routing table
    Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
    0.0.0.0         192.168.1.1     0.0.0.0         UG    0      0        0 eth0
    192.168.1.0     0.0.0.0         255.255.255.0   U     0      0        0 eth0
    192.168.2.0     0.0.0.0         255.255.255.0   U     0      0        0 eth1
    $
    $ /sbin/route -n | awk '/^0\.0\.0\.0/{print $NF}'
    eth0
    so
    Code:
    $ ip addr show $(/sbin/route -n | awk '/^0\.0\.0\.0/{print $NF}') \
       |awk 'BEGIN{FS=" *|/"}/inet /{print $3}'
    192.168.1.123
    EDIT: here's another one, shorter
    Code:
    ip route get 10.10.10.10 | awk '/src/{print $NF}'
    Last edited by watael; 09-01-2013 at 03:31 PM.
    narugawa likes this.

  3. #3
    Just Joined!
    Join Date
    Aug 2013
    Posts
    17
    Quote Originally Posted by watael View Post
    ...few googling later...
    Code:
    $ /sbin/route -n
    Kernel IP routing table
    Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
    0.0.0.0         192.168.1.1     0.0.0.0         UG    0      0        0 eth0
    192.168.1.0     0.0.0.0         255.255.255.0   U     0      0        0 eth0
    192.168.2.0     0.0.0.0         255.255.255.0   U     0      0        0 eth1
    $
    $ /sbin/route -n | awk '/^0\.0\.0\.0/{print $NF}'
    eth0
    so
    Code:
    $ ip addr show $(/sbin/route -n | awk '/^0\.0\.0\.0/{print $NF}') \
       |awk 'BEGIN{FS=" *|/"}/inet /{print $3}'
    192.168.1.123
    EDIT: here's another one, shorter
    Code:
    ip route get 10.10.10.10 | awk '/src/{print $NF}'
    sorry I did not understand edit edit sorry is ok you edited at the same time
    Last edited by narugawa; 09-01-2013 at 03:56 PM.

  4. #4
    Linux Newbie
    Join Date
    Nov 2012
    Posts
    224
    «do not understand», as «not working», doesn't say much about what's not working/understood.
    you want the default network interface's address on your system ?
    here's two methods, one long, one shorter.

  5. #5
    Just Joined!
    Join Date
    Aug 2013
    Posts
    17
    Code:
    ip=$(ip route get 10.10.10.10 | awk '/src/{print $NF}'| cut -d/ -f1)
    works for one step

    step 1 of detecting the IP =works
    step 2 ask the user if he wants to use the IP or change manually

  6. #6
    Linux Newbie
    Join Date
    Nov 2012
    Posts
    224
    step 2 : ask the user, and read the answer.
    depending on the answer, ask for IP, and... read it

    you'll surely want to check IP is given in a valid format; but that's another question.
    narugawa likes this.

  7. #7
    Just Joined!
    Join Date
    Aug 2013
    Posts
    17
    yes for all and IP in variable $ip

  8. #8
    Linux Newbie
    Join Date
    Nov 2012
    Posts
    224
    awk is self-sufficient
    Code:
    ip=$(ip route get 10.10.10.10 | awk '/src/{sub("/.*","",$NF); print $NF}')
    if you're using awk, then cut, grep, sed..., are neither longer needed, nor desirable.
    narugawa likes this.

  9. #9
    Just Joined!
    Join Date
    Aug 2013
    Posts
    17
    Quote Originally Posted by watael View Post
    awk is self-sufficient
    Code:
    ip=$(ip route get 10.10.10.10 | awk '/src/{sub("/.*","",$NF); print $NF}')
    if you're using awk, then cut, grep, sed..., are neither longer needed, nor desirable.
    Thanks working

    Code:
    #!/bin/bash
    
    ip1=$(ip route get 10.10.10.10 | awk '/src/{sub("/.*","",$NF); print $NF}')
    
    echo ton IP est $ip1 entrer  votre IP ou remplacer le ip par défaut
    name="$ip1"
    read -e -i "$name" -p "entrez votre ip: " ip
     echo
    
    
    if [[ $ip =~ ^((2([0-4][0-9]|5[0-5])|[0-1]{0,1}[0-9]{1,2})\.){3}(2([0-4][0-9]|5[0-5])|[0-1]{0,1}[0-9]{1,2})$ ]]; then  
      echo "ton IP est valide"
    else
    echo  "ton IP n'est pas valide"
      exit 1 how to return to the start line to enter the IP
    
    fi
    
    echo mon ip est :$ip
    Code:
    echo  "ton IP n'est pas valide"
      exit 1 how to return to the start line to enter the IP
    this is the only problem I have left
    Last edited by narugawa; 09-02-2013 at 07:20 PM.

  10. #10
    Just Joined!
    Join Date
    Aug 2013
    Posts
    17
    Code:
    #!/bin/bash
     
    ip1=$(ip route get 10.10.10.10 | awk '/src/{sub("/.*","",$NF); print $NF}')
     
    echo Your IP is $ip1 replace the IP or keep it default
    name="$ip1"
    read -e -i "$name" -p "enter your IP: " ip
    name="${input:-$name}"
     echo
     
     
    if [[ $ip =~ ^((2([0-4][0-9]|5[0-5])|[0-1]{0,1}[0-9]{1,2})\.){3}(2([0-4][0-9]|5[0-5])|[0-1]{0,1}[0-9]{1,2})$ ]]; then 
      echo "Your IP is valid"
    else
    echo  "your IP is not valid"
    
     
    fi
     
    echo your IP is :$ip
    the topic is resolved
    Last edited by narugawa; 09-03-2013 at 06:27 PM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •