Created
July 5, 2016 02:44
-
-
Save yrps/66690a2e749c3f5c52b5b2e22ae2869f to your computer and use it in GitHub Desktop.
Set local date to remote host's date by means of ssh
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/bin/sh | |
| set -eu | |
| usage() { | |
| local basename | |
| basename="$(basename "$0")" | |
| cat<<USAGE | |
| Usage: | |
| $basename [USER@]REMOTEHOST [SSH_OPTIONS]... | |
| Sets the date on this host to the date on REMOTEHOST. | |
| Getting the remote date requires opening an ssh connection to a remote sshd, | |
| and setting it requires root privilege. | |
| Ensure the timezone on both hosts is correct. | |
| USAGE | |
| exit 127 | |
| } | |
| [ $# -lt 1 ] && usage | |
| ssh_args=$* | |
| echo "Getting the remote date..." | |
| # -u specifies UTC and %s formats in epoch seconds | |
| theirs="$(ssh $ssh_args date -u +%s)" | |
| ours="$(date -u +%s)" | |
| echo "Setting local date..." | |
| newdate="$(date -u -s "@$theirs")" | |
| printf '%s\t%s\n' "Old date:" "$(date -d "@$ours")" | |
| printf '%s\t%s\n' "New date:" "$newdate" | |
| printf '%s %d %s\n\n' "Clock adjusted by" "$((theirs - ours))" "sec." |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment