Getip (script)

From LQWiki
Jump to: navigation, search

Getip is a shellscript that will retrieve your IP address from the command line

#!/bin/bash
# get ip
/sbin/ifconfig $1 | grep inet | awk '{print $2}' | sed 's/^addr://g'

   To do it without any external commands (apart from ifconfig), or to get your Internet address if you are behind a NAT:

## The -n option retrieves the Internet IP address
## if you are behind a NAT
if [ "$1" = "-n" ]
then
  ip=$(lynx -dump http://cfaj.freeshell.org/ipaddr.cgi)
else
  if=$1   ## specify which interface, e.g. eth0, fxp0
  system=$(uname)
  case $system in
      FreeBSD) sep="inet " ;;
      Linux) sep="addr:" ;;
  esac
  temp=$(ifconfig $if)
  temp=${temp#*"$sep"}
  ip=${temp%% *}
fi

printf "%s\n" "$ip"
### CFAJ ###