Quantcast
Channel: Bashタグが付けられた新着記事 - Qiita
Viewing all articles
Browse latest Browse all 2828

ポートを使用しているプロセス

$
0
0
指定したポートを使用しているプロセスを探す いつも、ちまちまとコマンドを打っていたけど、さすがに面倒くさくなったので、普段使っている「現在使用されているポート一覧」を表示する自作コマンドに、パラメータでポート番号を与えるとそのポートを使用しているプロセスを表示するようにアップデート。 うん、かなり楽になった。 #!/usr/bin/env bash PATH=/bin:/usr/bin:/sbin:/usr/sbin OPT=$1 NETSTAT=`which netstat` if [ -z ${NETSTAT} ]; then echo "Can't execute netstat command." exit 1 fi if [ "$OPT" = "" ]; then case "${OSTYPE}" in darwin*) PORTLIST=(`netstat -an | grep 'LISTEN ' | awk 'match($4, /[\:\.][0-9]*$/) {print substr($4, RSTART+1, RLENGTH)}' | sort -nu`) ;; linux*) PORTLIST=(`netstat -an | grep 'LISTEN ' | awk '{print $4}' | grep '^[0-9:]' | sed -e 's/.*://' | sort -n | uniq`) ;; esac for p in ${PORTLIST[@]}; do echo $p done else ps ax | grep `lsof -ni :$OPT | grep LISTEN | awk '{print $2}'` fi

Viewing all articles
Browse latest Browse all 2828

Latest Images

Trending Articles