Centos7 time synchronization API solution

Time:2021-2-10

background
Because of the company’s special environment, servers need to use agents to access the Internet, and agents only support HTTP and socks. Try to use redsocks and proxychains to proxy ntpdate. In case of no result, use API to get the current time and synchronize the time

Implementation mode
Proxychains mode, the error is as follows, unable to proxy normally

[[email protected] ~]# proxychains4 ntpdate -u ntp1.aliyun.com
[proxychains] config file found: /etc/proxychains.conf
[proxychains] preloading /usr/lib/libproxychains4.so
[proxychains] DLL init: proxychains-ng 4.1425 Aug 17:22:25
ntpdate[32353]: no server suitable for synchronization found                    

So we searched the Internet to get the time and set it

#Method 1
date -s "$(curl -s http://quan.suning.com/getSysTime.do|jq '.sysTime2'|sed 's/\"//g')"
date -s $(curl -s http://worldtimeapi.org/api/timezone/Asia/Hong_Kong|jq '.datetime'|sed 's/\"//g')

The above methods are handled by JQ JSON

Installation required

yum install epel-release
yum install jq