UPDATED: now works with most apache installs and is more portable / useful:
http://ip2k.com/sitelist.sh
This bash script combines many useful little tricks that you may be interested in even if you don’t need the whole thing. Some include:
- Print a list of IPs on the linux system
- Print a list of websites hosted on the server that match a certain pattern (app = Magento in this case)
- Looping
trap sorry INT
#Two levels of debugging, 1 and 2. 1 shows just pings, 2 shows everything.
dbg=0
function prnt {
echo -e "\e[1;36m[ $1 ]\e[00m"
}
function prntok {
echo -e "\e[1;32m[ $1 ]\e[00m"
}
sorry ()
{
prnt "Caught SIGINT, cleaning up..."
if [ -f tempfile ]; then
rm -rf tempfile
prntok "Found and removed tempfile, exiting now"
else
prntok "tempfile not found, exiting..."
fi
exit 1
}
function getip {
ping -c1 -W1 -q $1 | grep PING | awk '{print $3}' |tr -d \(\)
}
iplist="$(ifconfig | grep 'inet addr' | awk '{print $2}' | tr -d [a-z,:] | grep -v '127.0.0.1')"
echo > tempfile
for ip in $iplist; do
echo $ip >> tempfile
done
sites="$(awk '$1 == "ServerName" { print $2 }' /etc/httpd/conf.d/vhost_* | uniq)"
for site in $sites; do
if [ $dbg -gt 0 ]; then
prnt "DEBUG: Pinging $site..."
fi
siteip=$(getip $site)
ison=$(grep -c $siteip tempfile)
if [ $dbg -gt 1 ]; then
prntok "DEBUG: siteip = $siteip"
fi
if [ $ison = 1 ]; then
if [ $dbg -gt 1 ]; then
prntok "DEBUG: $site found!"
fi
echo "http://$site"
else
if [ $dbg -gt 1 ]; then
prnt "DEBUG: $site not found"
fi
fi
done
rm -rf tempfile
exit 0