2014-02-09 4 views
1

시스코 장치에서 사용자 이름, 암호 및 암호를 확인하는 python 스크립트를 작성하려고합니다.잘못된 모듈을 사용하고 있습니까? pxssh

스크립트가 장치에 로그인했지만 쉘 프롬프트를 설정 및 설정하려고합니다. pxssh가 아닌 표준 pexpect만을 사용해야합니까? 나는 이것을 시도했지만 '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) 

답변

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 
관련 문제