0

I am facing issue while executing a remote command. If I execute command by logging into remote machine, it works. But if I execute same command from my laptop to remote machine it returns different status code.

Local execution on 192.168.0.166:  
root@monica-E470:~# virsh list --state-shutoff | grep Qcow2 | wc -l
1

Remote Command execution: 
root@sandipd-ThinkPad-E450:~# ssh [email protected] 'virsh list --state-shutoff | grep Qcow2 | wc -l'
[email protected]'s password: 
0

I have tried with different scenarios, but no luck. Has anyone faced same issue?

5
  • 1
    Not sure what's wrong there, but what about ssh [email protected] virsh list --state=shutoff | grep -Fc Qcow2? (Replacing grep | wc -l with grep -c is just an optimization, not really the beef here -- the main thing is to run the grep locally, as there is no reason to run it as root on the remote server.) Commented Sep 1, 2017 at 6:37
  • 1
    (... unless the output is humongous and you would like to avoid sending it over the remote pipe.) Commented Sep 1, 2017 at 6:38
  • Still not working. -c option will not help. After execution of remote command it always return 0. Commented Sep 1, 2017 at 6:58
  • What if you remove the pipeline, or change it to just | cat? I'm guessing there will be no output from virsh list but I have no idea why. Commented Sep 1, 2017 at 7:00
  • I'm not familiar with virsh but are there any environment variables set in the local session that are not there when you ssh? You can compare with the output of env in both cases. Commented Sep 1, 2017 at 7:06

1 Answer 1

2

You have to add a pseudo terminal to execute complex commands in a remote server. Try this instead:

ssh -t [email protected] 'virsh list --state-shutoff | grep Qcow2 | wc -l'

Refer man page of ssh for more info.

Sign up to request clarification or add additional context in comments.

1 Comment

Cool man. Its working. Yes will check this pseudo terminal thing.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.