python 当不使用Channel.exec_command()时,是否可以调用Channel.invoke_shell()而不事先调用Channel.get_pty()

ttvkxqim  于 2023-01-24  发布在  Python
关注(0)|答案(1)|浏览(188)

我已经读了很多遍Paramiko的文档,但是我还是找不到我的问题的答案:
为什么我不能调用channel.invoke_shell()而不事先调用channel.get_pty()?-这是我从文档中得到的理解-如果我想使用channel.exec_command(),就没有必要调用channel.get_pty()。但是如果我想使用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。如果我在invoke_shell()之前调用channel.get_pty(),它就可以工作。我只想使用交互式环境而不是终端语义。这可能吗?
Flink 日志:

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
afdcj2ne

afdcj2ne1#

您可以调用Channel.invoke_shell()而不调用Channel.get_pty()
如果它不起作用,这是服务器的限制,而不是JSch的限制。
你从上一个问题中已经知道是这样。
在Windows 10 Pro 1903上使用我的Win32-OpenSSH 7.7,我可以使用shell会话而不使用TTY(-T),没有任何问题:

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:

...

相关问题