来自python脚本的SSH,由PHP后端代码触发

时间:2015-12-09 22:28:53

标签: php python shell ssh

我试图从php调用python脚本(使用xamp) python脚本在内部调用shell脚本,shell脚本有ssh和scp命令 在使用exec执行PHP后端代码时,我在xamp日志文件中观察到以下错误 python脚本通过命令行

工作正常
Could not create directory '/sbin/.ssh'.^M
Failed to add the host to the list of known hosts          (/sbin/.ssh/known_hosts).^M
Permission denied, please try again.^M
Permission denied, please try again.^M
Permission denied (publickey,gssapi-keyex,gssapi-with-mic,password).^M
Could not create directory '/sbin/.ssh'.^M
Host key verification failed.^M

2 个答案:

答案 0 :(得分:0)

据推测,python脚本在被PHP调用时由不同的用户运行,而不是手动运行时,它没有适当的权限来执行脚本尝试执行的任何操作。因此,您需要针对脚本要尝试执行的各种操作调整权限/用户组,以使其成功运行。

看这个问题可能是一个很好的起点:

How to check what user php is running as?

确定实际运行脚本的用户后,您可以尝试以该用户身份运行脚本,然后在问题出现时解决问题。

答案 1 :(得分:0)

如果没有看到Python脚本,我无法肯定地说,但这可能与php用户调用脚本时工作目录有关。您应该在代码中使用绝对路径或使用

os.chdir(<path>)

确保您使用正确的工作目录。请注意,如果执行此操作,您可能会遇到其他答案中提到的权限错误,因为.ssh及其中的文件通常只能由其目录所在的用户访问。