Suresh Kota
Suresh Kota

Reputation: 315

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

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

Upvotes: 2

Views: 2179

Answers (1)

Martin Prikryl
Martin Prikryl

Reputation: 202494

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 (-T) 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:

...

Upvotes: 2

Related Questions