By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,310 Members | 1,466 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,310 IT Pros & Developers. It's quick & easy.

How to test whether a host is reachable?

P: n/a
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
Share this Question
Share on Google+
5 Replies


P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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.