【问题标题】:if result from ping如果是 ping 的结果
【发布时间】:2013-01-29 13:47:06
【问题描述】:

我基本上想要以下内容:

#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 function having undesirable results的解决方案吗

【问题讨论】:

  • 我想/var/www/vhosts 是一个包含www.example.com 之类的文件夹的文件夹?
  • 你如何测试 $?。假设 vhost 包含有意义的数据,代码对我来说看起来不错
  • @MichelFeldheim 是的,但没有 www。在 DavideBerra 我试过:“$?” -ne "0" , $? -ne 0 , "$?" -eq "0" 和 $? -eq 0 。问题是当我运行它时,即使我创建了 NonExistantDomain.com,因为 ping 成功返回/退出,它从不使用回显“主机 $each 无法访问!”行。
  • [$? -eq 0 ] 是正确的。并且 ping 会在失败时返回一个非零值——但是你提供给它的很多看起来很傻的东西实际上会在某个地方找到可以连接的东西。 (我尝试了 xxxxxxxxxxxxxxxxx.com 并找到了服务器。另一方面,您的 NonExistantDomain.com 没有,至少不是来自这里。)

标签: bash if-statement for-loop ping


【解决方案1】:
  • 遍历 /var/www/vhosts 中的所有文件,过滤目录,仅获取名称
  • ping域,从第一行获取IP地址
  • 可以通过PIPESTATUS访问ping的退出代码
  • 将肯定(退出代码 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 内部变量 $PIPESTATUS

我认为,这是目前的问题。

【讨论】:

  • @Michael Feldheim 谢谢你,我刚刚把我的剧本弄得一团糟,你把它清理干净了。
【解决方案2】:

感谢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. ===="

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-09-28
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多