LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Networking
User Name
Password
Linux - Networking This forum is for any issue related to networks or networking.
Routing, network cards, OSI, etc. Anything is fair game.

Notices


Reply
  Search this Thread
Old 05-13-2018, 06:13 PM   #1
LinuxMintyFresh
LQ Newbie
 
Registered: Feb 2018
Posts: 19

Rep: Reputation: Disabled
Talking [SOLVED!] Arrange and set date / time extracted from http header in linux router with single curl or wget line


So the following techniques do extract the date and time, the problem is it is not arranged properly for this particular setup. Months must be changed to numbers.

Method 1:

Code:
dateFromServer=$(curl -v --silent https://google.com/ 2>&1 \
 | grep Date | sed -e 's/< Date: //')
date +"%d%m%Y%H%M%S" -d "$dateFromServer"

The result is

'ate: invalid date 'Sat, 12 May 2018 18:49:18 GMT
I have tried re-arranging the code of this first command and it achieves nothing, ordering always stays the same.

Method 2: Get the date from a HTTP response header. Remove clutter. Set the date.
Code:
date -s `curl -I 'https://startpage.com/' 2>/dev/null | grep -i '^date:' | sed 's/^[Dd]ate: //g'`
The results I get are as follows:

Quote:
@Heyzeus:/tmp/home/root# date -s `curl -I 'https://google.com/' 2>/dev/null | grep -i '^date:' | sed 's/^[Dd]ate: //g'` BusyBox v1.25.1 (2018-05-06 13:19:15 EDT) multi-call binary.

Usage: date [OPTIONS] [+FMT] [TIME]

Display time (using +FMT), or set time

[-s,--set] TIME Set time to TIME
-u,--utc Work in UTC (don't convert to local time)
-R,--rfc-2822 Output RFC-2822 compliant date string
-I[SPEC] Output ISO-8601 compliant date string SPEC='date' (default) for date only, 'hours', 'minutes', or 'seconds' for date and time to the indicated precision
-r,--reference FILE Display last modification time of FILE
-d,--date TIME Display TIME, not 'now'
-D FMT Use FMT for -d TIME conversion

Recognized TIME formats: hh:mm[:ss] [YYYY.]MM.DD-hh:mm[:ss] YYYY-MM-DD hh:mm[:ss] [[[[[YY]YY]MM]DD]hh]mm[.ss] 'date TIME' form accepts MMDDhhmm[[YY]YY][.ss] instead
Another
Method 3:

Code:
 date -s "$(wget -qSO- --max-redirect=0 startpage.com 2>&1 | grep Date: | cut -d' ' -f5-8)"
results in: date: invalid date '13 May 2018 22:46:44' [wget code results in the closest match]

The time extracted in the first results is for example: "Sat, 12 May 2018 18:49:18 GMT" and date -s on AsusWRT requires something more like 2018-05-12 18:49:18 [this works] Or as listed under "recognized time formats."

For the first one, "Sat," needs to be removed, month needs replacing with a number, and then arranged properly; Wget requires the month be turned to a number, and arranged as follows 2018-05-12 18:49:18; Would like to have curl and certainly wget versions if possible.

If this could be done exclusively in a single terminal command that would be phenomenal.

Last edited by LinuxMintyFresh; 05-14-2018 at 04:37 PM. Reason: solved!
 
Old 05-13-2018, 07:28 PM   #2
LinuxMintyFresh
LQ Newbie
 
Registered: Feb 2018
Posts: 19

Original Poster
Rep: Reputation: Disabled
Code:
date -u -D '%a, %d %b %Y %H:%M:%S' -d "$(curl -I 'https://1.1.1.1/' 2>/dev/null | grep -i '^date:' | sed 's/^[Dd]ate: //g')"
results in the following:

successfully sets inaccurate date & time (off by a day): Mon May 14 00:22:52 UTC 2018
time is actually Sun May 13 00:17:31

Last edited by LinuxMintyFresh; 05-13-2018 at 07:32 PM.
 
Old 05-13-2018, 07:32 PM   #3
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 21,126

Rep: Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120
Very likely a limitation in the busybox date implementation - in which case you'd have to do your own parsing. sed (amongst others) would work using capture groups.

Edit: you posted while I was (slowly) typing. Above was in reference to your initial post.

Last edited by syg00; 05-13-2018 at 07:54 PM.
 
1 members found this post helpful.
Old 05-14-2018, 04:24 PM   #4
LinuxMintyFresh
LQ Newbie
 
Registered: Feb 2018
Posts: 19

Original Poster
Rep: Reputation: Disabled
Answer

Thanks to Twiglets https://www.snbforums.com/threads/nt...-3#post-404882

Here is an alternative that DOES set the date !!! [-s option].
Prints out 'Date' it retrieves & the 'Date' that is set for comparison.

This works on the AsusWRT / Merlin, the only thing that is odd is that the date retrieved is ".... GMT" and the date utility sets the correct time but changes it to "... DST"
Environment has TZ set to "GMT"

Code:
datetext=$(curl -I 'https://1.1.1.1/' 2>/dev/null | grep "Date:" |sed 's/Date: [A-Z][a-z][a-z], //g'| sed 's/\r//') ; echo "Date Retrieved = $datetext" ; echo -n "Date set = " ; date -s "$datetext" -D'%d %b %Y %T %Z'
 
  


Reply

Tags
curl, date, header, time, wget



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
Wget: last modiffied header missing --time-stamps turned off postcd Linux - General 1 04-18-2014 02:03 AM
How to use curl instead of wget to read http commands Osafeliz Linux - Newbie 1 02-19-2014 03:41 PM
Multiple fallback URLs for single file wget/curl/other ? TheLexx Linux - Software 1 02-01-2014 06:13 AM
curl and wget http post williebens Linux - Newbie 4 10-11-2010 11:03 PM
thunderbird puts time instead of date under 'date' header wabbalee Linux - Software 4 11-26-2006 04:58 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Networking

All times are GMT -5. The time now is 12:31 AM.

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