paramiko-expect 涉及多级认证时如何使用

paramiko-expect how to use when multi level auth involved

问题简述:

是否可以在需要多级身份验证时使用 paramiko-expect。 我的第一级身份验证连接成功,但无法提供用户名,它只是连接超时

# Connect to the host
client.connect(hostname=hostname, username=login, password=password1)
interact = SSHClientInteraction(client, timeout=10, display=True)
interact.expect(prompt1)
interact.send('username')
interact.expect(prompt2)
interact.send(password2)
interact.expect()
cmd_output = interact.current_output_clean

上面给出了时间放置错误

详细问题:

目前,这是 windows 每天在 Putty 中执行的操作。

点击我的主机然后它会这样提示我,是的它有两级验证。

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.

我不确定这是不是我编写代码的正确方法。 我确定它连接到级别 1 的主机名,但未传递二级用户名。所以不确定我做错了什么。

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)

我在一个项目中工作,它必须使用 username/password 通过 SSH 登录,然后再次对另一台主机执行相同的操作。由于某种原因,我无法控制网络 ACL 和 SSH 密钥。这是我如何让它工作的:

import paramiko
from paramiko_expect import SSHClientInteraction

user1 = 'admin'
pass1 = 'admin'
user2 = 'root'
pass2 = 'root'

# not needed for this example, but included for reference
user_prompt = '.*$ '   

# will match root user prompt
root_prompt = '.*$ '

# will match Password: or password:
pass_prompt = '.*assword: '

# SSH to host1
ssh_client = paramiko.SSHClient()
ssh_client.set_missing_host_key_policy(
    paramiko.AutoAddPolicy())
ssh_client.connect(hostname='host1', username=user1, password=pass1)

# Interact with SSH client
with SSHClientInteraction(ssh_client, display=True) as interact:
    # Send the command to SSH as root to the final host
    interact.send('ssh {}@host2'.format(user2)
    # Expect the password prompt
    interact.expect(pass_prompt)
    # Send the root password
    interact.send(pass2)
    # Expect the root prompt
    interact.expect(root_prompt)

ssh_client.close()