- 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.