gpt4 book ai didi

python - 我使用了错误的模块吗? pxssh

转载 作者:太空宇宙 更新时间:2023-11-03 18:35:29 25 4
gpt4 key购买 nike

我正在尝试构建一个 python 脚本来检查思科设备上的用户名、密码和启用密码。

该脚本明确登录到设备,但正在尝试设置和取消设置 shell 提示符。我应该只使用标准 pexpect 而不是 pxssh 吗?我也尝试过这个,但是“if”语句似乎不能很好地与expect配合使用。任何建议都会有很大帮助。

Exception:
could not set shell prompt
unset PROMPT_COMMAND
^
% Invalid input detected at '^' marker.

3750_core#PS1='[PEXPECT]\$ '
3750_core#set prompt='[PEXPECT]\$ '
^
% Invalid input detected at '^' marker.

代码:

def check_creds(host, user, password, en_passwd):
global success
global failure

try:
ssh = pxssh.pxssh()
ssh.login(host, user, password, en_passwd)
if ssh.prompt(PRIV_EXEC_MODE):
print 'privilege mode creds work'
ssh.logout()
success = True
if ssh.prompt(USER_EXEC_MODE):
print('username and password are correct')
ssh.sendline('enable')
ssh.sendline(en_passwd)
if ssh.prompt(PRIV_EXEC_MODE):
print 'enable password is correct'
ssh.logout()
success = True
else:
print 'enable password is incorrect'
ssh.logout()
failure = True

except pxssh.ExceptionPxssh, fail:
print str(fail)
failure = True
exit(0)

最佳答案

是的,模块错误。我找到了使用 pexpect 和 if 语句的正确方法。

def check_creds(host, user, passwd, en_passwd):
ssh_newkey = 'Are you sure you want to continue connecting (yes/no)?'
constr = 'ssh ' + user + '@' + host
ssh = pexpect.spawn(constr)
ret = ssh.expect([pexpect.TIMEOUT, ssh_newkey, '[P|p]assword:'])
if ret == 0:
print '[-] Error Connecting to ' + host
return
if ret == 1:
ssh.sendline('yes')
ret = ssh.expect([pexpect.TIMEOUT, '[P|p]assword:'])
if ret == 0:
print '[-] Could not accept new key from ' + host
return
ssh.sendline(passwd)
auth = ssh.expect(['[P|p]assword:', '>', '#'])
if auth == 0:
print 'User password is incorrect'
return
if auth == 1:
print('username and password are correct')
ssh.sendline('enable')
ssh.sendline(en_passwd)
enable = ssh.expect(['[P|p]assword:', '#'])
if enable == 0:
print 'enable password is incorrect'
return
if enable == 1:
print 'enable password is correct'
return
if auth == 2:
print 'privilege mode creds work'
return
else:
print 'creds are incorrect'
return

关于python - 我使用了错误的模块吗? pxssh,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21662113/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com