Skip to content

Instantly share code, notes, and snippets.

@mnzk
Created April 11, 2012 15:39
Show Gist options
  • Save mnzk/2360102 to your computer and use it in GitHub Desktop.
Save mnzk/2360102 to your computer and use it in GitHub Desktop.
IPアドレス取得
#!/bin/sh
IP_ADDR1=`LANG=C /sbin/ifconfig \
| grep '^\s*inet addr' \
| grep -v '\b127\.0\.0\.1\b' \
| head -1 \
| awk '{print $2}' \
| cut -d: -f2`
echo "[$IP_ADDR1]"
#-------------------------------------
IP_ADDR2=`LANG=C /sbin/ifconfig \
| grep '^\s*inet addr' \
| grep -v '\b127\.0\.0\.1\b' \
| head -1 \
| perl -pe 's/^\s*inet addr\D*((?:\d+?\.){3}\d+?)\b.*$/$1/'`
echo "[$IP_ADDR2]"
#-------------------------------------
IP_ADDR3=`LANG=C /sbin/ifconfig \
| perl -ne 'print "$1\n" if /^\s*inet addr\D*((?:\d+\.){3}\d+)/' \
| grep -v '^127\.0\.0\.1$' \
| head -1`
echo "[$IP_ADDR3]"
#-------------------------------------
get_ip_addr ()
{
LANG=C /sbin/ifconfig \
| perl -ne 'print "$1\n" if /^\s*inet addr\D*((?:\d+\.){3}\d+)/'
}
IP_ADDR4=`get_ip_addr | grep -v '^127\.0\.0\.1$' | head -1`
echo "[$IP_ADDR4]"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment