1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

nc と ping をつかってサーバリストから接続確認をするシェルスクリプト

Last updated at Posted at 2019-02-21

excelおじさん貼り付けようですぐに作る必要があったので雑ですが

server_list

domain.jp:www.domain.jp

nc_to_csv.sh

nc_to_csv.sh
#!/bin/bash

#set -x
SERVER_LIST="./server_list"
CMD="nc -w 0.1 -i 0.01 -vz"
PORT=(22 25 80 443)
CMD02="ping -w 0.1 -c 1 -t 1"
RESULT_FILE="/tmp/nc.csv"

if [ -f ${RESULT_FILE} ]; then
  rm ${RESULT_FILE}
fi

for LIST in $(cat ${SERVER_LIST}); do
  for i in "${PORT[@]}"; do
    RECORD=$(echo ${LIST} | awk -F":" '{print $1}')
    SERVER=$(echo ${LIST} | awk -F":" '{print $2}')
    ${CMD} ${SERVER} ${i} 2>/dev/null
    if [ $? = 0 ]; then
     RESULT+=("YES")
    elif [ $? = 1 ]; then
     RESULT+=("NO")
    else
     RESULT+=("fail")
    fi
  done

  ${CMD02} ${SERVER} 1>/dev/null
  if [ $? = 0 ]; then
   RESULT+=("YES")
  elif [ $? = 1 ]; then
   RESULT+=("NO")
  else
   RESULT+=("fail")
  fi
  #echo "${SERVER} ${RESULT[@]}"

  RESULT_COMMA=$(echo "${RESULT[@]}"  | sed -e 's/\ /\,/g')
  echo "${LIST},${RESULT_COMMA}" >> ${RESULT_FILE}
  RESULT=()
done

実行

$ ./portcheck.sh
13.115.109.xx NO YES NO NO NO
54.64.135.xx NO  NO  NO NO NO
1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?