paramiko-expect icon indicating copy to clipboard operation
paramiko-expect copied to clipboard

channel.recv() problem

Open draganmarkovic opened this issue 7 years ago • 7 comments

Hi,

I've experienced issue when trying to match against output when running .expect() right after .send(). Expect does not show output as with pexpect library. It return the command I've sent along with prompts instead of returning result of the command. If I introduce certain delay after each .send() it tends to work more correctly.

Thanks, Dragan

draganmarkovic avatar Nov 17 '17 17:11 draganmarkovic

Same issue here!

marcelosz avatar Dec 03 '17 16:12 marcelosz

can you guy share an example ?

fruch avatar Dec 03 '17 16:12 fruch

timeout = 30
display = False
cmd = passwd
password = mynewpassword
exp_dict = {
                "New password: ":"{}".format(password), 
                "Retype new password: ":"{}".format(password), 
                "passwd: all authentication tokens updated successfully.":"\n"
            }
prompts = []
try:
    for prompt in exp_dict:
        prompts.append(prompt)
except Exception:
    print('Error building prompts list from exp_dict!')
    return '!! Failed !!'

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=server, username=myusername, password=mypassword)

    # Create a client interaction class which will interact with the host
    with SSHClientInteraction(client, timeout=timeout, display=display) as interact:           
        interact.send(cmd)
        for prompt in prompts:
            interact.expect(prompts) #expect a prompt for our prompts list
            interact.send(exp_dict[interact.last_match]) #send the value assinged to prompt key
        interact.send('exit')
        interact.expect()
        status = 'Success'
except Exception:
    status = '!! Failed !!'
    pass
finally:
    try:
        client.close()
    except:
        pass

return status

I am experiencing this same problem. Sometimes it returns success, sometimes it returns failure. If I add a 'time.sleep(.2)' after .send(), it seems to return success more often but still not 100%.

mistergates avatar Jul 20 '18 20:07 mistergates

Same issue here!,the not set time.sleep(2),after send,the expect get erro output, especically same time the output out of order after loop send command and expect

msln1314 avatar Jul 26 '18 01:07 msln1314

I am also having the same issue ..

gurucp avatar Aug 25 '18 07:08 gurucp

Facing similar issue. In my case, first partial output coming in self.current_output_clean and then this issue occurs for successive commands.

sureshkotapydev avatar Sep 13 '19 12:09 sureshkotapydev

Any workarounds or fix yet?

a11apurva avatar Mar 01 '21 07:03 a11apurva