1

私は基本的に次のことを望んでいます:

#full .co.uk/.com domain names are kept in /var/www/vhosts
for each in /var/www/vhosts
 do
  echo "Attempting to ping $each"
  ping -c 1 $each

  if [ $? comes back with an IP ]; then
   echo "The web server for $each is responding. $each points to $IPADDRESS"
   domainlist=`echo "$each points to $IPADDRESS" >> domaintoiplist.txt`
   echo ""
    else
   echo "Host $each is not reachable!"
  fi

 done

私がうまくいかないのは、pingが何があっても終了ステータス0を返すという事実です。からの解決策が必要ですか:望ましくない結果をもたらす Ping 関数

4

2 に答える 2

3
  • /var/www/vhosts 内のすべてのファイルを調べ、ディレクトリをフィルター処理し、名前のみを取得します
  • ドメインにpingを実行し、最初の行からIPアドレスを取得します
  • ping の終了コードはPIPESTATUS経由でアクセスできます
  • ポジティブ (終了コード 0) をファイルに書き込みます

これはもっと簡単にできると確信していますが、私はbashの達人ではありません:)

for domain in $(ls -lA /var/www/vhosts | grep "^d" | sed -E 's/.* ([^ ]+)?$/\1/g');
do
    ip=$(ping -c 1 $domain|grep "PING" | sed -E 's/PING .* .([0-9.]+). .*/\1/g');
    if [ ${PIPESTATUS[0]} -eq 0 ];
    then 
        echo "${domain} translates to ${ip}";
        echo -e "${domain}\t${ip}" >> translation.txt;
    fi;
done

私のテストフォルダ

# ls -al /var/www/vhosts
-rw-r--r-- 1 root root    0 29. Jan 15:45 other
-rw-r--r-- 1 root root  699 29. Jan 16:34 translation.txt
drwxr-xr-x 2 root root 4096 29. Jan 16:12 www.adasdasdadasdadsadadasdasdasd.com
drwxr-xr-x 2 root root 4096 29. Jan 15:44 www.doesntexist.com
drwxr-xr-x 2 root root 4096 29. Jan 15:44 www.google.com

私の出力

www.doesntexist.com translates to 204.13.248.119 // uh, actually it DOES exist :)
www.google.com translates to 173.194.35.145

編集

プログラムの出力を別のプログラムにパイプする$?と、受信プログラムの終了コードが含まれます。パイプ チェーンで以前に呼び出されたプログラムの終了コードを取得するには、bash-internal 変数を使用できます$PIPESTATUS

これが現在の問題だと思います。

于 2013-01-29T14:49:13.550 に答える
0

https://stackoverflow.com/users/1032504/michel-feldheimへのすべてのクレジット

動作した最終的なスクリプトは次のとおりです。

#!/bin/bash

# Get the IP addresses assigned to the server. This can be used later to match the domains to the servers IP addresses.
IPs=`ifconfig | grep "inet addr" | awk '{print $2;}' | cut -c 6-19 | grep -v "127.0"`

echo "=== This server's IP addresses: ==="
ifconfig | grep "inet addr" | awk '{print $2;}' | cut -c 6-19 | grep -v "127.0"
echo "=== Done. They are listed above. ==="
echo ""

#change the directory where the sites are located below
for domain in $(ls -lA /var/www/vhosts | grep "^d" | sed -E 's/.* ([^ ]+)?$/\1/g' | grep -v '.skel\|chroot\|default\|fs\|fs-passwd\|httpsdocs');
do
echo "Attempting to ping $domain"

ip=$(ping -c 1 $domain|grep "PING" | sed -E 's/PING .* .([0-9.]+). .*/\1/g' | grep -v '.skel\|chroot\|default\|fs\|fs-passwd\|httpsdocs');

  if [ ${PIPESTATUS[0]} -eq 0 ];
   then
   echo "${domain} points to ${ip}";
   #the below line doesn't output to STDOUT on purpose, it goes into the file below.
   echo -e "${domain} points to ${ip}" >> domaintoiplist.txt
   echo ""
    else
   echo "Host ${domain} is not reachable!"
   echo ""
  fi;
done

    echo "==== Done. Written a list of domains & its IP to domaintoiplist.txt in the current directory. ===="
于 2013-01-29T16:39:10.227 に答える