Search code examples
shellunixscripting

Remote SSH - Not printing the variables


I am having trouble execting the below command on remote SSH. Trying to execute 3 commands on remote server and returning its output as CSV value. If the run the command on the command line it works well. But when i save it and run it as shell script, the variable values are not getting printed. However it prints the =s...

s=$(ssh -q user@host "a=$(free -g | grep Mem | awk '{print $2}' | sed 's/ //g') && b=$(lscpu | grep "^CPU(s)" | awk '{print $2}' | sed 's/ //g') && c=$(hostname) && echo ===$a===$b===$c===")

echo $s

Please do suggest if there is a better way to achieve this.

Another peculear thing I noted is in c, where i am trying to print hostname, it prints the kernel version instead of host name.


Solution

  • #!/bin/bash
    
    
    s=$(ssh ttdev 'a=$(free -g | grep Mem | awk "{print \$2}" | sed "s/ //g") && b=$(lscpu | grep "^CPU(s)" | awk "{print \$2}" | sed "s/ //g") && c=$(hostname) && echo ===$a===$b===$c===')
    
    echo $s
    

    ttdev is a name of ssh configuration to my server done under ~/.ssh/config.
    The script should work with user@host as per your case as well.