在不使用Channel.exec_命令()时,我是否可以调用Channel.invoke_shell()而不调用Channel.get_pty()

2024-09-28 20:58:11 发布

您现在位置:Python中文网/ 问答频道 /正文

我已经阅读了帕拉米科文件多次。我仍然无法为我的问题找到答案:

为什么我不能事先调用channel.invoke_shell()而不调用channel.get_pty()这是我从文档中得到的理解——如果我想使用^{,就没有必要调用channel.get_pty()。但是,如果我想使用带有channel.send()channel.recv()的交互式shell呢。它是强制性的吗

以下是我的尝试:

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,而不是终端语义。可能吗

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

Tags: tokeyclienthostparamikogetchannelpassword
1条回答
网友
1楼 · 发布于 2024-09-28 20:58:11

您可以调用Channel.invoke_shell(),而无需调用Channel.get_pty()

如果不起作用,则是服务器的限制,而不是JSch的限制。
你已经{a1}知道它是这样的

使用Windows 10 Pro 1903上的Win32 OpenSSH 7.7,我可以在没有TTY的情况下使用shell会话,而不会出现任何问题:

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:

...

相关问题 更多 >