Parse ifconfig to get only my IP address using Bash

后端 未结 20 1216
甜味超标
甜味超标 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:38

    Both the following work here (CentOS 5).

    ip addr show eth0 | awk '$1 == "inet" {gsub(/\/.*$/, "", $2); print $2}'
    
    ifconfig eth0 | awk '/inet addr/ {gsub("addr:", "", $2); print $2}'
    

    For OS X (v10.11 (El Capitan) at least):

    ifconfig en0 | awk '$1 == "inet" {print $2}'
    

提交回复
热议问题