linux - 为什么 linux 远程命令或 bash 脚本不返回正确的输出?

标签 linux bash shell remote-server

我在执行远程命令时遇到问题。如果我通过登录到远程机器来执行命令,它就可以工作。但是如果我从笔记本电脑向远程机器执行相同的命令,它会返回不同的状态代码。

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 root@192.168.0.166 'virsh list --state-shutoff | grep Qcow2 | wc -l'
root@192.168.0.166's password: 
0

我尝试过不同的场景,但没有成功。有没有人遇到同样的问题?

最佳答案

您必须添加一个伪终端才能在远程服务器中执行复杂的命令。试试这个:

ssh -t root@192.168.0.166 'virsh list --state-shutoff | grep Qcow2 | wc -l'

引用 sshman 页面获取更多信息。

关于linux - 为什么 linux 远程命令或 bash 脚本不返回正确的输出?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45994394/

相关文章:

python - 如果在 python/bash 中包含来自另一个 .txt 文件的单词,则从文件中删除行

php - Shell 脚本命令 "ldap_search"无法与 php exec 或 shell_exec 命令一起使用

bash - Grep 2 个变量,中间有 ":"

linux - 如何编写 cron 以在新终端中显示脚本输出

Linux BlueZ dbus 通信

mysql错误ubuntu未知数据库

mysql - 子查询不适用于 Windows,但适用于 Linux

c++ - 如何从输入文件中添加和删除用户到linux系统?

python - 使用 shell=True w/list 时忽略 subprocess.call() 参数

linux - bash 。读取符号之间的输入 "words"字符串