'Sync Time via ssh with remote Linux Server

i want to get the time of a remote server and set that date on my pc. I tried that with the following bash script: (yes, i need to substract 39 years from server year)

[code]

#!/bin/bash
NOW=$(sshpass -p "password" ssh -o StrictHostKeyChecking=no user@serverip -t "date -d '-39 year' '+%Y-%m-%d %T'")
date $NOW

Unfortunately, i get the error:

date: extra operand '20:42:37\\r'

I think the problem is the return. I tried to remove the \r with sed and tr, but it did not work at all. Who can help me?



Solution 1:[1]

#!/bin/bash
NOW=$(sshpass -p "password" ssh -o StrictHostKeyChecking=no user@serverip "date -d '-39 year' '+%Y-%m-%d %T'")
date "$NOW"

You have two problems in your script:

  1. -t introduces a carriage return into the variable
  2. You need to quote the variable, "$NOW"

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 tink