Parse ifconfig to get only my IP address using Bash

后端 未结 20 1210
甜味超标
甜味超标 2020-11-30 05:04

I want to edit the bashrc file to have a simple function called \"myip\" to run. As you might guess, the function myip prints only my internal IP address of my machine.

相关标签:
20条回答
  • 2020-11-30 05:31

    Code working on VDS/VPS too:

    ifconfig | grep -A2 "venet0:0\|eth0" | grep 'inet addr:' | sed -r 's/.*inet addr:([^ ]+).*/\1/' | head -1
    

    or

    ifconfig | grep 'inet addr:' | grep -v 127.0.0.1 | head -n1 | cut -f2 -d: | cut -f1 -d ' '
    
    0 讨论(0)
  • 2020-11-30 05:34

    You can use awk to do both the selecting of the inet line and the parsing of the IP address like so:

    $ ip addr ls docker0 | awk '/inet / {print $2}' | cut -d"/" -f1
    172.17.42.1
    

    In the example above, substitute the device handle eth0 in for docker0. Also, if you want a pure AWK implementation, you can do the "cutting" within like so:

    $ ip addr ls docker0 | awk '/inet / {split($2, ary, /\//); print ary[1]}'
    172.17.42.1
    
    0 讨论(0)
  • 2020-11-30 05:35

    After troubles with greping ifconfig I found a wrapper library to simplify my life

    pip install my_ip 
    mip
    
    0 讨论(0)
  • 2020-11-30 05:36

    Use:

    ifconfig enops3 | greb broadcast | cut -d " " -f10
    

    Where enops3 is the interface name.

    0 讨论(0)
  • 2020-11-30 05:37

    Well, after hours of struggling I finally got it right:

    ifconfig en1 | awk '{ print $2}' | grep -E -o "([0-9]{1,3}[\.]){3}[0-9]{1,3}"
    

    That last part I had missing is just grep a pattern of IP addresses from my list.

    0 讨论(0)
  • 2020-11-30 05:37

    You can also try this

    user@linux:~$ cat script.sh
    ifconfig | grep ad.*Bc | cut -d: -f2 | awk '{ print $1}'
    user@linux:~$ 
    

    Output

    user@linux:~$ ./script.sh
    192.168.1.1
    10.0.1.1
    user@linux:~$
    

    Please take note that ifconfig output might be different depending on your linux version. Hence, you might want to change the script accordingly.

    Btw, this is my ifconfig output

    user@linux:~$ ifconfig 
    eth0      Link encap:Ethernet  HWaddr 00:00:00:00:00:10  
              inet addr:192.168.1.1  Bcast:192.168.56.255  Mask:255.255.255.0
              UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
              RX packets:112 errors:0 dropped:0 overruns:0 frame:0
              TX packets:93 errors:0 dropped:0 overruns:0 carrier:0
              collisions:0 txqueuelen:1000 
              RX bytes:14616 (14.2 KiB)  TX bytes:17776 (17.3 KiB)
    
    eth1      Link encap:Ethernet  HWaddr 00:00:00:00:00:11
              inet addr:10.0.1.1  Bcast:10.0.1.255  Mask:255.255.255.0
              UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
              RX packets:0 errors:0 dropped:0 overruns:0 frame:0
              TX packets:0 errors:0 dropped:0 overruns:0 carrier:0
              collisions:0 txqueuelen:1000 
              RX bytes:0 (0.0 B)  TX bytes:0 (0.0 B)
    
    user@linux:~$
    
    0 讨论(0)
提交回复
热议问题