I am trying to automatically set a date after boot on an embeddd linux with busybox ( I dont have a RTC). I also dont have ntp command in busybox, nor curl. My wget is very limited too, and I cant seem to get headers with it.

I have seen multiple ways that use curl or wget that parse the header of google.com to get the current date.

But my wget command doesnt support --server-response option:

BusyBox v1.21.0 (2014-11-25 08:52:04 CET) multi-call binary.

Usage: wget [-c|--continue] [-s|--spider] [-q|--quiet] [-O|--output-document FILE]
        [--header 'header: value'] [-Y|--proxy on/off] [-P DIR]
        [-U|--user-agent AGENT] [-T SEC] URL...

Retrieve files via HTTP or FTP

        -s      Spider mode - only check file existence
        -c      Continue retrieval of aborted transfer
        -q      Quiet
        -P DIR  Save to DIR (default .)
        -T SEC  Network read timeout is SEC seconds
        -O FILE Save to FILE ('-' for stdout)
        -U STR  Use STR for User-Agent header
        -Y      Use proxy ('on' or 'off')

I this solution somewhere, but I only have POSIX shell, not bash, so it doesnt work (can't create /dev/tcp/www.google.com/80: nonexistent directory):

exec 5<>/dev/tcp/www.google.com/80
cat mypostfile >&5
cat <&5 # reply

Any ideas how I can still retrieve the current date from somewhere so I can set my system clock with my busybox?

Thank you!


For a proxy at localhost port 8000, if your busybox has telnet, you can try

( echo 'HEAD http://www.google.com/ HTTP/1.0'
  sleep 2 ) |
busybox telnet localhost 8000 |
grep 'Date:'

where the sleep command is approximate and stops telnet closing the connection too soon.

I'm surprised this still works without redirecting to https, which, for example, busybox wget does not support. You would still get the date on the redirection though.

  • Thanks! Since I'm behind a proxy I'd need to talk to the proxy first. So when I try telnet proxy.ip port, then enter HEAD http://www.google.com/ HTTP/1.1 and then hit enter twice it prints out the header. But I dont know how to put this in a bash script. Could you give me a hint there? – tzippy Jan 15 at 8:44
  • I don't think much changes. I've edited the answer for a simple proxy at localhost. You probably still only need the single extra echo on its own to send a blank line after header. If this doesn't flush the data through you can add more echo commands. – meuh Jan 15 at 9:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.