我正在尝试构建一个python脚本来检查思科设备上的用户名,密码和启用密码。
该脚本清楚地登录到设备,但正在尝试设置和取消设置shell提示符。我应该只使用标准的pexpect而不是pxssh吗?我也试过这个,但是'if'语句似乎不能很好地满足期望。任何建议都会有很大帮助。
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)
答案 0 :(得分:1)
是的,错误的模块。我找到了使用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