0

I don't understand why my command fails when I use it remotely with ssh or pdsh :

Locally it works:

line=` last -F | grep -vE '^(svc_qual|s594998a|reboot|s823850a|s825722a|s559857a|s603256a|root|s823656a|s230281a|s638181a|s835786a)' | head -1 `;login=`echo $line | awk '{print $1}'`dm;user=`getent passwd $login` ;echo $line ; echo $user

But on remote it fails:

ssh User@Server " line=`last -F | grep -vE '^(svc_qual|s594998a|reboot|s823850a|s825722a|s559857a|s603256a|root|s823656a|s230281a|s638181a|s835786a)' | head -1 `;login=`echo $line | awk '{print $1}'`dm;user=`getent passwd $login` ;echo $line ; echo $user  "

I also tried it with pdsh :

/opt/techunix/bin/pdsh.sh -f /tmp/vmod/servers -c "line=`last -F | grep -vE '^(svc_qual|s594998a|reboot|s823850a|s825722a|s559857a|s603256a|root|s823656a|s230281a|s638181a|s835786a)' | head -1 `;login=`echo $line | awk '{print $1}'`dm;user=`getent passwd $login` ;echo $line ; echo $user"
Faheem Mitha
  • 34,649
  • 32
  • 119
  • 183
Med
  • 125
  • 1
  • 2
  • 13
  • 2
    you know that backquote are executed localy ? – Archemar Jun 20 '17 at 10:49
  • Yes i know but i dont think that the error came from the backquote because i had used single quote at the first and the end of the command – Med Jun 20 '17 at 13:08

1 Answers1

0

You will need to escape characters that you don't want your local shell to interpret, namely "`" and "$"

Instead of:

ssh User@Server " line=`last -F | grep -vE '^(svc_qual|s594998a|reboot|s823850a|s825722a|s559857a|s603256a|root|s823656a|s230281a|s638181a|s835786a)' | head -1 `;login=`echo $line | awk '{print $1}'`dm;user=`getent passwd $login` ;echo $line ; echo $user  "

Do:

ssh User@Server " line=\`last -F | grep -vE '^(svc_qual|s594998a|reboot|s823850a|s825722a|s559857a|s603256a|root|s823656a|s230281a|s638181a|s835786a)' | head -1 \`;login=\`echo \$line | awk '{print $1}'\`dm;user=\`getent passwd \$login\` ;echo \$line ; echo \$user  "

You can also choose to put single quotes around the whole ssh command. Then, you need to only change the single quotes of the awk and grep command into double quotes:

ssh User@Server ' line=`last -F | grep -vE "^(svc_qual|s594998a|reboot|s823850a|s825722a|s559857a|s603256a|root|s823656a|s230281a|s638181a|s835786a)" | head -1 `;login=`echo $line | awk "{print $1}"`dm;user=`getent passwd $login` ;echo $line ; echo $user  '
Gohu
  • 1,934
  • 1
  • 15
  • 28