fgimian / paramiko-expect

A Python expect-like extension for the Paramiko SSH library which also supports tailing logs.
MIT License
205 stars 78 forks source link

Assistance need to achieve this task using paramiko-expect #30

Closed rajesh-h closed 7 years ago

rajesh-h commented 7 years ago

Currently this is what is do in Putty on windows on daily basis.

Click on my host then it prompts me like this, yes it has two level authentication

login as:      loginName(Then enter)
Password:   Password1(Then enter)
Username:  UserName(Then enter)
Password:   Password2(Then enter)
Then one more  Enter

Here I get my Required output shown on Putty , this is just a couple line of text which usually gets changed everyday.
So i need to capture this on a daily basis.

I am not sure is this the right way to write my code. I am sure it gets connected to hostname on level 1, but second level username is not passed. So not sure what I am doing wrong.

import traceback
import paramiko
from paramiko_expect import SSHClientInteraction

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

hostname = 'MyCustomServer.com'
login = 'myFirstLevelLogin'
password1 = 'MyFirstPassword'
username = 'mySecondLevelLogin'
password2 = 'MySecondPassword'
prompt1 = 'Username:'
prompt2 = 'Password:'

# Connect to the host
client.connect(hostname=hostname, username=login, password=password1)

# Create a client interaction class which will interact with the host
interact = SSHClientInteraction(client, timeout=10, display=True)
interact.expect(prompt1)
interact.send('username')
interact.expect(prompt2)
interact.send(password2)
interact.expect('Not sure what to put here as i just press enter in my usual putty session ')
interact.send('I send enter key')
interact.expect('This is my final output its two lines of text no command prompt here(i mean the typical user@mymachine:$) this will not be found, this screen goes off after 5 seconds')

cmd_output = interact.current_output_clean

# Send the exit command and expect EOF (a closed session)
interact.send('exit')
interact.expect()

print (cmd_output)

Thank you for this wonderful package

fruch commented 7 years ago

@just10minutes try to print out the interact.current_output_clean between you call to figure out what's going on

also I would recommand using regex for the prompts:

prompt=".*Password:.*"

hopes that's helps

rajesh-h commented 7 years ago

@fruch Thanks a lot for your response.

I have managed it by removing couple of expect lines instead sending direct send command. it worked for me.

I will close this from my end.