Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- [root@enterprise hostresolve]# cat hostresolve.sh
- #!/bin/bash
- function check_ip()
- {
- # This function prints 'internal' or 'external' for a given IP address.
- # IP addresses are listed as 'internal' if they fall into the private
- # address space defined by rfc1918.
- local ip=$1
- local stat=1
- if [[ $ip =~ ^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}$ ]]; then
- OIFS=$IFS
- IFS='.'
- ip=($ip)
- IFS=$OIFS
- [[ ${ip[0]} -le 255 && ${ip[1]} -le 255 \
- && ${ip[2]} -le 255 && ${ip[3]} -le 255 ]]
- stat=$?
- fi
- type=external
- if [ $stat == "0" ]; then
- if [ ${ip[0]} -eq 10 ]; then
- type=internal
- fi
- if [ ${ip[0]} -eq 192 ] && [ ${ip[1]} -eq 168 ]; then
- type=internal
- fi
- if [ ${ip[0]} -eq 172 ] && [ ${ip[1]} -gt 15 ] && [ ${ip[1]} -lt 32 ]; then
- type=internal
- fi
- else
- type="Invalid IP"
- fi
- echo $type
- }
- echo "<table border="1">"
- echo "<tr><td>Lookup Value</td><td>External IP</td><td>Internal IP</td><td>Respond to Ping</td><td>Hostname</td><td>DNS Name for Internal IP</td></tr>"
- for host in $@; do
- external_ip=""
- external_name=""
- dns=""
- hostname=""
- internal_ip=""
- internal_name=""
- ping=""
- efrom="?"
- ifrom="?"
- ciscoconfig="/u01/grabcfgs/configs/comparisons/indcasa1.latest"
- #First check if it's an IP address
- if [ "$(check_ip ${host})" == "Invalid IP" ]; then
- #Look in external (Google) and internal DNS
- external_ip=$(dig @8.8.8.8 $host a +short | grep -o '[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}')
- internal_ip=$(dig @192.168.10.10 $host a +short +domain=herffjones.hj-int | grep -o '[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}')
- efrom="DNS"
- ifrom="DNS"
- elif [ "$(check_ip $host)" == "external" ]; then
- external_ip="$host"
- efrom="user entry"
- elif [ "$(check_ip $host)" == "internal" ]; then
- internal_ip="$host"
- ifrom="user entry"
- fi
- # The external IP is empty, lets look it up in the ASA
- if [ "$external_ip" == "" ]; then
- # If it's in the Cisco config, move on.
- if grep -q "$host" $ciscoconfig; then
- # Okay, here's what this does (sorry if you're reading this)
- # The inside grep looks for the IP address in the file, and a "name" line associated with it
- # The one outside of that takes the name it found before, and looks for the NAT mapping
- # The one outside of that one, takes the name it found in the nat mapping, and turns that into an IP
- external_ip=$(grep " $(grep " $(grep " $host " $ciscoconfig | grep name | awk '{print $3}')" $ciscoconfig | grep static | awk '{print $3}')" $ciscoconfig | grep name | awk '{print $2}')
- efrom="HQ ASA"
- fi
- fi
- # Check if we've got anything.
- if [ "$internal_ip" == "" ] && [ "$external_ip" == "" ]; then
- echo "<tr><td>$host</td><td colspan='5'>Unable to resolve host IP</td></tr>"
- continue
- fi
- external_name=$(grep -i " $external_ip " "$ciscoconfig" | grep name | awk '{print $3}')
- if [ "$external_name" == "" ] && [ "$internal_ip" == "" ] ; then
- ciscoconfig="/u01/grabcfgs/configs/comparisons/edesign_asa.latest"
- if grep -q $external_ip $ciscoconfig; then
- internal_ip=$(grep -i " $external_ip " "$ciscoconfig" | grep static | awk '{print $4}')
- ifrom="SW ASA"
- fi
- fi
- if [ "$external_name" != "" ]; then
- internal_name=$(grep -i " $external_name " "$ciscoconfig" | grep static | awk '{print $4}')
- fi
- if [ "$internal_name" != "" ] && [ "$internal_ip" == "" ]; then
- internal_ip=$(grep -i " $internal_name" "$ciscoconfig" | grep name | awk '{print $2}')
- ifrom="HQ ASA"
- if [ "$internal_ip" == "" ]; then
- internal_ip=$internal_name
- ifrom="HQ ASA"
- fi
- fi
- if [ "$internal_ip" == "" ]; then
- if grep -q $external_ip /u01/grabcfgs/configs/comparisons/hj-bigip1.clean; then
- internal_ip=$external_ip
- ifrom="HQ F5"
- fi
- fi
- if [ "$external_ip" == "" ]; then
- external_ip="None"
- fi
- if [ "$internal_ip" == "" ]; then
- internal_ip="None"
- fi
- # Reverse DNS Entries
- dns=$(grep -h -e "$external_ip$" -e "${internal_ip:-"null"}$" /u01/grabcfgs/configs/comparisons/dns_*.latest 2>/dev/null | awk '{print $1}')
- if [ "$internal_ip" != "" ]; then
- hostname=$(grep -H -e "$internal_ip\$" -e "$internal_ip " hostips/* 2>/dev/null | cut -d "/" -f 2 | cut -d ":" -f 1)
- ping=$(ping -c 1 -W 1 ${internal_ip} >/dev/null 2>&1 && echo "Yes" || echo "No")
- instanceconfig=$(ssh inxx34.herffjones.hj-int /x01/deploy/instanceinfo.sh ${host})
- if [ "$hostname" == "" ]; then
- lbfiles=$(grep -H "$internal_ip" /u01/grabcfgs/configs/comparisons/*lb*.clean /u01/grabcfgs/configs/comparisons/hj-bigip1.clean 2>/dev/null | cut -d ":" -f 1 | sort -u)
- for file in $lbfiles; do
- config=$(./print_bigipconf "$file" "$internal_ip")
- for line in $config; do
- item=$(echo $line | cut -d ":" -f 1)
- variable=$(echo $line | cut -d ":" -f 2-)
- nodeip=""
- port=""
- nstatus=""
- servername=""
- if [ "$item" == "ForcedSSL" ]; then
- ssl="$variable"
- fi
- #if [ "$item" == "PoolName" ]; then
- #hostname="<b>$variable</b><br />"
- #fi
- #head="<table border=1><tr><td>Server</td><td>IP</td><td>Port</td><td>Status</td></tr>"
- head="<table border=1><tr><td>Server</td><td>IP</td><td>Port</td><td>Status</td><td>Available</td><td>Priority</td></tr>"
- if [ "$item" == "Member" ]; then
- nodeip=$(echo $line | cut -d ":" -f 2)
- port=$(echo $line | cut -d ":" -f 3)
- nstatus=$(echo $line | cut -d ":" -f 4)
- servername=$(grep -H -e "$nodeip\$" -e "$nodeip " hostips/* 2>/dev/null | cut -d "/" -f 2 | cut -d ":" -f 1)
- servername="<a href=hostinfo.php?hostname=${servername}>${servername}</a>"
- avail=$(echo $line | cut -d ":" -f 5)
- pri=$(echo $line | cut -d ":" -f 6)
- #body="$body<tr><td>$servername</td><td>$nodeip</td><td>$port</td><td>$nstatus</td></tr>"
- IFSBAK=$IFS
- for line in $instanceconfig; do
- if echo "$line" | grep -q $nodeip; then
- extra=""
- if [ "$(echo $line | cut -d '|' -f 4)" == "coldfusion" ]; then
- url="http://$(echo $line | cut -d '|' -f 2).herffjones.hj-int"
- extra="<tr><td align=\"center\"><a href="http://$(echo $line | cut -d '|' -f 3).herffjones.hj-int/CFIDE/administrator/">CFIDE Administrator</a></td></tr>"
- else
- url="http://$(echo $line | cut -d '|' -f 2).herffjones.hj-int"
- fi
- nodeip="<table border=1><tr><td align=\"center\">${nodeip}</td></tr><tr><td align=\"center\"><a href="$url">$(echo $line | cut -d '|' -f 2)</a></td></tr>${extra}</tr></table>"
- break
- fi
- done
- IFS=$IFSBAK
- body="$body<tr><td>$servername</td><td>$nodeip</td><td>$port</td><td><a href="${url}/health">$nstatus</a></td><td>$avail</td><td>$pri</td></tr>"
- fi
- done
- if [ "$config" == "" ]; then
- head="<table border=1>"
- body="<tr><td align=\"center\" colspan=\"6\">ERROR: No Pool Members Configured</td></tr>"
- ssl="unknown"
- fi
- hostname="${head}${body}<tr><td align=\"center\" colspan=\"3\">Forced SSL</td><td align=\"center\" colspan=\"3\">${ssl}</td></tr></table>"
- break
- done
- else
- hostname="<a href=hostinfo.php?hostname=${hostname}>${hostname}</a>"
- fi
- fi
- if [ "$hostname" == "</table>" ]; then
- hostname=""
- fi
- if [ "$(check_ip $external_ip)" != "external" ]; then
- external_ip="None"
- fi
- echo -e "<tr $([ ${ping} != "Yes" ] && echo "" || [ "x${hostname}" == "x" ] && echo "style=\"background-color:#FFFF99\"")><td><b>$host</b></td><td align=center>$external_ip (${efrom})</td><td>${internal_ip:-"UNKNOWN"} (${ifrom})</td><td>${ping:-"UNKNOWN"}</td><td>${hostname:-"UNKNOWN"}</td><td>${dns:-"UNKNOWN"}</td></tr>\n"
- hostname=""
- head=""
- body=""
- done
- echo "</table>"
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement