-1

I know about

ping -c 1 8.8.8.8

What about a clean and portable way? I think something like checking syslog for the last request or asking a network manager when present would be the best route but apart from Android (http://developer.android.com/reference/android/net/ConnectivityManager.html) I couldn't find anything...

I'm working with Python but this code doesn't exist...

import sys
sys.getnetworkstatus() # True when recent succesful connection went through

Any pointers?

pnuts
  • 58,317
  • 11
  • 87
  • 139
thomas
  • 325
  • 3
  • 11
  • 1
    there is no guarantee that being able to ping one of google's dns servers means you have connectivity. lots of firewalls will eat ICMP. – Doon Nov 02 '15 at 20:42
  • 2
    "*What about a clean and portable way?" A clean and portable way to do what? What are you trying to accomplish? – Robᵩ Nov 02 '15 at 20:46
  • "Is the system online" – thomas Nov 02 '15 at 20:48
  • Something like running `tcpdump` but without requiring priviledges and resources, the idea is to behave like a Skype client: on deconnection, warn the user. But leveraging system properties, not external resources. – thomas Nov 02 '15 at 20:50

2 Answers2

0

The following program will tell you if you are connected to an internet.

import netifaces
def is_connected_to_an_internet():
    ifaces = netifaces.interfaces()
    for iface in ifaces:
        addrs = netifaces.ifaddresses(iface)
        for addr in addrs.get(netifaces.AF_INET, []):
            if addr['addr'].startswith('127'): continue
            return True
        for addr in addrs.get(netifaces.AF_INET6, []):
            if addr['addr'] == '::1': continue
            if addr['addr'].lower().startswith('fe80'): continue
            return True
    return False

Reference: https://pypi.python.org/pypi/netifaces

Robᵩ
  • 163,533
  • 20
  • 239
  • 308
  • Nice, definitely in the right direction! But a bit too simple to check if an interface is up (if I understood your snippet right). I'd like something even more reliable but it does achieve the effect desired – thomas Nov 02 '15 at 21:43
  • It has to be "too simple". You've specified the constraint, "without reaching out." – Robᵩ Nov 02 '15 at 21:45
  • Yeah :) Thanks a lot. I am just hoping for a unified "recent network connection succesful thingy" I'll leave it up until tomorrow and accept your answer probaly – thomas Nov 02 '15 at 21:48
0

"Is the system online" is a wildly under-specified question. The question you really want the answer to is:

Can I reach a remote service on a remote host right now?

and there is only one valid answer:

Try it and see.

You can look at any aspect of system configuration, whether the interfaces are up, whether you've got a valid IP address, are you connecting through some proxy, is the NAT box natting, is the remote service running, is some path into the remote service congested and dropping packets, and so forth. All of these and many more will only yield an approximation to the answer you are really looking for.

Even if you were to to have successfully contacted the service you want one second ago, that tells you far less than you'd like about whether you can contact it now.

msw
  • 42,753
  • 9
  • 87
  • 112
  • Yes I understand it's similar to asking if a gun still has amo and answer without firing it... – thomas Nov 02 '15 at 21:38