1

I basically want the following:

#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

The bit I can't get working is the fact that ping returns exit status of 0 no matter what. Do I need the solution from: Ping function having undesirable results

Community
  • 1
  • 1
Zippyduda
  • 107
  • 2
  • 7
  • 19
  • I suppose `/var/www/vhosts` is a folder containing folders like `www.example.com` ? – Michel Feldheim Jan 29 '13 at 13:54
  • how do you test $?. Assuming vhost contains data that makes sense, the code looks good to me – Davide Berra Jan 29 '13 at 14:07
  • @MichelFeldheim yes but without the www. at DavideBerra I have tried: "$?" -ne "0" , $? -ne 0 , "$?" -eq "0" and $? -eq 0 . The issue is that when I run it, even if I create NonExistantDomain.com, because ping returns/exits successfully, it never uses the echo "Host $each is not reachable!" line. – Zippyduda Jan 29 '13 at 14:14
  • [ $? -eq 0 ] is correct. And ping will return a non-zero value on failure - but a lot of things you feed it that look silly will actually find something somewhere to connect to. (I tried xxxxxxxxxxxxxxxxx.com and it found a server. On the other hand, your NonExistantDomain.com didn't, at least not from here.) – William Jan 29 '13 at 14:59

2 Answers2

3
  • walk through all files in /var/www/vhosts, filter directories, get the names only
  • ping the domain, get the IP address from the first line
  • exit code of ping is accessible via PIPESTATUS
  • write positives ( exit code 0 ) into the file

I am sure, this can be done simpler but I am not a bash guru :)

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

my test folder

# 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

my output

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

EDIT

If you pipe a programs output to another program, $? will contain the exit code of the receiving program. To get the exit code of a program which is called earlier in the pipe chain, you can use the bash-internal variable $PIPESTATUS.

I think, this is the problem currently.

Michel Feldheim
  • 17,625
  • 5
  • 60
  • 77
0

All credit to https://stackoverflow.com/users/1032504/michel-feldheim

Final script that worked is below:

#!/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. ===="
Community
  • 1
  • 1
Zippyduda
  • 107
  • 2
  • 7
  • 19