简短问题:
是否可以在需要多级认证时使用paramiko-expect。 我的第一级auth已成功连接,但无法提供用户名,只是超时连接
# 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
上面给出了时间错误
详细问题:
目前这就是每天在窗户上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)
答案 0 :(得分:2)
我参与了一个项目,它必须通过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()