How to test whether a host is reachable?

Discussion in 'Python' started by Fabian Steiner, Feb 22, 2007.

  1. 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
     
    Fabian Steiner, Feb 22, 2007
    #1
    1. Advertising

  2. Fabian Steiner

    Chris Mellon Guest

    On 2/22/07, 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?
    >


    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.
     
    Chris Mellon, Feb 22, 2007
    #2
    1. Advertising

  3. Fabian Steiner

    Larry Bates Guest

    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
     
    Larry Bates, Feb 22, 2007
    #3
  4. > 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
     
    Diez B. Roggisch, Feb 22, 2007
    #4
  5. Hello!

    Chris Mellon wrote:
    > On 2/22/07, Fabian Steiner <> wrote:
    >> [...]
    >> 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
     
    Fabian Steiner, Feb 22, 2007
    #5
  6. On Feb 22, 3:22 pm, Fabian Steiner <> wrote:

    > 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.
     
    Bart Ogryczak, Feb 22, 2007
    #6
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. =?Utf-8?B?U3Vl?=

    Nested usercontrols - system.page not reachable?

    =?Utf-8?B?U3Vl?=, May 19, 2006, in forum: ASP .Net
    Replies:
    1
    Views:
    557
    =?Utf-8?B?QWx0YWYgQWwtQW1pbiBOYWp3YW5p?=
    May 21, 2006
  2. Ke Tao

    Detect internet reachable

    Ke Tao, Jul 18, 2007, in forum: C Programming
    Replies:
    6
    Views:
    326
    David Thompson
    Aug 26, 2007
  3. Tony Johansson
    Replies:
    3
    Views:
    368
    Martin Honnen
    Jul 3, 2010
  4. inetquestion
    Replies:
    0
    Views:
    175
    inetquestion
    May 6, 2009
  5. Malte Forkel
    Replies:
    0
    Views:
    89
    Malte Forkel
    Nov 25, 2013
Loading...

Share This Page