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.
grep 'associated' /var/log/kern.log | tail -n1
Jun 21 17:08:25 anaconda kernel: [ 4910.819781] wlan1: associated
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:
$ cat /var/lib/NetworkManager/timestamps
[timestamps]
c562ac2d-8911-4273-b165-ed1495b28c9a=1432777079
46cfcdd9-d095-418f-acd6-0a7ca282bb9a=0
d81fb3d0-1717-42c0-903d-4622c2381597=1434895707
b0bdefe6-df88-49bb-83d8-154dd21d77d9=1433093286
To show the latest entry
date --date=@"$( awk -F'=' 'BEGIN {var=0}{if(var<$2) var=$2;} END{print var}' /var/lib/NetworkManager/timestamps )"
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 connection
The 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 last
man NetworkManager.conf
, the file is /etc/NetworkManager/NetworkManager.conf
; this is going to work only after having enabled logging in the daemon, but unfortunately I need to check this afterwards supposing that the machine has not been configured for this. Anyway +1 for the kern.log
solution and for the first solution which might be helpful in other cases
You can check the file /var/log/syslog
it will display the last time you connected to a network.
Example
Jun 21 08:00:00 Ubuntu dhclient: DHCPREQUEST of 192.0.0.0 on wlan0 to 192.0.0.0 port 67 (xid=0xec7c6e7)
You can run a grep command to pull only what you need from the log
< /var/log/syslog grep DHCPREQUEST
< /var/apt/syslog grep DHCPREQUEST
or variations.
DHCPREQUEST
is not reliable. I'am online since afternoon and since I had a lot of DHCP requests.
Check CONNECTED_GLOBAL
after we have found link connected
in /var/log/syslog
/link connected/,/CONNECTED_GLOBAL/
% awk '/link connected/,/CONNECTED_GLOBAL/ {line=$0} END{print line}' /var/log/syslog
Jun 21 11:12:54 sturm NetworkManager[736]: <info> NetworkManager state is now CONNECTED_GLOBAL Jun 21 11:12:54
% awk '/link connected/,/CONNECTED_GLOBAL/ {month=$1;day=$2;time=$3} END{print month,day,time}' /var/log/syslog
Jun 21 11:12:54
sudo
in the first one, however none of these is working, I think it's because of the POSIXly [[:space:]]
. Which version of awk
are you running?
sudo
removed. GNU Awk 4.1.1, API: 1.1 (GNU MPFR 3.1.2-p11, GNU MP 6.0.0)
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 a nohup
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.
function offline {
## Test if offline
## Returns 0 if online, 2 if offline
local RC
ping -c 1 google.com > /dev/null 2>&1
RC=$?
##echo "offline returning [${RC}]"
return $RC
}
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 hooks
Well 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)
$ grep -r ntpdate /var/log/syslog*
1st_trial Jul 14 00:35:56 user-VirtualBox ntpdate[774]: Can't find host ntp.ubuntu.com: Name or service not known (-2)
Jul 14 00:35:56 user-VirtualBox ntpdate[774]: no servers can be used, exiting
2nd_trial Jul 14 03:38:57 user-VirtualBox ntpdate[2571]: Can't find host ntp.ubuntu.com: Name or service not known (-2)
Jul 14 03:38:57 user-VirtualBox ntpdate[2571]: no servers can be used, exiting
3rd_trial Jul 14 03:46:09 user-VirtualBox ntpdate[2782]: Can't find host ntp.ubuntu.com: Name or service not known (-2)
Jul 14 03:46:09 user-VirtualBox ntpdate[2782]: no servers can be used, exiting
4th_trial Jul 14 03:47:48 user-VirtualBox ntpdate[2917]: step time server 91.189.89.199 offset 3.458355 sec
1st_trial Jul 14 10:23:07 user-VirtualBox ntpdate[728]: Can't find host ntp.ubuntu.com: Name or service not known (-2)
Jul 14 10:23:07 user-VirtualBox ntpdate[728]: no servers can be used, exiting
2nd_trial Jul 14 10:37:22 user-VirtualBox ntpdate[2099]: step time server 91.189.89.199 offset 2.021103 sec
dis-/re-con Jul 14 15:27:09 user-VirtualBox ntpdate[20174]: step time server 91.189.89.199 offset 1.677465 sec
1st_trial Jul 14 23:55:00 user-VirtualBox ntpdate[807]: Can't find host ntp.ubuntu.com: Name or service not known (-2)
Jul 14 23:55:00 user-VirtualBox ntpdate[807]: no servers can be used, exiting
2nd_trial Jul 15 00:00:24 user-VirtualBox ntpdate[2041]: step time server 91.189.94.4 offset 1.619839 sec
1st_trial Jul 15 05:39:08 user-VirtualBox ntpdate[767]: Can't find host ntp.ubuntu.com: Name or service not known (-2)
Jul 15 05:39:08 user-VirtualBox ntpdate[767]: no servers can be used, exiting
2nd_trial Jul 15 05:39:21 user-VirtualBox ntpdate[1088]: step time server 91.189.94.4 offset 2.897077 sec
1st_trial Jul 15 05:49:40 user-VirtualBox ntpdate[787]: Can't find host ntp.ubuntu.com: Name or service not known (-2)
Jul 15 05:49:40 user-VirtualBox ntpdate[787]: no servers can be used, exiting
2nd_trial Jul 15 05:49:50 user-VirtualBox ntpdate[1101]: adjust time server 91.189.94.4 offset 0.090520 sec
reconnect Jul 15 05:55:50 user-VirtualBox ntpdate[2251]: adjust time server 91.189.94.4 offset 0.261432 sec
reconnect Jul 15 06:04:53 user-VirtualBox ntpdate[2702]: step time server 91.189.94.4 offset 0.525658 sec
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
zgrep -ih ntpdate /var/log/syslog* | sort -bn -k2
ntpdate
is installed by default but notntpd
/openntpd
daemon/service which comes in separate packages. To check that change you time&date to wrong one, disconnect then re-connect.
ntpdate
could be used with cron
to update time periodically, it will have same results as running a service (ntpd/openntpd). But you are looking for default configuration and no additional tunning, right?