0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

ワンライナーで疎通確認を一括実行 ~Linux~

Last updated at Posted at 2025-03-02

概要

複数の宛先に対して、疎通確認を一括実行するワンライナーです。
今回はping、Tracepathで作成しました。

コマンド実行手順

疎通確認の結果を、OKかNGでCSVファイルに出力させます。

出力ファイル例 ↓↓↓.
google.com,OK
8.8.8.7,NG
8.8.8.8,OK

① 読み込みファイルの作成

任意のworkディレクトリに移動し、ip_list.txtを作成。
ip_list.txtに疎通対象のIPアドレスを改行区切りで記載します。
※Windowsでファイルを作成した場合、sed -i -e 's/\r//g' ip_list.txtを実行して、改行を修正する必要があります。

ip_list.txt:記載例
google.com
8.8.8.7
8.8.8.8

② 疎通コマンドの実行

  • ping疎通確認 CSV出力
while read -r ip; do ping -c 4 "$ip" > /dev/null 2>&1 && echo "${ip},OK" || echo "${ip},NG"; done < ip_list.txt >> ping_result.csv
# 実行後、ping_result.csvを確認
  • tracepath疎通確認 CSV出力
while read -r ip; do tracepath -m 15 "$ip" 2>/dev/null | grep -qi "reached" && echo "${ip},OK" || echo "${ip},NG"; done < ip_list.txt >> tracepath_result.csv
# 実行後、tracepath_result.csvを確認

時間がかかりすぎる問題の対策

並列処理化

ワンライナーの処理は逐次処理のため時間がかかります。
(コマンド) &で実行することで、別の処理を並列で実行することができます。処理が完了したかは、jobs -lで確認しましょう。

セッションが切れた時の対策

セッションがきれた場合は、処理が終了します。
以下のようにnohupをつけるとセッション終了しても実行可能です。
※シェルの終了を忘れると止まらないので注意が必要です。実行したサーバのIPは必ず記録しましょう。

nohup bash -c 'コマンド' > nohup.out 2>&1 &

処理が中断した時の対策

処理が途中で中断してしまった場合、以下のコマンドで実行済みのアドレスをip_list.txtから削除できます。
削除後に、再度疎通コマンドを実行しましょう。

# 以下はtracepath_result.csvが対象、ping_result.csvの場合は読み込みファイルを置き換える。
## 予備としてip_list.bakのバックアップを作成している
cp ip_list.txt ip_list.bak && while read -r pattern; do sed -i "/^${pattern}$/d" ip_list.txt; done < <(cut -d',' -f1 tracepath_result.csv)

疎通コマンド 全経過を取得する場合

前段は結果だけでしたが、以下は疎通の経過も取得できます。

出力ファイル例 ↓↓↓.
===== Pinging 8.8.8.7 =====
PING 8.8.8.7 (8.8.8.7) 56(84) bytes of data.

--- 8.8.8.7 ping statistics ---
4 packets transmitted, 0 received, 100% packet loss, time 3091ms

===== Pinging 8.8.8.8 =====
PING 8.8.8.8 (8.8.8.8) 56(84) bytes of data.
64 bytes from 8.8.8.8: icmp_seq=1 ttl=117 time=9.49 ms
64 bytes from 8.8.8.8: icmp_seq=2 ttl=117 time=10.8 ms
64 bytes from 8.8.8.8: icmp_seq=3 ttl=117 time=12.6 ms
64 bytes from 8.8.8.8: icmp_seq=4 ttl=117 time=9.41 ms

--- 8.8.8.8 ping statistics ---
4 packets transmitted, 4 received, 0% packet loss, time 3172ms
rtt min/avg/max/mdev = 9.405/10.555/12.569/1.280 ms

① 読み込みファイルの作成

任意のworkディレクトリに移動し、ip_list.txtを作成。
ip_list.txtに疎通対象のIPアドレスを改行区切りで記載します。
※Windowsでファイルを作成した場合、sed -i -e 's/\r//g' ip_list.txtを実行して、改行を修正する必要があります。

ip_list.txt:記載例
google.com
8.8.8.7
8.8.8.8

② 疎通コマンドの実行

以下のコマンドのうち、確認したいものを実行します。
オプションは適宜修正してください。
前段のip_list.txtに記載したアドレス分、ip_result_${ip}.txtファイルが作成されます。

  • ping
while read -r ip; do echo "===== Pinging $ip =====" >> ip_result_${ip}.txt ; ping -c 4 "$ip" >> ip_result_${ip}.txt 2>&1 ; echo "" >> ip_result_${ip}.txt & done < ip_list.txt; wait
  • tracepath(通信経路の調査)
while read -r ip; do echo "===== Tracepath to $ip =====" >> ip_result_${ip}.txt ; tracepath -m 10 "$ip" >> ip_result_${ip}.txt 2>&1 ;echo "" >> ip_result_${ip}.txt & done < ip_list.txt; wait
  • netcat(Netcat:ポート接続確認)
    ※以下はPortを80で指定。時間制限は5秒
while read -r ip; do echo "===== netcat $ip =====" >> ip_result_${ip}.txt ;netcat -zv -w 5 "$ip" 80 >> ip_result_${ip}.txt 2>&1 ;echo "" >> ip_result_${ip}.txt & done < ip_list.txt; wait

③ 結果をまとめる

前段で作成したip_result_${ip}.txtファイルをip_result.txt にまとめます。
以下のコマンドを実行後、ip_resultを確認して、疎通結果を見ます。

while read -r ip; do cat "ip_result_${ip}.txt" >> ip_result.txt && rm -f ip_result_${ip}.txt 2>&1 & done < ip_list.txt; wait

おまけ

Linuxの接続文字について、なんとなく使っていたのですが、
これを機に整理できました。

Linux接続文字一覧

  • 並列処理 → &
  • 逐次実行(失敗時も) → ;
  • 成功時実行 → &&
  • 失敗時実行 → ||
  • パイプ処理(データの受け渡し) → |
  • リダイレクトで出力をファイルに保存 → > / >>
  • エラー出力を標準出力に統合 → 2>&1
  • 不要データを捨てる → >/dev/null
0
1
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
0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?