Skip to content

Instantly share code, notes, and snippets.

@huzhifeng
Created January 9, 2015 08:08
Show Gist options
  • Save huzhifeng/457dd68aa1765e54e2ad to your computer and use it in GitHub Desktop.
Save huzhifeng/457dd68aa1765e54e2ad to your computer and use it in GitHub Desktop.
Get IP address
huzhifeng@Ubuntu12:~$ hostname -i
127.0.1.1
huzhifeng@Ubuntu12:~$ hostname -I
192.168.2.132
huzhifeng@Ubuntu12:~$ ifconfig eth0 | grep 'inet addr:' | cut -d: -f2 | awk '{print $1;}'
192.168.2.132
huzhifeng@Ubuntu12:~$ ifconfig eth0 2>/dev/null | awk '/inet addr:/ {print $2}' | sed 's/addr://'
192.168.2.132
huzhifeng@Ubuntu12:~$ ip route get 8.8.8.8 | awk 'NR==1 {print $NF}'
192.168.2.132
huzhifeng@Ubuntu12:~$ ip route get 8.8.8.8
8.8.8.8 via 192.168.2.1 dev eth0 src 192.168.2.132
cache
huzhifeng@Ubuntu12:~$ ifconfig | grep -Eo 'inet (addr:)?([0-9]*\.){3}[0-9]*' | grep -Eo '([0-9]*\.){3}[0-9]*' | grep -v '127.0.0.1'
192.168.2.132
huzhifeng@Ubuntu12:~$ ifconfig | grep -Eo 'inet (addr:)?([0-9]*\.){3}[0-9]*' | grep -Eo '([0-9]*\.){3}[0-9]*'
192.168.2.132
127.0.0.1
huzhifeng@Ubuntu12:~$ ifconfig | sed -En 's/127.0.0.1//;s/.*inet (addr:)?(([0-9]*\.){3}[0-9]*).*/\2/p'
192.168.2.132
huzhifeng@Ubuntu12:~$
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment