Parse ifconfig to get only my IP address using Bash

后端 未结 20 1256
甜味超标
甜味超标 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: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:~$
    

提交回复
热议问题