我不明白为什么当我通过 ssh 或 pdsh 远程使用命令时会失败:
在本地它的工作原理:
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
但在远程它失败:
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 "
我也用 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"
答案1
您将需要转义您不希望本地 shell 解释的字符,即“`“ 和 ”$”
代替:
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 "
做:
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 "
您还可以选择在整个 ssh 命令两边加上单引号。然后,只需将awk
andgrep
命令的单引号改为双引号即可:
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 '