LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This 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


Reply
  Search this Thread
Old 11-29-2018, 08:45 AM   #46
michaelk
Moderator
 
Registered: Aug 2002
Posts: 25,749

Rep: Reputation: 5928Reputation: 5928Reputation: 5928Reputation: 5928Reputation: 5928Reputation: 5928Reputation: 5928Reputation: 5928Reputation: 5928Reputation: 5928Reputation: 5928

What we have is an XY problem. The XY problem is asking about your attempted solution rather than your actual problem.
https://en.wikipedia.org/wiki/XY_problem

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.

Last edited by michaelk; 11-29-2018 at 08:54 AM.
 
Old 11-29-2018, 09:59 AM   #47
l0f4r0
Member
 
Registered: Jul 2018
Location: Paris
Distribution: Debian
Posts: 900

Rep: Reputation: 290Reputation: 290Reputation: 290
Quote:
Originally Posted by berndbausch View Post
By the way, I don’t see any date on google.com. Perhaps the output depends on the browser.
Quote:
Originally Posted by michaelk View Post
My google.com does not display time and date either.
If I'm right, OP talked about that previously :
Quote:
Originally Posted by 1s440 View Post
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.
 
Old 11-29-2018, 10:55 AM   #48
berndbausch
LQ Addict
 
Registered: Nov 2013
Location: Tokyo
Distribution: Mostly Ubuntu and Centos
Posts: 6,316

Rep: Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002
Quote:
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 View Post
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.
 
Old 11-29-2018, 11:08 AM   #49
l0f4r0
Member
 
Registered: Jul 2018
Location: Paris
Distribution: Debian
Posts: 900

Rep: Reputation: 290Reputation: 290Reputation: 290
Quote:
Originally Posted by berndbausch View Post
I have a small doubt that their web server is named google.com.

Maybe OP wanted to talk about a kind of proxy that would alter webpages returned by any web server?
 
Old 11-29-2018, 11:39 AM   #50
scasey
LQ Veteran
 
Registered: Feb 2013
Location: Tucson, AZ, USA
Distribution: CentOS 7.9.2009
Posts: 5,735

Rep: Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212
Quote:
Originally Posted by 1s440 View Post
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.

Last edited by scasey; 11-29-2018 at 11:42 AM.
 
Old 12-04-2018, 02:03 AM   #51
1s440
Member
 
Registered: Mar 2018
Posts: 266

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by scasey View Post
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)

Last edited by 1s440; 12-04-2018 at 02:09 AM.
 
Old 12-04-2018, 03:46 AM   #52
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,930

Rep: Reputation: 7321Reputation: 7321Reputation: 7321Reputation: 7321Reputation: 7321Reputation: 7321Reputation: 7321Reputation: 7321Reputation: 7321Reputation: 7321Reputation: 7321
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.
 
Old 12-04-2018, 04:16 AM   #53
l0f4r0
Member
 
Registered: Jul 2018
Location: Paris
Distribution: Debian
Posts: 900

Rep: Reputation: 290Reputation: 290Reputation: 290
Quote:
Originally Posted by 1s440 View Post
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.
 
Old 12-04-2018, 09:15 AM   #54
michaelk
Moderator
 
Registered: Aug 2002
Posts: 25,749

Rep: Reputation: 5928Reputation: 5928Reputation: 5928Reputation: 5928Reputation: 5928Reputation: 5928Reputation: 5928Reputation: 5928Reputation: 5928Reputation: 5928Reputation: 5928
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

Last edited by michaelk; 12-04-2018 at 10:47 AM.
 
Old 12-04-2018, 10:49 AM   #55
l0f4r0
Member
 
Registered: Jul 2018
Location: Paris
Distribution: Debian
Posts: 900

Rep: Reputation: 290Reputation: 290Reputation: 290
Quote:
Originally Posted by michaelk View Post
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
But my previous warning still applies...

Last edited by l0f4r0; 12-04-2018 at 10:52 AM.
 
Old 12-05-2018, 06:35 AM   #56
1s440
Member
 
Registered: Mar 2018
Posts: 266

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by michaelk View Post
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.

Last edited by 1s440; 12-05-2018 at 07:07 AM.
 
Old 12-05-2018, 06:51 AM   #57
1s440
Member
 
Registered: Mar 2018
Posts: 266

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by l0f4r0 View Post
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

Last edited by 1s440; 12-05-2018 at 06:55 AM.
 
Old 12-05-2018, 07:54 AM   #58
l0f4r0
Member
 
Registered: Jul 2018
Location: Paris
Distribution: Debian
Posts: 900

Rep: Reputation: 290Reputation: 290Reputation: 290
If you want to display the whole date you must not use date or awk this way (configured like this they are too much restrictive)...
 
Old 12-05-2018, 07:59 AM   #59
michaelk
Moderator
 
Registered: Aug 2002
Posts: 25,749

Rep: Reputation: 5928Reputation: 5928Reputation: 5928Reputation: 5928Reputation: 5928Reputation: 5928Reputation: 5928Reputation: 5928Reputation: 5928Reputation: 5928Reputation: 5928
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.

Last edited by michaelk; 12-05-2018 at 08:06 AM.
 
Old 12-05-2018, 08:08 AM   #60
1s440
Member
 
Registered: Mar 2018
Posts: 266

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by l0f4r0 View Post
If you want to display the whole date you must not use date or awk this way (configured like this they are too much restrictive)...
I guess we cannot display date and time together with curl
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
bash script - incrementing a filename in a script tslinux Programming 10 08-05-2003 11:58 PM
can't run a script script from icon in konqueror scottsteibel Linux - Software 1 08-02-2003 07:59 PM
bash script prob: how can i tell the script that a 'dd' has finished? Frustin Linux - General 2 04-02-2003 05:34 AM
E-Mail notification to users via SMS (gateway script ok, but notification script?!?) Riku2015 Linux - Networking 10 03-08-2002 10:16 AM
Including methods from a perl script into another perl script gene_gEnie Programming 3 01-31-2002 05:03 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 10:35 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration