2016-03-30 24 views
0

私はPythonでスクリプトを書いていますが、私はpxsshとpexpectを使って仕事をやっています。私はそれがMOTDのバナーのためだと信じています。以下は、私がこれまでのコードのために持っているものであり、その下のバナーは、次のようになります。Python(pexpectとpxssh)MOTDバナーを使用したCisco ASAファイアウォールへのSSH

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が過ぎてあなたを取得ことが判明バナーとあなたはただのプロンプトを心配する必要があります。興味があるものについては、私は次のようでした:「」私は同意しないのCisco ASAファイアウォール、にログインしてい

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) 

を文字をホスト名の区切り文字として使用します。私は "。"を置き換える変数を定義しました。 " - "で置き換え、それをsendlineの後のexpectの "expact_exact"として使用しました。

(Ciscoデバイスの)関連するノートでは、「端末ページャ0」は、その特定のセッションのページャバッファを0に変更するためのものです。すべての出力が一度に表示されます。

関連する問題