@peccul is peccu

(love peccu '(emacs lisp cat outdoor bicycle mac linux coffee))

雑にローカルのIPアドレスを取得するエイリアス

ip aまたはifconfigからIPアドレスを抜き出す。

alias currentip='(if ip a 2>/dev/null 1>&2 ; then ip a|grep "inet "|awk "{print \$2}"|egrep -v "[01]/"|cut -d/ -f1; else ifconfig|grep "inet "|egrep -v "\.[01] netmask"|awk "{print \$2}"; fi)|tr -d \\r\\n'

inetがついていて、末端が0でも1でもない行を抜き取ると事足りた。

フォーマットするとこんな感じ。

(
  if ip a 2>/dev/null 1>&2
  then
    # ipコマンドがある。主にLinux系
    # x.x.x.1/xx を除外している
    ip a | grep "inet " | awk "{print \$2}" | egrep -v "[01]/" | cut -d/ -f1
  else
    # ない。主にmac用
    # x.x.x.1 netmask xx を除外している
    ifconfig | grep "inet " | egrep -v "\.[01] netmask" | awk "{print \$2}"
  fi
) | tr -d \\r\\n
# ↑末尾の改行をとる