问题: 为什么这串代码执行 ls 等命令会报 sh: ls: cannot excute - No such file or directory 的问题?
原因分析:
- paramiko 默认使用 non-interactive / non-login shell
当你使用 exec_command() 时,paramiko 启动的是一个非交互式、非登录式的 shell;这种 shell 不会加载完整的用户环境变量(如 PATH),导致找不到 /bin/ls 等命令。
解决方法:
-
使用命令的完整路径
bash使用 which 命令查看文件的完整路径, 例如: which ls 完整路径为 /bin/ls stdin, stdout, stderr = client.exec_command('/bin/ls')