How to test whether a host is reachable?

F

Fabian Steiner

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
 
C

Chris Mellon

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

Larry Bates

Fabian said:
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
 
D

Diez B. Roggisch

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
 
F

Fabian Steiner

Hello!

Chris said:
[...]
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
 
B

Bart Ogryczak

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top