how can you ping a domain and store the ip?
like given a url in a variable $url
how can i ping it?
also how can i find the local server's ip address on a cpanel server?
(i have multiple servers and didnt want to hard code it in)
(basically i want to check the domain accounts on the server, ping them and see if they are resolving to the local server ip)
methyl
December 29, 2011, 2:29pm
2
Please post your exact Operating System and version and state what Shell you prefer.
There is so much variation in TCP/IP commands that there is no generic answer.
There seems to be a Forum on the cPanel site:
http://www.cpanel.net/
centos
and my shell is like this:
#!/bin/bash
thanks!
---------- Post updated at 10:35 PM ---------- Previous update was at 02:37 PM ----------
given any domain domain.com
do you know how to ping the domain to get its ip from a shell script?
thanks
Not tested
a="www.google.com"
ip=$(host $a | awk '/has add/ {print $4;exit}')
[ "$ip" -eq "" ] && echo "Not Valid Domain" || echo "IP address is : $ip"
Something like this?
val=$( ping $url -c 1 )
IP=$(echo $c | awk -F'[()]' '{print $6}')
If ping succeeded, the IP will have the ip address.
If your OS is solaris, then this will not work!
HTH
--ahamed
host doesnt give the right ip of google
for example, Google is 74.125.227.49 (from a ping)
but
host google.com says:
google.com has address 173.194.34.52
google.com has address 173.194.34.48
google.com has address 173.194.34.49
google.com has address 173.194.34.50
google.com has address 173.194.34.51
google.com mail is handled by 30 alt2.aspmx.l.google.com .
google.com mail is handled by 40 alt3.aspmx.l.google.com .
google.com mail is handled by 50 alt4.aspmx.l.google.com .
google.com mail is handled by 10 aspmx.l.google.com .
google.com mail is handled by 20 alt1.aspmx.l.google.com .
---------- Post updated at 05:08 AM ---------- Previous update was at 05:01 AM ----------
ahamed101:
Something like this?
val=$( ping $url -c 1 )
IP=$(echo $c | awk -F'[()]' '{print $6}')
If ping succeeded, the IP will have the ip address.
If your OS is solaris, then this will not work!
HTH
--ahamed
what's val?
the ping gives somethign like this:
ping "google.com " -c 1
how do you extract the ip from that?
PING google.com (173.194.34.48) 56(84) bytes of data.
64 bytes from par03s03-in-f16.1e100.net (173.194.34.48): icmp_seq=1 ttl=57 time=4.31 ms
--- google.com ping statistics ---
1 packets transmitted, 1 received, 0% packet loss, time 0ms
rtt min/avg/max/mdev = 4.314/4.314/4.314/0.000 ms
what if the url times out
like
ping nonexistentdomaifsffdsdfsdsfn.com
ping: unknown host nonexistentdomaifsffdsdfsdsfn.com
---------- Post updated at 05:10 AM ---------- Previous update was at 05:08 AM ----------
ahamed101:
Something like this?
val=$( ping $url -c 1 )
IP=$(echo $c | awk -F'[()]' '{print $6}')
If ping succeeded, the IP will have the ip address.
If your OS is solaris, then this will not work!
HTH
--ahamed
is the code like this?
val=$( ping $domain -c 1 )
IP=$(echo $val | awk -F'[()]' '{print $6}')
Yes... sorry about the typo...
val=$( ping $domain -c 1 )
IP=$(echo $val | awk -F'[()]' '{print $6}')
--ahamed
this seems to be working, thanks
i am really bad with syntax, do you know how to store this into a variable:
hostname -i
(this gives the ip of the server)
when i do
serverip=hostname -i it doesnt work
serverip=$( hostname -i )
#or
serverip=`hostname -i `
--ahamed
figured it out
serverip=$(hostname -i)
works
---------- Post updated at 05:38 AM ---------- Previous update was at 05:36 AM ----------
do you know how to compare two variables
like $serverip and $IP
and store the result into a boolean like $check
check=1
if [ "$serverip" = "$IP" ]
then
check=0
fi
#or
test "$serverip" = "$IP" && check=0 || check=1
--ahamed