在不使用Channel.exec_command()的情况下,可以不事先调用Channel.invoke_shell()而调用Channel.get_pty()吗?

问题描述 投票:1回答:1

我已经阅读了多次Paramiko文档。但我还是找不到我的问题的答案。

为什么我不能调用 channel.invoke_shell() 不鸣则已 channel.get_pty() 事先?- 这是我从文档中得到的理解--不需要调用 channel.get_pty() 如果我想使用 channel.exec_command(). 但是,如果我想使用交互式shell与 channel.send()channel.recv(). 是强制性的吗?

这是我的尝试。

client = paramiko.SSHClient()

# Set SSH key parameters to auto accept unknown hosts
client.load_system_host_keys()
client.set_missing_host_key_policy(paramiko.AutoAddPolicy())

# Connect to the host
client.connect(hostname=hostname, username=username, password=password)
channel = client.get_transport().open_session()
channel.invoke_shell() # ---> Channel closed Exception here
channel.send(cmd + "\r")

用这个我得到 paramiko.ssh_exception.SSHException: Channel closed. 如果我打电话 channel.get_pty() 之前 invoke_shell() 它的工作原理。我只是想使用交互式shell,但不是终端语义。这可能吗?

Plink日志。

plink.exe -T -v user@host -pw password interactive-tool.exe
Looking up host "host" for SSH connection
Connecting to host port 22
We claim version: SSH-2.0-PuTTY_Release_0.72
Remote version: SSH-2.0-OpenSSH_for_Windows_7.7
Using SSH protocol version 2
No GSSAPI security context available
Doing ECDH key exchange with curve Curve25519 and hash SHA-256 (unaccelerated)
Server also has ecdsa-sha2-nistp256/ssh-rsa host keys, but we don't know any of them
Host key fingerprint is:
ssh-ed25519 255 xx:xx:xx:xx
Initialised AES-256 SDCTR (AES-NI accelerated) outbound encryption
Initialised HMAC-SHA-256 (unaccelerated) outbound MAC algorithm
Initialised AES-256 SDCTR (AES-NI accelerated) inbound encryption
Initialised HMAC-SHA-256 (unaccelerated) inbound MAC algorithm
Attempting keyboard-interactive authentication
Using username "user".
Server refused keyboard-interactive authentication
Sent password
Access granted
Access granted. Press Return to begin session.
Opening main session channel
Opened main channel
Started a shell/command
python shell ssh paramiko windows-server-2016
1个回答
0
投票

你可以调用 Channel.invoke_shell() 不鸣则已 Channel.get_pty().

如果不能工作,那是你的服务器的限制,而不是JSch的限制。而且你已经 从你之前的问题中得知 是这样的。

在Windows 10 Pro 1903上使用Win32-OpenSSH 7.7,我可以在没有TTY的情况下使用shell session,没有任何问题。

C:\>echo ipconfig | plink -T username@localhost -pw password
Using username "username".
Microsoft Windows [Version 10.0.18362.356]
(c) 2019 Microsoft Corporation. All rights reserved.

username@COMPUTERNAME C:\Users\username>ipconfig 

Windows IP Configuration


Ethernet adapter Ethernet 2:

...
© www.soinside.com 2019 - 2024. All rights reserved.