当我尝试使用 popen 执行此操作时,它会返回此错误,但当我在终端中运行此操作时,它会起作用!
popen("ssh -n -f *.*.*.* 'sshfs -o nonempty *.*.*.*:/home/foo/bar/ /foo1/foo2/foo3'", "r");
错误:
ssh_exchange_identification: Connection closed by remote host
我使用公钥和私钥进行 ssh,无需密码,并且它们可以正常工作,因为该命令在终端中完美运行。
我把它改成这样:
popen("ssh -n -f *.*.*.* `sshfs -o nonempty *.*.*.*:/home/foo/bar/ /foo1/foo2/foo3`", "r");
它也会返回错误。
错误:
fuse: bad mount point `/foo1/foo2/foo3': No such file or directory
Cannot fork into background without a command to execute.
我还尝试以这种方式转义内部 ""
:\"\"
但它挂起!
最佳答案
将 ssh 替换为 /usr/bin/ssh
,对其他命令(如 sshfs)执行相同操作。指定命令的完整路径,/usr/sbin/foo
或任何情况。 popen 不一定使用与命令行相同的 shell 来执行命令。检查您的文档。
关于c - 使用popen()在c程序中执行ssh命令,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20072286/