Skip to content

Instantly share code, notes, and snippets.

@powellc
Created September 15, 2010 16:37
Show Gist options
  • Save powellc/581017 to your computer and use it in GitHub Desktop.
Save powellc/581017 to your computer and use it in GitHub Desktop.
#!/bin/bash
###
### This script simply parses ifconfig and loads a box's main IP into a shell variable
###
OS=`uname`
IP="" # store IP
case $OS in
Linux) IP=`ifconfig | grep 'inet addr:'| grep -v '127.0.0.1' | cut -d: -f2 | awk '{ print $1}' | grep 24 `;;
FreeBSD|OpenBSD) IP=`ifconfig | grep -E 'inet.[0-9]' | grep -v '127.0.0.1' | awk '{ print $2}'` ;;
SunOS) IP=`ifconfig -a | grep inet | grep -v '127.0.0.1' | awk '{ print $2} '` ;;
*) IP="Unknown";;
esac
echo $IP
export IP
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment