I am unable to use a couple of sudo commands while logging in through ssh. I am using the following command but its not working properly.

ssh hypr1000.opsr.com "ot=sudo virsh list --all |grep running| awk '{print $2}' | tail -2 ;sudo virsh dominfo $ot"

This is the returned error message:

error: command 'dominfo' requires <domain> option

What should I do to make this command run successfully?


You are basically running ssh host "var=value; echo $var". The shell performs variable expansion before ssh received the command. Thus, the actual command as received by ssh becomes:

var=$value; echo 

In order to avoid the shell eating your dollar, use single quotes:

ssh host 'var=value; echo $var'

Considering your command, it looks like you are trying to use the output of one command in the last one. For that to succeed, you must make proper use of command substitution:

ssh host 'output=$(sudo virsh list --all | awk "/running/{print \$2}" | tail -2); sudo virsh dominfo $output'

Or even:

ssh host 'sudo virsh dominfo $(sudo virsh list --all | awk "/running/{print \$2}" | tail -2)'

This assumes that sudo is configured not to ask for a password, otherwise you will get an error such as:

sudo: no tty present and no askpass program specified
  • Awesome.. its working perfectly. I got the exact output that i am looking for. Thanks Lekensteyn :) – sasuke May 27 '13 at 15:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.