複数のアドレスで繰り返し:pingを実行し、データをファイルに入れます。

複数のアドレスで繰り返し:pingを実行し、データをファイルに入れます。

私はスクリプトが欲しいです:

  1. IPアドレスのリストを見る
  2. pingアドレスの1つ
  3. データをインポートしてファイルに入れる
  4. 次のIPに移動

これまで私は以下を持っています:

cd /Path/to/addressees || exit 1
for targethost in a b c; do
  {ping {targethost}
      echo $'\n'"finished:
  } >"$log_file" 2>&1
done

これを実行するとエラーが発生します。

./ping_address: line 3: cd: /path/to/ip_adress: No such file or directory
./ping_address: line 7: unexpected EOF while looking for matching `"'
./ping_address: line 8: syntax error: unexpected end of file

私はまだUnixスクリプトに慣れていないので、どんな助けでも大きな助けになります!

ベストアンサー1

いくつか:

  1. ファイルにCDを挿入することはできません(テストを使用して-fファイルが存在することを確認できます。以下を参照)。
  2. 「ab c」を使ったのか分からない。この変数にはアドレスを含める必要がありますか?あなたのアドレスがスクリプトに含まれているのか、ファイルに保存されているのかはわかりません。
  3. 通常、変数を参照するには$が必要です(つまり、$では${targethost}ないtargethost)、$割り当てるときは省略されます。

それ以外の理由は何ですか(ip_addressesというファイルがあり、1行に1つのアドレスがあるか、スペースで区切られたアドレスがあるとします)。

#!/bin/bash
IP_FILE="/tmp/ip_address_file" # The file with the IP addresses
LOGFILE="/tmp/log_results"  # Where the results will be stored
if [[ ! -f ${IP_FILE} ]]; then
   echo "No File!"
   exit 1
fi
for IP_ADDRESS in $(cat $IP_FILE); do
   echo "TEST FOR ${IP_ADDRESS}" >> $LOGFILE
   # The -c 1 means send one packet, and the -t 1 means a 1 second timeout    
   ping -c 1 -t 1 ${IP_ADDRESS} >> $LOGFILE 2>&1 
done

または、各 IP のファイルを生成するには、次のように使用できます。

#!/bin/bash
IP_FILE="/tmp/ip_address_file" # The file with the IP addresses
if [[ ! -f ${IP_FILE} ]]; then
   echo "No File!"
   exit 1
fi
for IP_ADDRESS in $(cat $IP_FILE); do
   echo "TEST FOR ${IP_ADDRESS}"
   # The -c 1 means send one packet, and the -t 1 means a 1 second timeout    
   ping -c 1 -t 1 ${IP_ADDRESS} >> ${IP_ADDRESS}.log 2>&1 
done

スクリプトに IP を含めるには:

#!/bin/bash
IPS='1.1.1.1 2.2.2.2 3.3.3.3 4.4.4.4 5.5.5.5'
if [[ ! -f ${IP_FILE} ]]; then
   echo "No File!"
   exit 1
fi
for IP_ADDRESS in ${IPS}; do
   echo "TEST FOR ${IP_ADDRESS}"
   # The -c 1 means send one packet, and the -t 1 means a 1 second timeout    
   ping -c 1 -t 1 ${IP_ADDRESS} >> ${IP_ADDRESS}.log 2>&1 
done

おすすめ記事