python - SSH 无法在脚本内工作并给出错误 'ssh' 未被识别为内部或外部命令、可操作程序或批处理文件

标签 python windows ssh automation scripting

我计划使用 python 自动执行手动步骤来运行 ssh 命令。我开发了自动执行以下命令并将我登录到虚拟机的代码。每当我在spyder和conda提示符下运行代码时,SSH命令就可以正常工作。每当我打开 cmd 并直接在 key 所在的位置尝试该命令时,该命令都会起作用,但每当我在 cmd 提示符下运行 python 脚本时就会失败并给出错误

os.system('cmd /k "ssh -i <path to private key> <user>@<remotehost>"')

错误:

'ssh' is not recognized as an internal or external command,
operable program or batch file.

如何解决在cmd上运行脚本时出现的错误? 注意:ssh 命令在 cmd 中运行良好,但在 cmd 上运行时在脚本内运行不佳

最佳答案

也许尝试使用命令的实际路径。

示例: os.system('cmd/k "/usr/bin/ssh -i "@"')

那条路对你来说可能会有所不同。 如果您使用的是 Linux 环境,可以输入“which ssh”来查找 ssh 的路径。

关于python - SSH 无法在脚本内工作并给出错误 'ssh' 未被识别为内部或外部命令、可操作程序或批处理文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60155460/

相关文章:

c++ - NtEnumerateKey() 的 KeyInformation 参数

c++ - #在 DLL 文件中包含 Windows.h

bash - 捕获远程脚本的退出代码?

linux - 发送 (2) 在无法访问的网络上成功建立连接

python - Sqlalchemy 在尝试连接到 clickhouse 数据库时显示 "Code 516 Authentication failed"

python - 将带有json列的数据从mysql导入到elasticsearch

python - Python中不可变对象(immutable对象)的类型是什么(针对mypy)

windows - 如何在 Windows XP、2003、7、2008 下以编程方式添加其他打印机驱动程序 (x86/x64)

ssh - 如何检测 ssh key 是否受密码保护?

Python if/else 循环不起作用