当不使用 Channel.exec_command() 时,我可以调用 Channel.invoke_shell() 而不事先调用 Channel.get_pty() [英] Can I call Channel.invoke_shell() without calling Channel.get_pty() beforehand, when NOT using Channel.exec_command()
问题描述
我已多次阅读 Paramiko 文档.我仍然无法找到问题的答案:
I have read Paramiko docs multiple times. Still I am not able to find an answer for question I had:
为什么我不能在不事先调用 channel.get_pty()
的情况下调用 channel.invoke_shell()
?– 这是我对文档的理解 – 如果我想使用 channel.exec_command()
,则没有必要调用 channel.get_pty()
.但是,如果我想使用带有 channel.send()
或 channel.recv()
的交互式 shell,该怎么办.是强制性的吗?
Why can't I call channel.invoke_shell()
without calling channel.get_pty()
beforehand? – Here is my understanding from the docs – it is not necessary to call channel.get_pty()
if I want to use channel.exec_command()
. But what if I would like to use an interactive shell with channel.send()
or channel.recv()
. Is it mandatory?
这是我的尝试:
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()
它工作.我只想使用交互式 shell 而不是终端语义.可能吗?
With this I get paramiko.ssh_exception.SSHException: Channel closed
. If I call channel.get_pty()
before invoke_shell()
it works. I would just like to use interactive shell but not terminal semantics. Is it possible?
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
推荐答案
无需调用 Channel.get_pty()
即可调用 Channel.invoke_shell()
.
You can call Channel.invoke_shell()
without calling Channel.get_pty()
.
如果它不起作用,则是您的服务器的限制,而不是 JSch.
而且您已经从您之前的问题中知道确实如此.
If it does not work it is a limitation of your server, not of JSch.
And you already know from your previous question that it is so.
在 Windows 10 Pro 1903 上使用我的 Win32-OpenSSH 7.7,我可以毫无问题地使用没有 TTY 的 shell 会话:
With my Win32-OpenSSH 7.7 on Windows 10 Pro 1903, I can use shell session without TTY without any problem:
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:
...
这篇关于当不使用 Channel.exec_command() 时,我可以调用 Channel.invoke_shell() 而不事先调用 Channel.get_pty()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!