Can I call Channel.invoke_shell() without calling Channel.get_pty() beforehand, when NOT using Channel.exec_command()

℡╲_俬逩灬. 提交于 2020-05-08 17:09:28

问题


I have read Paramiko docs multiple times. Still I am not able to find an answer for question I had:

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?

Here is my try:

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")

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 log:

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

回答1:


You can call Channel.invoke_shell() without calling Channel.get_pty().

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.

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:

...


来源:https://stackoverflow.com/questions/58063024/can-i-call-channel-invoke-shell-without-calling-channel-get-pty-beforehand

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!