Linux bash script to extract IP address

前端 未结 13 2379
我在风中等你
我在风中等你 2020-12-04 15:44

I want to make big script on my Debian 7.3 ( something like translated and much more new user friendly enviroment ). I have a problem. I want to use only some of the informa

相关标签:
13条回答
  • 2020-12-04 16:02

    Just a note, since I just spent some time trouble-shooting a botched upgrade on a server. Turned out, that (years ago) I had implemented a test to see if dynamically added interfaces (e.g. eth0:1) were present, and if so, I would bind certain proggis to the 'main' IP on eth0. Basically it was a variation on the 'ifconfig|grep...|sed... ' solution (plus checking for 'eth0:' presence).
    The upgrade brought new net-tools, and with it the output has changed slightly:

    old ifconfig:

    eth0      Link encap:Ethernet  HWaddr 42:01:0A:F0:B0:1D
              inet addr:10.240.176.29  Bcast:10.240.176.29  Mask:255.255.255.255
              UP BROADCAST RUNNING MULTICAST  MTU:1460  Metric:1
              ...<SNIP>
    

    whereas the new version will display this:

    eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1460
          inet 10.240.212.165  netmask 255.255.255.255  broadcast 10.240.212.165
          ...<SNIP>
    

    rendering the hunt for 'eth0:' as well as 'inet addr:' search busted (never mind interfaces called 'em0','br0' or 'wlan0'...). Sure you could check for 'inet ' (or 'inet6'), and make the addr: part optional, but looking closer, you'll see that more or less everything has changed, 'Mask' is now 'netmask',...

    The 'ip route ...' suggestion's pretty nifty - so maybe:

    _MyIP="$( ip route get 8.8.8.8 | awk 'NR==1 {print $NF}' )"
    if [ "A$_MyIP" == "A" ]
    then
        _MyIPs="$( hostname -I )"
        for _MyIP in "$_MyIPs"
        do
            echo "Found IP: \"$_MyIP\""
        done
    else
        echo "Found IP: $_MyIP"
    fi
    

    Well, something of that sort anyway. Since all proposed solutions seem to have circumstances where they fail, check for possible edge cases - no eth, multiple eth's & lo's, when would 'hostname -i' fail,... and then decide on best solution, check it worked, otherwise 2nd best.

    Cheers 'n' beers!

    0 讨论(0)
  • 2020-12-04 16:07

    In my opinion the simplest and most elegant way to achieve what you need is this:

    ip route get 8.8.8.8 | tr -s ' ' | cut -d' ' -f7
    

    ip route get [host] - gives you the gateway used to reach a remote host e.g.:

    8.8.8.8 via 192.168.0.1 dev enp0s3  src 192.168.0.109
    

    tr -s ' ' - removes any extra spaces, now you have uniformity e.g.:

    8.8.8.8 via 192.168.0.1 dev enp0s3 src 192.168.0.109
    

    cut -d' ' -f7 - truncates the string into ' 'space separated fields, then selects the field #7 from it e.g.:

    192.168.0.109
    
    0 讨论(0)
  • 2020-12-04 16:09

    Take your pick:

    $ cat file
    eth0      Link encap:Ethernet  HWaddr 08:00:27:a3:e3:b0
              inet addr:192.168.1.103  Bcast:192.168.1.255  Mask:255.255.255.0
              inet6 addr: fe80::a00:27ff:fea3:e3b0/64 Scope:Link
              UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
              RX packets:1904 errors:0 dropped:0 overruns:0 frame:0
              TX packets:2002 errors:0 dropped:0 overruns:0 carrier:0
              collisions:0 txqueuelen:1000
              RX bytes:1309425 (1.2 MiB)  T
    
    $ awk 'sub(/inet addr:/,""){print $1}' file
    192.168.1.103
    
    $ awk -F'[ :]+' '/inet addr/{print $4}' file
    192.168.1.103
    
    0 讨论(0)
  • 2020-12-04 16:10

    To just get your IP address:

    echo `ifconfig eth0 2>/dev/null|awk '/inet addr:/ {print $2}'|sed 's/addr://'`
    

    This will give you the IP address of eth0.

    Edit: Due to name changes of interfaces in recent versions of Ubuntu, this doesn't work anymore. Instead, you could just use this:

    hostname --all-ip-addresses or hostname -I, which does the same thing (gives you ALL IP addresses of the host).

    0 讨论(0)
  • 2020-12-04 16:10

    If You want to use only sed to extract IP address:

    ifconfig eth0 2>/dev/null sed -n 's/.*[[:space:]]\([[:digit:]][[:digit:]]*\.[[:digit:]][[:digit:]]*\.[[:digit:]][[:digit:]]*\.[[:digit:]][[:digit:]]*\).*/\1/p'
    
    0 讨论(0)
  • 2020-12-04 16:11
      /sbin/ifconfig eth0 | grep 'inet addr:' | cut -d: -f2 | awk '{ print $1}'
    
    0 讨论(0)
提交回复
热议问题