469,933 Members | 2,452 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,933 developers. It's quick & easy.

How to test whether a host is reachable?

Hello!

As the subject says I need to test whether a host computer in our
network is reachable or not. At the moment I simply attempt to connect
to a given port that is open when the machine is online:

[...]
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
try:
sock.connect(('192.168.0.100', 80))
except socket.error:
print >>sys.stderr "Server offline"
sock.close()
[...]

Now I am wondering if there isn't any better method which would be more
general. In fact, I think of something like a python version of ping
which only tries to send ICMP packets. However, I don't know what the
code has to look like then. Any ideas or suggestions?

Thanks,
Fabian
Feb 22 '07 #1
5 19945
On 2/22/07, Fabian Steiner <li***@fabis-site.netwrote:
Hello!

As the subject says I need to test whether a host computer in our
network is reachable or not. At the moment I simply attempt to connect
to a given port that is open when the machine is online:

[...]
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
try:
sock.connect(('192.168.0.100', 80))
except socket.error:
print >>sys.stderr "Server offline"
sock.close()
[...]

Now I am wondering if there isn't any better method which would be more
general. In fact, I think of something like a python version of ping
which only tries to send ICMP packets. However, I don't know what the
code has to look like then. Any ideas or suggestions?
This is the only reliable way of telling if you can communicate with a
service on a machine. A ping will tell you if it's connected to the
network, but not if it is actually providing any services.

If you really want a ping, the common way is to just execute the systems ping.
Feb 22 '07 #2
Fabian Steiner wrote:
Hello!

As the subject says I need to test whether a host computer in our
network is reachable or not. At the moment I simply attempt to connect
to a given port that is open when the machine is online:

[...]
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
try:
sock.connect(('192.168.0.100', 80))
except socket.error:
print >>sys.stderr "Server offline"
sock.close()
[...]

Now I am wondering if there isn't any better method which would be more
general. In fact, I think of something like a python version of ping
which only tries to send ICMP packets. However, I don't know what the
code has to look like then. Any ideas or suggestions?

Thanks,
Fabian
Just because you could ping with ICMP packets doesn't mean you could
do anything with the machine. I assume that you are connecting to
do something on the machine. Just wrap what you are trying to do
in try: block. It will either succeed or fail. Handle the exeption.

-Larry
Feb 22 '07 #3
Just because you could ping with ICMP packets doesn't mean you could
do anything with the machine. I assume that you are connecting to
do something on the machine. Just wrap what you are trying to do
in try: block. It will either succeed or fail. Handle the exeption.
And the other way round: just because you can't ping a machine doesn't mean
you can't do anything with it - a Firewall might just have snipped away all
the ICMP-packets.

Diez
Feb 22 '07 #4
Hello!

Chris Mellon wrote:
On 2/22/07, Fabian Steiner <li***@fabis-site.netwrote:
>[...]
Now I am wondering if there isn't any better method which would be more
general. In fact, I think of something like a python version of ping
which only tries to send ICMP packets. However, I don't know what the
code has to look like then. Any ideas or suggestions?

This is the only reliable way of telling if you can communicate with a
service on a machine. A ping will tell you if it's connected to the
network, but not if it is actually providing any services.

If you really want a ping, the common way is to just execute the systems
ping.
Ok, obviously, my approach was already the best way to achive this aim.

Thanks for you help,
Fabian
Feb 22 '07 #5
On Feb 22, 3:22 pm, Fabian Steiner <l...@fabis-site.netwrote:
Now I am wondering if there isn't any better method which would be more
general. In fact, I think of something like a python version of ping
which only tries to send ICMP packets.
Server or a firewall in between most probably will filter out any ICMP
packets, so you'll get no pings at all from a machine which IS on-
line. The only way is try the services that you know, that should be
open on that machine. Do not try too many at a time, becouse that
could be interpeted as portscan and you'll get blacklisted.

Feb 22 '07 #6

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

reply views Thread by Jussi Mononen | last post: by
1 post views Thread by gallaczmit | last post: by
6 posts views Thread by SevDer | last post: by
2 posts views Thread by Netkiller | last post: by
5 posts views Thread by Ke Tao | last post: by
1 post views Thread by rich_sposato | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.