2016-03-30 72 views
0

我正在Python中編寫一個腳本,我正在使用pxssh和pexpect來完成工作,問題是我無法成功發送任何命令和我相信這是由於MOTD的旗幟。下面是我對到目前爲止的代碼再下面是旗幟的樣子:Python(pexpect和pxssh)SSH到思科ASA防火牆與MOTD橫幅

import pexpect 
import getpass 
import pxssh 
import sys 

try: 

    s = pxssh.pxssh() 

    #this is for input file/lists - host, username, and password 
    hostname = ('fw1.aff.tempe') 
    username = ('tmarciniak') 
    password = ('<password>') 
    s.login(hostname, username, password, auto_prompt_reset=False) 

    s.logfile = sys.stdout 

    #s.expect('***.*') #matching the first characters of the MOTD banner for sending command 
    s.sendline('enable') # run a command  
    s.prompt() # match the prompt           
    print(s.before) 

    #s.prompt() # match the prompt 
    # s.sendline('enable') # run a command 
    #s.prompt() # match the prompt 
    print(s.before) # print everything before the prompt 
    s.logout() 
except pxssh.ExceptionPxssh as e: 
    print("pxssh failed on login.") 
    print(e) 

MOTD標語和成功的SSH連接後輸出:

*********************************************** 
*            * 
* This Device is owned by Telesphere Networks * 
*            * 
* Unauthorized Access is Strictly Prohibited * 
*            * 
*  Telesphere NOC: (800) 680-2203  * 
*            * 
*********************************************** 
************************************************************************ 
* 
* Name: Amerifirst Financial - Tempe (36714) 
* 
* Hostname: fw1.aff.tempe 
* 
* Location: 2151 E Broadway Rd 
*   Tempe, AZ 85282 
* 
* Notes: 
* 
************************************************************************ 
Type help or '?' for a list of available commands. 
fw1-aff-tempe> 

回答

0

原來,pxssh讓你過去橫幅,你只需要擔心提示。對於那些好奇,我做了以下內容:「」

def ssh(hostname, username, password, enable_password): 
ssh_session = pxssh.pxssh(maxread=32768, searchwindowsize=1024) 

original_prompt = '%s>' % (hostname.replace('.', '-'),) #matching the first line with the prompt as exact as possible 
enable_prompt = '%s#' % (hostname.replace('.', '-'),) 
ssh_session.login(hostname, username, password, original_prompt=original_prompt, auto_prompt_reset=False) 

ssh_session.sendline('enable') # run a command 
ssh_session.expect_exact('Password:') #match the response as exact as possible 
ssh_session.sendline(enable_password) 

#print('%s#' % (hostname.replace('.','-'),)) #not sure what this part does 
ssh_session.expect_exact(enable_prompt) 

ssh_session.sendline('terminal pager 0') 
ssh_session.expect_exact(enable_prompt) 

我登錄到思科ASA防火牆,不接受字符作爲主機名中的分隔符。我定義了一個可以代替「。」的變量。與「 - 」,然後用它作爲我的「extect_exact」發送線後的期望。

在相關說明中(對思科設備),「終端尋呼機0」用於將該特定會話的尋呼機緩衝區修改爲0;這會導致任何輸出都立即顯示。