[SOLVED] Synchronization of date between server and apache webserver, grep and sed instructions.
Linux - NewbieThis Linux forum is for members that are new to Linux.
Just starting out and have a question?
If it is not in the man pages or the how-to's this is the place!
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
My google.com does not display time and date either. So figure out how to use awk, sed or whatever to find the date in the HTML code from the curl command and then format the output of the date command to match. Google can and does change its page all the time so what works today may not tomorrow. Again due to latency there could be a difference between your system time and the time displayed on the google.com page. Even though your system time is correct your comparison will fail.
You could have problems with ntp if your system is a virtual versus a physical machine or if your internet connection is not stable or if it is a mobile device and your switching between access points.
By the way, I don’t see any date on google.com. Perhaps the output depends on the browser.
Quote:
Originally Posted by michaelk
My google.com does not display time and date either.
If I'm right, OP talked about that previously :
Quote:
Originally Posted by 1s440
Hi we have configured apache such that when you browse the web server you see a welcome date for example if you have logged in today it shows today date.
Hi we have configured apache such that when you browse the web server you see a welcome date for example if you have logged in today it shows today date.
Quote:
Originally Posted by l0f4r0
If I'm right, OP talked about that previously :
I have a small doubt that their web server is named google.com.
But if they only want to sync the date (or time?) between two servers, why use HTTP?
I still don't think I know the full story. Luckily it continues to unfold.
when I use "date" on the server its shows correct date and time
when i use "curl -i http://google.com" | sed -n '/Date/s/Date: //p' shows wrong time but correct date
but when I use http://google.com on the browser it shows me correct date and time
I want to create a monitoring check such that "date" command on the server should be same as the date on the browser "http://google.com/ if there is a mismatch then it should be an error.
Hope its clear now
google's url is secure, so should be https://google.com/...although it's configured to resolve to that.
The date/time reported by
Code:
curl -i http://google.com | sed -n '/Date/s/Date: //p'
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
100 219 100 219 0 0 1953 0 --:--:-- --:--:-- --:--:-- 1955
Thu, 29 Nov 2018 17:31:22 GMT
(note there are no quotes as in the command the OP posted...which is one reason to use [code] tags)
That is the same date as here (MST), but the time is GMT...so not a "wrong" time, just a different time zone. MST is -0700, so, from here I'd need to convert the time or get the date command to report GMT.
I'm going to add to the comments that ask Why? If you're running NTP (and you probably are) then the date/time is always going to match google's (with the time zone difference). Every time.
Restating michaelk's question:
What problem are you actually trying to solve?
To insure that the time on your server is always correct, be sure ntp is configured and running.
google's url is secure, so should be https://google.com/...although it's configured to resolve to that.
The date/time reported by
Code:
curl -i http://google.com | sed -n '/Date/s/Date: //p'
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
100 219 100 219 0 0 1953 0 --:--:-- --:--:-- --:--:-- 1955
Thu, 29 Nov 2018 17:31:22 GMT
(note there are no quotes as in the command the OP posted...which is one reason to use [code] tags)
That is the same date as here (MST), but the time is GMT...so not a "wrong" time, just a different time zone. MST is -0700, so, from here I'd need to convert the time or get the date command to report GMT.
I'm going to add to the comments that ask Why? If you're running NTP (and you probably are) then the date/time is always going to match google's (with the time zone difference). Every time.
Restating michaelk's question:
What problem are you actually trying to solve?
To insure that the time on your server is always correct, be sure ntp is configured and running.
As said we have configured NTP correctly and it shows the perfect time. I have already quoted in my earlier statements google.com is just an example.
This is the url http://abcd.com/cd/index.htm as its synced correctly there is no time difference between http://abcd.com/cd/index.htm and time on server (when i give date command on debian server), both shows the correct time. I am just trying to match atleast the hour timings.
Mon Dec 3 09:05:31 CET 2018 (on server)
Mon Dec 3 09:00:02 2018 (from the webserver)
so you need to get the time from the server, parse it and compare. Use python, perl something which can easily parse and compare date/time strings. Shell is not the best, but not impossible.
As said we have configured NTP correctly and it shows the perfect time. I have already quoted in my earlier statements google.com is just an example.
This is the url http://abcd.com/cd/index.htm as its synced correctly there is no time difference between http://abcd.com/cd/index.htm and time on server (when i give date command on debian server), both shows the correct time. I am just trying to match atleast the hour timings.
Mon Dec 3 09:05:31 CET 2018 (on server)
Mon Dec 3 09:00:02 2018 (from the webserver)
Then, if timezone is the same and you just want to compare strings, you have all you need in previous posts (you just need to adapt the regular expression). PS: be careful though as there is a diff of some minutes in your example (I think this is why you want to limit yourself to comparing hours only).
Much as there is no impact between 09:05:31 and 09:00:02 (hour is the same), there will be sometimes one (for example: 09:00:49 and 08:55:20). So you need to take that into account in your logic.
How to parse hours from the time displayed in the browser from your web site or where ever depends on its HTML source code. Here is a quick bash script to just match the hours in the HTML header as compared to your server time.
Code:
#!/bin/bash
wtime=$(curl -i http://server_url/ 2>/dev/null | grep Date | sed 's/Date: //')
t3=$(cut -d' ' -f5 <<< "$wtime")
hr=$(cut -d':' -f1 <<< "$t3")
ltime=$(date -u "+%H")
if [[ $ltime == $hr ]]; then
echo "Hours match"
else
echo "Hours do not match"
fi
How to parse hours from the time displayed in the browser from your web site or where ever depends on its HTML source code. Here is a quick bash script to just match the hours in the HTML header as compared to your server time.
Code:
#!/bin/bash
wtime=$(curl -i http://server_url/ 2>/dev/null | grep Date | sed 's/Date: //')
t3=$(cut -d' ' -f5 <<< "$wtime")
hr=$(cut -d':' -f1 <<< "$t3")
ltime=$(date -u "+%H")
if [[ $ltime == $hr ]]; then
echo "Hours match"
else
echo "Hours not match"
fi
You can make it shorter (maybe the visibility suffers a little bit but 2 cut in a row seems too much IMHO, especially if OP doesn't use the variables later):
Code:
#!/bin/bash
if (( $(date -u "+%H") == $(curl -i http://server_url/ 2>/dev/null | awk 'BEGIN{FS="[ :]"} /Date: /{print $7}') )); then
echo "Hours match"
else
echo "Hours not match"
fi
How to parse hours from the time displayed in the browser from your web site or where ever depends on its HTML source code. Here is a quick bash script to just match the hours in the HTML header as compared to your server time.
Code:
#!/bin/bash
wtime=$(curl -i http://server_url/ 2>/dev/null | grep Date | sed 's/Date: //')
t3=$(cut -d' ' -f5 <<< "$wtime")
hr=$(cut -d':' -f1 <<< "$t3")
ltime=$(date -u "+%H")
if [[ $ltime == $hr ]]; then
echo "Hours match"
else
echo "Hours do not match"
fi
It says hours donot match. I wonder when i use curl -i https://server_url/ it shows time in GMT while in the browser it shows in CET.
You can make it shorter (maybe the visibility suffers a little bit but 2 cut in a row seems too much IMHO, especially if OP doesn't use the variables later):
Code:
#!/bin/bash
if (( $(date -u "+%H") == $(curl -i http://server_url/ 2>/dev/null | awk 'BEGIN{FS="[ :]"} /Date: /{print $7}') )); then
echo "Hours match"
else
echo "Hours not match"
fi
But my previous warning still applies...
I have assigned variables But it says only hours match doesnot display date
Code:
#!/bin/bash
Date=$(date -u "+%H")
echo "$Date"
VAR=$(curl -i http://server_url/ 2>/dev/null | awk 'BEGIN{FS="[ :]"} /Date: /{print $7}')
echo "$VAR"
if (( Date == VAR )); then
echo "Hours match"
else
echo "Hours not match"
fi
The date command -u option (date -u "+%H") displays the hours as UTC which is the same as GMT which should match the header time. In your code your forgot the "$" in your if statement variables and you should use [[ instead of ((.
Code:
if [[ $Date == $VAR ]]; then
echo "Hours match"
else
echo "Hours not match"
fi
Code:
#!/bin/bash
wtime=$(curl -i http://google.com/ 2>/dev/null | grep Date | sed 's/Date: //')
t3=$(cut -d' ' -f5 <<< "$wtime")
hr=$(cut -d':' -f1 <<< "$t3")
ltime=$(date -u "+%H:%M:%S")
lhr=$(date -u "+%H")
echo "Web Server time=$t3, Server time=$ltime"
if [[ $lhr == $hr ]]; then
echo "Hours match"
else
echo "Hours not match"
fi
I've added additional output to display the web server HTML header time and your server time as a visual display.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.