Open nhtha opened 4 months ago
Hi @nhtha, that's too little information to help you
can you provide more details, on how you connect ? with user/password ? with ssh key ? which try of key ? can you provider a code snippet ? the sshd configuration you are using on the server end ?
Hi Fruch, I'm using the demo itself with some slighly modifications on IP, user and password. Here is the code snippet: `from future import print_function import traceback
import paramiko from paramiko_expect import SSHClientInteraction
def main():
HOSTNAME = '192.168.254.129'
USERNAME = 'nhtha'
PASSWORD = '123456'
# PROMPT = r'nhtha@centos8:~\$\s+'
PROMPT = r"\[.+@.+\]#|\[.+@.+\]\$"
# Use SSH client to login
try:
# Create a new SSH client object
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)
# Create a client interaction class which will interact with the host
with SSHClientInteraction(client, timeout=10, display=False) as interact:
interact.expect(PROMPT)
# Run the first command and capture the cleaned output, if you want
# the output without cleaning, simply grab current_output instead.
interact.send('uname -a')
interact.expect(PROMPT)
cmd_output_uname = interact.current_output_clean
# Now let's do the same for the ls command but also set a timeout for
# this specific expect (overriding the default timeout)
interact.send('ls -l /')
interact.expect(PROMPT, timeout=5)
cmd_output_ls = interact.current_output_clean
# To expect multiple expressions, just use a list. You can also
# selectively take action based on what was matched.
# Send the exit command and expect EOF (a closed session)
interact.send('exit')
interact.expect()
# Print the output of each command
print('-' * 79)
print('Cleaned Command Output')
print('-' * 79)
print('uname -a output:')
print(cmd_output_uname)
print('ls -l / output:')
print(cmd_output_ls)
except Exception:
traceback.print_exc()
finally:
try:
client.close()
except Exception:
pass
if name == 'main':
main()
Here is the sshd configuration on the linux server end:
# $OpenBSD: sshd_config,v 1.100 2016/08/15 12:32:04 naddy Exp $
#
HostKey /etc/ssh/ssh_host_rsa_key
HostKey /etc/ssh/ssh_host_ecdsa_key HostKey /etc/ssh/ssh_host_ed25519_key
SyslogFacility AUTHPRIV
AuthorizedKeysFile .ssh/authorized_keys
PasswordAuthentication yes
ChallengeResponseAuthentication no
GSSAPIAuthentication no GSSAPICleanupCredentials no
UsePAM yes
GatewayPorts no X11Forwarding yes
PermitTTY yes
UseDNS no
AcceptEnv LANG LC_CTYPE LC_NUMERIC LC_TIME LC_COLLATE LC_MONETARY LC_MESSAGES AcceptEnv LC_PAPER LC_NAME LC_ADDRESS LC_TELEPHONE LC_MEASUREMENT AcceptEnv LC_IDENTIFICATION LC_ALL LANGUAGE AcceptEnv XMODIFIERS
Subsystem sftp /usr/libexec/openssh/sftp-server
Regards, Ha Nguyen
I would recommend enabling full log levels in the sshd side, to see what are the failures from its side.
Also I would note the version of open ssh being used on the server, and which paramiko version is used, I might be comptabiliy issues between them.
After that I would try a clean example of paramiko itself, and if that isn't working either, I would take the issue there.
When enabling display, I can see that results of uname -a and ls -l command are printed out as below, looks like the send command work fine, but the expect command failed.
uname -a output:
ls -l / output:`
I also enable the sshd debug log. Which log/file do I need to collect and send to this thread? Regards, Ha Nguyen
I'm using: paramiko version 3.2.0, paramiko-expect version 0.3.5, OpenSSH_7.4p1, OpenSSL 1.0.2k-fips 26 Jan 2017. Regards, Ha Nguyen
A strange is : self.channel.recv_ready() is always False. Regards, Ha Nguyen
I also tried with paramiko 3.4.0, the problem still happened in this version. Regards, Ha Nguyen
The same error happened with Centos8 and Centos9 target host. Regards, Ha Nguyen
I also tried with paramiko version 2.12.0, the result is the same. Regards, Ha Nguyen
Dear team, expect always fails with "EXCESS TIME RECV_READY TIMEOUT, did you expect() before a send()" error, including the demo. Please help me to fix it. I am using: Windows 11 Pro, Python 3.10 x64 to connect to Centos 7, 8, 9 machine. Best regards, Ha Nguyen