I need to get the local time and hostname of the remove server.
Following command executes correctly on local server.
echo "{\"hostname\":\"`hostname`\" , \"date\":\"`date '+%Y-%m-%d %H:%M:%S'`\"}"
and return the following output correctly:
{"hostname":"server1" , "date":"2018-10-29 17:43:51"}
But when I execute the same command on a remote server, it fails:
var1=`ssh server2 " echo "{\"hostname\":\"`hostname`\" , \"date\":\"`date '+%Y-%m-%d %H:%M:%S'`\"}" "`
bash: ": command not found
bash: +%Y-%m-%d %H:%M:%S: command not found
`ssh server2 " echo "{\"hostname\":\"`hostname`\" , \"date\":\"`date '+%Y-%m-%d %H:%M:%S'`\"}" "`
bash: ": command not found
bash: "} : command not found
bash: {hostname:hostnamedate: command not found
$( ... )in place of backticks. Backticks change how backslashes and quotes within them are parsed, so you need to modify your code every time you add a new nesting layer on the outside.