How can I reliably check which is the last time an Ubuntu machine has been connected to the internet?
In case this is not possible, a way to check the last time an Ubuntu machine has been connected to a network would be good enough.
How can I reliably check which is the last time an Ubuntu machine has been connected to the internet?
In case this is not possible, a way to check the last time an Ubuntu machine has been connected to a network would be good enough.
Method 1
Even though NetworkManager.conf allows logging, that apparently still goes into syslog. However, kern.log also has it.
Method 2
I've found that NetworkManager does store last connection time, and it is sorted in
/var/lib/NetworkManager/timestamps
file, in the unix epoch time format (seconds since 1970). Mine looks like this for instance:To show the latest entry
Awk will search for largest epoch time (in other words latest), and date will convert it to human readable form.
I suspect also that this file (
/var/lib/NetworkManager/timestamps
) is used by Edit Connections graphical menu to display last time of connectionThe problem is that if you're still connected to an access point, the GUI way keeps showing
now
not, the time when connection was established lastYou can check the file
/var/log/syslog
it will display the last time you connected to a network.Example
You can run a grep command to pull only what you need from the log
Check
CONNECTED_GLOBAL
after we have foundlink connected
in/var/log/syslog
/link connected/,/CONNECTED_GLOBAL/
If you don't find the "right" way to do this, you can always manufacture your own!
The following bash function will tell you if you're online (to the Internet) or not.
You would just have to write a script which calls it (in a loop and then sleeps) and records the last date and time to a file (overwriting, so it just has just the latest value).
You would have to add code to the loop so it checks as soon as it is initially called and logs (so your initial state is set correctly).
After that, you would only log again when the state first goes offline and when it first comes online after being offline. It's easier to code than to explain. ;)
This would be limited in accuracy by how long a delay (sleep) you use in the loop (to keep it from being a very tight loop that might consume too many system resources.) It would also be unaware of whatever happens before it is started or when it is not running.
The script itself could be started (as a background job (
&
at the end of the invocation), possibly with anohup
to keep it running if its parent process terminates) when your user logs in by running it from your $HOME/.profile file, starting it with your desktop autostart utility (KDE or Gnome), from a cron job that periodically checks to see if it's not already running, or even from whatever startup system you have (init/systemd/etc., if you know enough to do that.)When you can't find something like this that does exactly what you want, it's often not too hard to write your own script to do it and run it like a daemon in the background.
It is best to run it from a regular user account if possible - unless you code it in a robust language like C or Python - because shell scripts running with root privileges often pose security risks.
This approach has another issue. Sometimes a ping will timeout giving you a false offline status.
I have a full script, offline_alert, which pretty much does the opposite of what you want - alerts me when I go offline - but the code may prove useful. It has a better offline function which attempts to reduce false positives from ping timeouts.
One way to check any service/daemon log that relay on Internet to work. For example NTP (Network Time Protocol) updates.
See
dpkg -L ntpdate
for network state hooksWell as I have checked its log for Ubuntu desktop , its update run whenever network up and keep trying after short delay if last update failed. It connects
ntp.ubuntu.com
.(Note, I added first column in the output for my comments)
I confirm it is installed by default for Ubuntu 14.04 LTS 64Bit desktop & server editions.
I gave it a try in VM, it shows only time it connects or reconnects (with Internet available). Not what you want (last time was connected)
Note, syslog is a rotate log, if you want search all logs even old compressed ones, use
zgrep