1. 程式人生 > 其它 >SHELL指令碼中取得域名的IP地址

SHELL指令碼中取得域名的IP地址

單個獲取

編寫角本pingip.sh

#!/bin/sh

ADDR=qq.com
TMPSTR=`ping ${ADDR} -c 1 | sed '1{s/[^(]*(//;s/).*//;q}'`
echo ${TMPSTR}
echo ${TMPSTR} >> 666.txt

執行結果

haima@haima-PC:~/Desktop$ sh pingip.sh 
58.250.137.36

批量獲取

思路:
從檔案裡迴圈讀取每行域名,然後獲取,輸出到檔案裡

新建domain_list.txt檔案

qq.com
baidu.cn

留言個空行,否則讀到最後一行域名

編寫角本程式碼pingip_batch.sh

#!/bin/bash

filename=domain_list.txt # 要讀取的檔案
proc_cnt=0 # 計數器

cat $filename | while read LINE # 迴圈讀取
do
  echo ""
  proc_cnt=`expr $proc_cnt + 1`
  echo "processing  $proc_cnt   $LINE"
  dumpstring=`ping ${LINE} -c 1 | sed '1{s/[^(]*(//;s/).*//;q}'`
  echo ""
  echo $dumpstring
  echo $dumpstring >> domain_list-resp.txt
  sleep 0.05
done

執行

haima@haima-PC:~/Desktop/獲取ip$ sh pingip_batch.sh

processing  1   qq.com

58.250.137.36

processing  2   baidu.cn

220.181.38.148

輸出檔案:

[Haima的部落格] http://www.cnblogs.com/haima/