且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

Paramiko:调用“cd"带有 exec_command 的命令什么也不做

更新时间:2022-01-31 04:58:14

看起来您正在实施某种交互式程序,允许在服务器上执行一系列命令.

It looks like you are implementing some kind of interactive program that allows executing a sequence of commands on the server.

SSHClient.exec_command 在单独的exec"通道中执行每个命令.各个命令在它们自己的环境中运行.所以如果你执行cd命令,它对后续命令根本没有影响.它们将再次在用户的主目录中启动.

The SSHClient.exec_command executes each command in a separate "exec" channel. The individual commands run in their own environment. So if you execute cd command, it has no effect at all on subsequent commands. They will again start in user's home directory.

如果要实现交互式 shell 会话,请使用 SSHClient.invoke_shell.
例如,请参阅如何与 Paramiko 的交互式 shell 会话进行交互?

If you want to implement an interactive shell session, use SSHClient.invoke_shell.
For an example, see how to interact with Paramiko's interactive shell session?

另见在Paramiko中执行多个命令,使命令受其前任影响.