0

I've to run some reports on a particular host (HOST A). The commands being run on the this host (HOST A) can collect some information from remote servers, but not the 'current' server time of these remote hosts. Also, I don't have permission to connect to the hosts remotely or collect the time using ssh or rsh (rsh date) from HOST A.

So, I post two questions:-

  1. Is there a way to find the 'current' server time of the remote hosts by not using ssh/rsh.

  2. Can someone help me with a time calculation script based on GMT time settings. (I can do a date -u to collect the GMT time on HOST A and calculate the approx. time on remote host based on GMT time and remote host's Timezone. All hosts in the environment are updated using synced NTP servers)

I'm using BASH on Solaris 10 (preferred) / RHEL 6

Thanks in Advance!

Marcos
  • 845
  • 3
  • 10
  • 21
  • If everything is synchronised properly with NTP, then surely GMT (UTC) + timezone is sufficient? – Oliver Charlesworth Dec 24 '13 at 20:48
  • @Oli Charlesworth, Yes it is... but how to calculate it ? – Marcos Dec 24 '13 at 20:56
  • 1
    Oh, it wasn't clear from your question that you **don't** know the remote hosts' timezones! – Oliver Charlesworth Dec 24 '13 at 21:01
  • Still not sure what you mean with server time? The kernel measures in UTC. A user logging in may have any timezone set in his locale, so the timezone is 'merely' a display property. The root user could of course be considered special? Are you after the time zone of a special user? – Harald Dec 24 '13 at 21:15
  • I'm aware of the Time zones of each of the remote hosts. So how do I calculate the approx 'Current' time of the remote host based on GMT +/- offset ? For eg: If GMT is showing as `Tue Dec 24 21:23:43 GMT 2013` on Host A, a remote host in Paris would have 'Current' time as `Tue Dec 24 22:23:43 GMT 2013` (approx). So how do I calculate something like this on Host A for each of my remote hosts? – Marcos Dec 24 '13 at 21:26

2 Answers2

0

Do you want something like

getZoneOffset() {
  #assume ./zones.txt contains lines like
  #example.com 3600
  #i.e. host and time offset in seconds
  remote=$1
  set - $(grep "$1" ./zones.txt)
  echo $2
}

zoneOffsetSecs=$(getZoneOffset $remote)
now=$(date -u +%s)
there=$((now+zoneOffsetSecs))
date -d @$there

You may want to add error handling for cases where your remote host is not found in your zones.txt file.

Harald
  • 4,575
  • 5
  • 33
  • 72
  • I'm not aware of 'getZoneOffset' method. Is it some OS function that is used for Time conversion? It would be helpful if you can elaborate it a bit. In meantime, I'm trying to do some calculation converting the GMT time to seconds, add offset and output it using date -d. But I'm waiting for your reply as well. – Marcos Dec 24 '13 at 21:56
  • No, this is just a placeholder that you need to implement. That was my question, whether you could do this yourself. Basically you need a map from host names to zone offset in seconds. Maps in bash are a bit of a pain, so I suggest to use a simple file containing zones and offsets. I'll update the answer. – Harald Dec 24 '13 at 22:06
  • thanks for details, I've posted an answer as well, please check and suggest any improvements.... BTW, I like the idea of keeping the offset as 'seconds' instead of 'HH:MM' , makes adding easier! – Marcos Dec 24 '13 at 22:28
  • something in `date -u +%s` is not working fine. Test results bash-4.1$ nowgmt=$(date -u +%s) bash-4.1$ nowtm=$(date +%s) bash-4.1$ echo $nowgmt 1387925059 bash-4.1$ echo $nowtm 1387925072 bash-4.1$ date -d @$nowgmt Wed Dec 25 09:44:19 EST 2013 bash-4.1$ date -d @$nowtm Wed Dec 25 09:44:32 EST 2013 bash-4.1$ bash-4.1$ date Wed Dec 25 09:45:09 EST 2013 bash-4.1$ date -u Tue Dec 24 22:45:13 UTC 2013 – Marcos Dec 24 '13 at 22:49
0

I've achieved the result using the below method, seems good till now, but suggestions are most welcome

bash-4.1$ nowgmt=`date -u|awk '{print $4}'|awk -F: '{print ($1 * 3600) + ($2 * 60) + 50400}'`
bash-4.1$ tzoffset="10:30"
bash-4.1$ offsettime=`echo $tzoffset | awk -F: '{print ($1 * 3600) + ($2 * 60)}'`
bash-4.1$ thertime=$((nowgmt + offsettime))
bash-4.1$ date -d @$thertime | awk '{print $4}'
08:46:00
bash-4.1$ date -d @$thertime | awk '{print $4}' | cut -d ":" -f1-2
08:46
Marcos
  • 845
  • 3
  • 10
  • 21