Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
rajesh-h opened this issue Jul 14, 2017 · 2 comments
Closed

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

rajesh-h opened this issue Jul 14, 2017 · 2 comments
Labels

Comments

@rajesh-h
Copy link

rajesh-h commented Jul 14, 2017

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 fruch added the question label Sep 29, 2017
@fruch
Copy link
Collaborator

fruch commented Sep 29, 2017

@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
Copy link
Author

rajesh-h commented Oct 2, 2017

@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.

@rajesh-h rajesh-h closed this as completed Oct 2, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants