将通道与 Paramiko 一起使用
Using Channels with Paramiko
我正在将工具从 Ruby 转换为 Python,Ruby 版本使用 Net::SSH to connect to a remote host, and send commands and retrieve responses/data. I have been using paramiko in the Python counterpart, but I'm confused as to the purpose of Channels in paramiko. From what I've read so far, it seems to me that a channel (which uses a paramiko Transport) 用于保持与 SSH 的持久连接,而不是而不是执行命令然后终止连接。
是否需要频道?打开与主机的持久 SSH 连接以按顺序发送和接收多个命令并获取响应,然后在完成后手动关闭连接所需的堆栈是什么?
这是我在翻译成 Python 时遇到问题的两条主线,因为我不确定 Ruby "Channel" 是否直接映射到 paramiko [=26] =]:
@ssh_connection = Net::SSH.start(@linux_server_name,
@server_user_name,
:password => @password,
:paranoid => false)
稍后在代码中
@channel = @ssh_connection.open_channel do |new_channel|
编辑: 为了进一步解释我的问题,我能够使用 paramiko 连接到远程主机并执行多个顺序命令并在不使用传输或通道的情况下获得它们的结果,那么再说一次,paramiko中使用Channel的原因是什么?
def connect(self):
ssh_connection = paramiko.SSHClient()
ssh_connection.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh_connection.connect(self.linux_server_name)
stdin, stdout, stderr = ssh_connection.exec_command("ls -l")
print(stdout.readlines())
stdin, stdout, stderr = ssh_connection.exec_command("ls -l /tmp")
print(stdout.readlines())
ssh_connection.close()
如果 SSH 服务器支持 运行 命令(如 ssh user@host "cmd; cmd; ..."
),exec_command()
就足够了。但是一些 SSH 服务器(例如交换机、路由器等网络设备)只支持开始一个交互式会话。然后你需要使用 invoke_shell()
which returns a Channel.
Transport 和 Channel 都是 SSH 术语(Paramiko 除外)。有关详细信息,请参阅以下 RFC:
我正在将工具从 Ruby 转换为 Python,Ruby 版本使用 Net::SSH to connect to a remote host, and send commands and retrieve responses/data. I have been using paramiko in the Python counterpart, but I'm confused as to the purpose of Channels in paramiko. From what I've read so far, it seems to me that a channel (which uses a paramiko Transport) 用于保持与 SSH 的持久连接,而不是而不是执行命令然后终止连接。
是否需要频道?打开与主机的持久 SSH 连接以按顺序发送和接收多个命令并获取响应,然后在完成后手动关闭连接所需的堆栈是什么?
这是我在翻译成 Python 时遇到问题的两条主线,因为我不确定 Ruby "Channel" 是否直接映射到 paramiko [=26] =]:
@ssh_connection = Net::SSH.start(@linux_server_name,
@server_user_name,
:password => @password,
:paranoid => false)
稍后在代码中
@channel = @ssh_connection.open_channel do |new_channel|
编辑: 为了进一步解释我的问题,我能够使用 paramiko 连接到远程主机并执行多个顺序命令并在不使用传输或通道的情况下获得它们的结果,那么再说一次,paramiko中使用Channel的原因是什么?
def connect(self):
ssh_connection = paramiko.SSHClient()
ssh_connection.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh_connection.connect(self.linux_server_name)
stdin, stdout, stderr = ssh_connection.exec_command("ls -l")
print(stdout.readlines())
stdin, stdout, stderr = ssh_connection.exec_command("ls -l /tmp")
print(stdout.readlines())
ssh_connection.close()
ssh user@host "cmd; cmd; ..."
),exec_command()
就足够了。但是一些 SSH 服务器(例如交换机、路由器等网络设备)只支持开始一个交互式会话。然后你需要使用 invoke_shell()
which returns a Channel.
Transport 和 Channel 都是 SSH 术语(Paramiko 除外)。有关详细信息,请参阅以下 RFC: